gmyth-stream/server/0.1/plugins/comm/xmlrpc.py
branchtrunk
changeset 827 30368d31696e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gmyth-stream/server/0.1/plugins/comm/xmlrpc.py	Tue Aug 28 08:16:13 2007 +0100
     1.3 @@ -0,0 +1,102 @@
     1.4 +import lib
     1.5 +import SimpleXMLRPCServer
     1.6 +
     1.7 +
     1.8 +class Handler:
     1.9 +
    1.10 +    def __init__(self, recv_pool, send_pool):
    1.11 +        self.recv_pool = recv_pool
    1.12 +        self.send_pool = send_pool
    1.13 +        self.getMsg = self.sendMsg
    1.14 +
    1.15 +    def _listMethods(self):
    1.16 +        return ['setup', 'play', 'stop', 'close', 'getMsg']
    1.17 +
    1.18 +    def _methodHelp(self, method):
    1.19 +
    1.20 +        if method == 'setup':
    1.21 +            return "Setup the Media: setup( filename, mux, vcodec, vbitrate,"\
    1.22 +                   " fps, acodec, abitrate, width, height, port, options"
    1.23 +        elif method == 'play':
    1.24 +            return "Play the Media: play()"
    1.25 +        elif method == 'stop':
    1.26 +            return "Stop the Media: stop()"
    1.27 +        elif method == 'close':
    1.28 +            return "Close the connection: close()"
    1.29 +        elif method == 'getMsg':
    1.30 +            return "Return the first message in the pool: getMsg()"
    1.31 +        else:
    1.32 +            # By convention, return empty
    1.33 +            # string if no help is available
    1.34 +            return ""
    1.35 +
    1.36 +    def setup(self, filename, mux, vcodec, vbitrate,\
    1.37 +            fps, acodec, abitrate, width, height, port, options):
    1.38 +
    1.39 +        msg = "%s %s %s %s %s %s %s" % (filename, mux, vcodec, vbitrate,\
    1.40 +                                        fps, acodec, abitrate, width, height, port)
    1.41 +
    1.42 +        if len(options) > 0:
    1.43 +            for opt in options:
    1.44 +                msg += " %s" % opt
    1.45 +
    1.46 +        self.recv_pool.append("SETUP")
    1.47 +        self.recv_pool.append(msg)
    1.48 +        return self.sendMsg()
    1.49 +
    1.50 +    def play(self):
    1.51 +        self.recv_pool.append("PLAY")
    1.52 +        return self.sendMsg()
    1.53 +
    1.54 +    def stop(self):
    1.55 +        self.recv_pool.append("STOP")
    1.56 +        return self.sendMsg()
    1.57 +
    1.58 +    def close(self):
    1.59 +        self.recv_pool.append("CLOSE")
    1.60 +        return self.sendMsg()
    1.61 +
    1.62 +    def sendMsg(self):
    1.63 +        if self.send_pool != []:
    1.64 +            return self.send_pool.pop(0)
    1.65 +        else:
    1.66 +            return ""
    1.67 +
    1.68 +
    1.69 +class Server:
    1.70 +
    1.71 +    def __init__(self, config):
    1.72 +        self.host = 'localhost'
    1.73 +        self.port = int(config.get("Comm", "port"))
    1.74 +        self.finish = 0
    1.75 +        self.recv_pool = []
    1.76 +        self.send_pool = []
    1.77 +
    1.78 +        self.handler = Handler(self.recv_pool, self.send_pool)
    1.79 +
    1.80 +        self.xmlrpc = SimpleXMLRPCServer.SimpleXMLRPCServer((self.host, self.port))
    1.81 +        self.xmlrpc.register_instance(self.handler)
    1.82 +
    1.83 +
    1.84 +    def getMsg(self, size):
    1.85 +        if self.recv_pool != []:
    1.86 +            return self.recv_pool.pop(0)
    1.87 +        else:
    1.88 +            return ""
    1.89 +
    1.90 +    def sendMsg(self, msg):
    1.91 +        self.send_pool.append(msg)
    1.92 +
    1.93 +    def Ack(self, command):
    1.94 +        msg = "[%s] Command %s received" % (lib.now(), command)
    1.95 +        self.sendMsg(msg + "\n")
    1.96 +
    1.97 +    def getRequest(self):
    1.98 +        self.xmlrpc.handle_request()
    1.99 +        return (0, "RPC Client")
   1.100 +
   1.101 +    def disconnect_client(self, connection):
   1.102 +        connection = 0
   1.103 +
   1.104 +    def stop(self):
   1.105 +        self.xmlrpc.server_close()