1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/gmyth-stream/server/plugins/comm/xmlrpc.py Tue Apr 03 19:42:17 2007 +0100
1.3 @@ -0,0 +1,94 @@
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, fps, acodec, abitrate, width, height, port"
1.22 + elif method == 'play':
1.23 + return "Play the Media: play()"
1.24 + elif method == 'stop':
1.25 + return "Stop the Media: stop()"
1.26 + elif method == 'close':
1.27 + return "Close the connection: close()"
1.28 + elif method == 'getMsg':
1.29 + return "Return the first message in the pool: getMsg()"
1.30 + else:
1.31 + # By convention, return empty
1.32 + # string if no help is available
1.33 + return ""
1.34 +
1.35 + def setup(self, filename, mux, vcodec, vbitrate,\
1.36 + fps, acodec, abitrate, width, height, port):
1.37 + self.recv_pool.append("SETUP")
1.38 + self.recv_pool.append("%s %s %s %s %s %s %s" % (filename, mux, vcodec, vbitrate,\
1.39 + fps, acodec, abitrate, width, height, port)
1.40 + return self.sendMsg()
1.41 +
1.42 + def play(self):
1.43 + self.recv_pool.append("PLAY")
1.44 + return self.sendMsg()
1.45 +
1.46 + def stop(self):
1.47 + self.recv_pool.append("STOP")
1.48 + return self.sendMsg()
1.49 +
1.50 + def close(self):
1.51 + self.recv_pool.append("CLOSE")
1.52 + return self.sendMsg()
1.53 +
1.54 + def sendMsg(self):
1.55 + if self.send_pool != []:
1.56 + return self.send_pool.pop(0)
1.57 + else:
1.58 + return ""
1.59 +
1.60 +
1.61 +class Server:
1.62 +
1.63 + def __init__(self, config):
1.64 + self.host = 'localhost'
1.65 + self.port = int(config.get("Comm", "port"))
1.66 + self.finish = 0
1.67 + self.recv_pool = []
1.68 + self.send_pool = []
1.69 +
1.70 + self.handler = Handler(self.recv_pool, self.send_pool)
1.71 +
1.72 + self.xmlrpc = SimpleXMLRPCServer.SimpleXMLRPCServer((self.host, self.port))
1.73 + self.xmlrpc.register_instance(self.handler)
1.74 +
1.75 +
1.76 + def getMsg(self, size):
1.77 + if self.recv_pool != []:
1.78 + return self.recv_pool.pop(0)
1.79 + else:
1.80 + return ""
1.81 +
1.82 + def sendMsg(self, msg):
1.83 + self.send_pool.append(msg)
1.84 +
1.85 + def Ack(self, command):
1.86 + msg = "[%s] Command %s received" % (lib.now(), command)
1.87 + self.sendMsg(msg + "\n")
1.88 +
1.89 + def getRequest(self):
1.90 + self.xmlrpc.handle_request()
1.91 + return (0, "RPC Client")
1.92 +
1.93 + def disconnect_client(self, connection):
1.94 + connection = 0
1.95 +
1.96 + def stop(self):
1.97 + self.xmlrpc.server_close()