
This follows the README given earlier, at least in principle, but doesn't implement the entirety of QMP, by any stretch of the imagination. Notable by their absence are error responses, argument validation (for incoming and outgoing messsages of all types), and any visibility into qmp_capabilities. Also missing are integration tests.
35 lines
920 B
Ruby
35 lines
920 B
Ruby
require 'socket'
|
|
|
|
require 'qmp_client/api'
|
|
require 'qmp_client/connectors'
|
|
require 'qmp_client/messages'
|
|
|
|
# This library provides an interface to QEMU's QMP server, allowing you to
|
|
# manage and query running virtual machines.
|
|
#
|
|
# @author Nick Thomas <nick@lupine.me.uk>
|
|
module QMPClient
|
|
|
|
def self.connect_unix(filename, &blk)
|
|
sock = UNIXSocket.connect(filename)
|
|
connect_socket(sock, &blk)
|
|
ensure
|
|
sock.close if sock && !sock.closed?
|
|
end
|
|
|
|
def self.connect_tcp(host, port, local_host=nil, local_port=nil, &blk)
|
|
sock = TCPSocket.connect(host, port, local_host, local_port)
|
|
connect_socket(sock, &blk)
|
|
ensure
|
|
sock.close if sock && !sock.closed?
|
|
end
|
|
|
|
def self.connect_socket(read_socket, write_socket = nil, &blk)
|
|
conn = Connectors::Socket.new(QMPClient::Messages, QMPClient::Messages)
|
|
|
|
conn.run(read_socket, write_socket) do |rq, wq|
|
|
API.run(rq, wq, &blk)
|
|
end
|
|
end
|
|
|
|
end |