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