Revision 37628f11

b/QMP/qmp.py
22 22
    pass
23 23

  
24 24
class QEMUMonitorProtocol:
25
    def __init__(self, address):
25
    def __init__(self, address, server=False):
26 26
        """
27 27
        Create a QEMUMonitorProtocol class.
28 28

  
29 29
        @param address: QEMU address, can be either a unix socket path (string)
30 30
                        or a tuple in the form ( address, port ) for a TCP
31 31
                        connection
32
        @note No connection is established, this is done by the connect() method
32
        @param server: server mode listens on the socket (bool)
33
        @raise socket.error on socket connection errors
34
        @note No connection is established, this is done by the connect() or
35
              accept() methods
33 36
        """
34 37
        self.__events = []
35 38
        self.__address = address
36 39
        self.__sock = self.__get_sock()
37
        self.__sockfile = self.__sock.makefile()
40
        if server:
41
            self.__sock.bind(self.__address)
42
            self.__sock.listen(1)
38 43

  
39 44
    def __get_sock(self):
40 45
        if isinstance(self.__address, tuple):
......
43 48
            family = socket.AF_UNIX
44 49
        return socket.socket(family, socket.SOCK_STREAM)
45 50

  
51
    def __negotiate_capabilities(self):
52
        self.__sockfile = self.__sock.makefile()
53
        greeting = self.__json_read()
54
        if greeting is None or not greeting.has_key('QMP'):
55
            raise QMPConnectError
56
        # Greeting seems ok, negotiate capabilities
57
        resp = self.cmd('qmp_capabilities')
58
        if "return" in resp:
59
            return greeting
60
        raise QMPCapabilitiesError
61

  
46 62
    def __json_read(self, only_event=False):
47 63
        while True:
48 64
            data = self.__sockfile.readline()
......
67 83
        @raise QMPCapabilitiesError if fails to negotiate capabilities
68 84
        """
69 85
        self.__sock.connect(self.__address)
70
        greeting = self.__json_read()
71
        if greeting is None or not greeting.has_key('QMP'):
72
            raise QMPConnectError
73
        # Greeting seems ok, negotiate capabilities
74
        resp = self.cmd('qmp_capabilities')
75
        if "return" in resp:
76
            return greeting
77
        raise QMPCapabilitiesError
86
        return self.__negotiate_capabilities()
87

  
88
    def accept(self):
89
        """
90
        Await connection from QMP Monitor and perform capabilities negotiation.
91

  
92
        @return QMP greeting dict
93
        @raise socket.error on socket connection errors
94
        @raise QMPConnectError if the greeting is not received
95
        @raise QMPCapabilitiesError if fails to negotiate capabilities
96
        """
97
        self.__sock, _ = self.__sock.accept()
98
        return self.__negotiate_capabilities()
78 99

  
79 100
    def cmd_obj(self, qmp_cmd):
80 101
        """

Also available in: Unified diff