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