gmyth-stream/server/0.1/plugins/comm/xmlrpc.py
author melunko
Wed May 16 17:04:46 2007 +0100 (2007-05-16)
branchtrunk
changeset 670 85d9b58f86b0
permissions -rw-r--r--
[svn r676] new file transfer flag fixed
     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,"\
    19                    " fps, acodec, abitrate, width, height, port, options"
    20         elif method == 'play':
    21             return "Play the Media: play()"
    22         elif method == 'stop':
    23             return "Stop the Media: stop()"
    24         elif method == 'close':
    25             return "Close the connection: close()"
    26         elif method == 'getMsg':
    27             return "Return the first message in the pool: getMsg()"
    28         else:
    29             # By convention, return empty
    30             # string if no help is available
    31             return ""
    32 
    33     def setup(self, filename, mux, vcodec, vbitrate,\
    34             fps, acodec, abitrate, width, height, port, options):
    35 
    36         msg = "%s %s %s %s %s %s %s" % (filename, mux, vcodec, vbitrate,\
    37                                         fps, acodec, abitrate, width, height, port)
    38 
    39         if len(options) > 0:
    40             for opt in options:
    41                 msg += " %s" % opt
    42 
    43         self.recv_pool.append("SETUP")
    44         self.recv_pool.append(msg)
    45         return self.sendMsg()
    46 
    47     def play(self):
    48         self.recv_pool.append("PLAY")
    49         return self.sendMsg()
    50 
    51     def stop(self):
    52         self.recv_pool.append("STOP")
    53         return self.sendMsg()
    54 
    55     def close(self):
    56         self.recv_pool.append("CLOSE")
    57         return self.sendMsg()
    58 
    59     def sendMsg(self):
    60         if self.send_pool != []:
    61             return self.send_pool.pop(0)
    62         else:
    63             return ""
    64 
    65 
    66 class Server:
    67 
    68     def __init__(self, config):
    69         self.host = 'localhost'
    70         self.port = int(config.get("Comm", "port"))
    71         self.finish = 0
    72         self.recv_pool = []
    73         self.send_pool = []
    74 
    75         self.handler = Handler(self.recv_pool, self.send_pool)
    76 
    77         self.xmlrpc = SimpleXMLRPCServer.SimpleXMLRPCServer((self.host, self.port))
    78         self.xmlrpc.register_instance(self.handler)
    79 
    80 
    81     def getMsg(self, size):
    82         if self.recv_pool != []:
    83             return self.recv_pool.pop(0)
    84         else:
    85             return ""
    86 
    87     def sendMsg(self, msg):
    88         self.send_pool.append(msg)
    89 
    90     def Ack(self, command):
    91         msg = "[%s] Command %s received" % (lib.now(), command)
    92         self.sendMsg(msg + "\n")
    93 
    94     def getRequest(self):
    95         self.xmlrpc.handle_request()
    96         return (0, "RPC Client")
    97 
    98     def disconnect_client(self, connection):
    99         connection = 0
   100 
   101     def stop(self):
   102         self.xmlrpc.server_close()