gmyth-stream/server/plugins/comm/xmlrpc.py
author renatofilho
Tue Apr 03 16:42:04 2007 +0100 (2007-04-03)
branchtrunk
changeset 484 27e83a8f68d7
child 492 63d9475228ac
permissions -rw-r--r--
[svn r489] moved server implementation to new dir
     1 import lib
     2 import SimpleXMLRPCServer
     3 
     4 
     5 class Handler:
     6 
     7     def __init__(self, recv_pool, send_pool):
     8         self.recv_pool = recv_pool
     9         self.send_pool = send_pool
    10         self.getMsg = self.sendMsg
    11 
    12     def _listMethods(self):
    13         return ['setup', 'play', 'stop', 'close', 'getMsg']
    14 
    15     def _methodHelp(self, method):
    16 
    17         if method == 'setup':
    18             return "Setup the Media: setup( filename, mux, vcodec, vbitrate, fps, acodec, abitrate, width, height, port"
    19         elif method == 'play':
    20             return "Play the Media: play()"
    21         elif method == 'stop':
    22             return "Stop the Media: stop()"
    23         elif method == 'close':
    24             return "Close the connection: close()"
    25         elif method == 'getMsg':
    26             return "Return the first message in the pool: getMsg()"
    27         else:
    28             # By convention, return empty
    29             # string if no help is available
    30             return ""
    31 
    32     def setup(self, filename, mux, vcodec, vbitrate,\
    33             fps, acodec, abitrate, width, height, port):
    34         self.recv_pool.append("SETUP")
    35         self.recv_pool.append("%s %s %s %s %s %s %s" % (filename, mux, vcodec, vbitrate,\
    36                                                         fps, acodec, abitrate, width, height, port)
    37         return self.sendMsg()
    38 
    39     def play(self):
    40         self.recv_pool.append("PLAY")
    41         return self.sendMsg()
    42 
    43     def stop(self):
    44         self.recv_pool.append("STOP")
    45         return self.sendMsg()
    46 
    47     def close(self):
    48         self.recv_pool.append("CLOSE")
    49         return self.sendMsg()
    50 
    51     def sendMsg(self):
    52         if self.send_pool != []:
    53             return self.send_pool.pop(0)
    54         else:
    55             return ""
    56 
    57 
    58 class Server:
    59 
    60     def __init__(self, config):
    61         self.host = 'localhost'
    62         self.port = int(config.get("Comm", "port"))
    63         self.finish = 0
    64         self.recv_pool = []
    65         self.send_pool = []
    66 
    67         self.handler = Handler(self.recv_pool, self.send_pool)
    68 
    69         self.xmlrpc = SimpleXMLRPCServer.SimpleXMLRPCServer((self.host, self.port))
    70         self.xmlrpc.register_instance(self.handler)
    71 
    72 
    73     def getMsg(self, size):
    74         if self.recv_pool != []:
    75             return self.recv_pool.pop(0)
    76         else:
    77             return ""
    78 
    79     def sendMsg(self, msg):
    80         self.send_pool.append(msg)
    81 
    82     def Ack(self, command):
    83         msg = "[%s] Command %s received" % (lib.now(), command)
    84         self.sendMsg(msg + "\n")
    85 
    86     def getRequest(self):
    87         self.xmlrpc.handle_request()
    88         return (0, "RPC Client")
    89 
    90     def disconnect_client(self, connection):
    91         connection = 0
    92 
    93     def stop(self):
    94         self.xmlrpc.server_close()