morphbr@565: import lib morphbr@565: import SimpleXMLRPCServer morphbr@565: morphbr@565: morphbr@565: class Handler: morphbr@565: morphbr@565: def __init__(self, recv_pool, send_pool): morphbr@565: self.recv_pool = recv_pool morphbr@565: self.send_pool = send_pool morphbr@565: self.getMsg = self.sendMsg morphbr@565: morphbr@565: def _listMethods(self): morphbr@565: return ['setup', 'play', 'stop', 'close', 'getMsg'] morphbr@565: morphbr@565: def _methodHelp(self, method): morphbr@565: morphbr@565: if method == 'setup': morphbr@565: return "Setup the Media: setup( filename, mux, vcodec, vbitrate,"\ morphbr@565: " fps, acodec, abitrate, width, height, port, options" morphbr@565: elif method == 'play': morphbr@565: return "Play the Media: play()" morphbr@565: elif method == 'stop': morphbr@565: return "Stop the Media: stop()" morphbr@565: elif method == 'close': morphbr@565: return "Close the connection: close()" morphbr@565: elif method == 'getMsg': morphbr@565: return "Return the first message in the pool: getMsg()" morphbr@565: else: morphbr@565: # By convention, return empty morphbr@565: # string if no help is available morphbr@565: return "" morphbr@565: morphbr@565: def setup(self, filename, mux, vcodec, vbitrate,\ morphbr@565: fps, acodec, abitrate, width, height, port, options): morphbr@565: morphbr@565: msg = "%s %s %s %s %s %s %s" % (filename, mux, vcodec, vbitrate,\ morphbr@565: fps, acodec, abitrate, width, height, port) morphbr@565: morphbr@565: if len(options) > 0: morphbr@565: for opt in options: morphbr@565: msg += " %s" % opt morphbr@565: morphbr@565: self.recv_pool.append("SETUP") morphbr@565: self.recv_pool.append(msg) morphbr@565: return self.sendMsg() morphbr@565: morphbr@565: def play(self): morphbr@565: self.recv_pool.append("PLAY") morphbr@565: return self.sendMsg() morphbr@565: morphbr@565: def stop(self): morphbr@565: self.recv_pool.append("STOP") morphbr@565: return self.sendMsg() morphbr@565: morphbr@565: def close(self): morphbr@565: self.recv_pool.append("CLOSE") morphbr@565: return self.sendMsg() morphbr@565: morphbr@565: def sendMsg(self): morphbr@565: if self.send_pool != []: morphbr@565: return self.send_pool.pop(0) morphbr@565: else: morphbr@565: return "" morphbr@565: morphbr@565: morphbr@565: class Server: morphbr@565: morphbr@565: def __init__(self, config): morphbr@565: self.host = 'localhost' morphbr@565: self.port = int(config.get("Comm", "port")) morphbr@565: self.finish = 0 morphbr@565: self.recv_pool = [] morphbr@565: self.send_pool = [] morphbr@565: morphbr@565: self.handler = Handler(self.recv_pool, self.send_pool) morphbr@565: morphbr@565: self.xmlrpc = SimpleXMLRPCServer.SimpleXMLRPCServer((self.host, self.port)) morphbr@565: self.xmlrpc.register_instance(self.handler) morphbr@565: morphbr@565: morphbr@565: def getMsg(self, size): morphbr@565: if self.recv_pool != []: morphbr@565: return self.recv_pool.pop(0) morphbr@565: else: morphbr@565: return "" morphbr@565: morphbr@565: def sendMsg(self, msg): morphbr@565: self.send_pool.append(msg) morphbr@565: morphbr@565: def Ack(self, command): morphbr@565: msg = "[%s] Command %s received" % (lib.now(), command) morphbr@565: self.sendMsg(msg + "\n") morphbr@565: morphbr@565: def getRequest(self): morphbr@565: self.xmlrpc.handle_request() morphbr@565: return (0, "RPC Client") morphbr@565: morphbr@565: def disconnect_client(self, connection): morphbr@565: connection = 0 morphbr@565: morphbr@565: def stop(self): morphbr@565: self.xmlrpc.server_close()