35 lines
912 B
Ruby
35 lines
912 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.new(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.new(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 |