gmyth-stream/plugins/comm/xmlrpc.py
author morphbr
Sat Mar 31 00:55:28 2007 +0100 (2007-03-31)
branchtrunk
changeset 474 07c12d2ceddf
parent 453 a806d8ad0ff0
permissions -rw-r--r--
[svn r479] * GMyth-Stream:
- Error handling: file does not exist on setup (mencoder)
morphbr@470
     1
import lib
morphbr@453
     2
import SimpleXMLRPCServer
morphbr@453
     3
morphbr@453
     4
morphbr@453
     5
class Handler:
morphbr@453
     6
morphbr@470
     7
    def __init__(self, recv_pool, send_pool):
morphbr@470
     8
        self.recv_pool = recv_pool
morphbr@470
     9
        self.send_pool = send_pool
morphbr@470
    10
        self.getMsg = self.sendMsg
morphbr@453
    11
morphbr@453
    12
    def _listMethods(self):
morphbr@470
    13
        return ['setup', 'play', 'stop', 'close', 'getMsg']
morphbr@453
    14
morphbr@453
    15
    def _methodHelp(self, method):
morphbr@453
    16
morphbr@470
    17
        if method == 'setup':
morphbr@470
    18
            return "Setup the Media: setup( filename, mux, vcodec, vbitrate, fps, acodec, abitrate, width, height, port"
morphbr@470
    19
        elif method == 'play':
morphbr@470
    20
            return "Play the Media: play()"
morphbr@470
    21
        elif method == 'stop':
morphbr@470
    22
            return "Stop the Media: stop()"
morphbr@470
    23
        elif method == 'close':
morphbr@470
    24
            return "Close the connection: close()"
morphbr@470
    25
        elif method == 'getMsg':
morphbr@470
    26
            return "Return the first message in the pool: getMsg()"
morphbr@453
    27
        else:
morphbr@453
    28
            # By convention, return empty
morphbr@453
    29
            # string if no help is available
morphbr@453
    30
            return ""
morphbr@453
    31
morphbr@470
    32
    def setup(self, filename, mux, vcodec, vbitrate,\
morphbr@470
    33
            fps, acodec, abitrate, width, height, port):
morphbr@470
    34
        self.recv_pool.append("SETUP")
morphbr@470
    35
        self.recv_pool.append("%s %s %s %s %s %s %s" % (filename, mux, vcodec, vbitrate,\
morphbr@470
    36
                                                        fps, acodec, abitrate, width, height, port)
morphbr@470
    37
        return self.sendMsg()
morphbr@453
    38
morphbr@470
    39
    def play(self):
morphbr@470
    40
        self.recv_pool.append("PLAY")
morphbr@470
    41
        return self.sendMsg()
morphbr@453
    42
morphbr@470
    43
    def stop(self):
morphbr@470
    44
        self.recv_pool.append("STOP")
morphbr@470
    45
        return self.sendMsg()
morphbr@453
    46
morphbr@470
    47
    def close(self):
morphbr@470
    48
        self.recv_pool.append("CLOSE")
morphbr@470
    49
        return self.sendMsg()
morphbr@453
    50
morphbr@470
    51
    def sendMsg(self):
morphbr@470
    52
        if self.send_pool != []:
morphbr@470
    53
            return self.send_pool.pop(0)
morphbr@470
    54
        else:
morphbr@470
    55
            return ""
morphbr@453
    56
morphbr@453
    57
morphbr@453
    58
class Server:
morphbr@453
    59
morphbr@453
    60
    def __init__(self, config):
morphbr@453
    61
        self.host = 'localhost'
morphbr@453
    62
        self.port = int(config.get("Comm", "port"))
morphbr@453
    63
        self.finish = 0
morphbr@470
    64
        self.recv_pool = []
morphbr@470
    65
        self.send_pool = []
morphbr@453
    66
morphbr@470
    67
        self.handler = Handler(self.recv_pool, self.send_pool)
morphbr@453
    68
morphbr@453
    69
        self.xmlrpc = SimpleXMLRPCServer.SimpleXMLRPCServer((self.host, self.port))
morphbr@453
    70
        self.xmlrpc.register_instance(self.handler)
morphbr@453
    71
morphbr@453
    72
morphbr@453
    73
    def getMsg(self, size):
morphbr@470
    74
        if self.recv_pool != []:
morphbr@470
    75
            return self.recv_pool.pop(0)
morphbr@470
    76
        else:
morphbr@453
    77
            return ""
morphbr@453
    78
morphbr@470
    79
    def sendMsg(self, msg):
morphbr@470
    80
        self.send_pool.append(msg)
morphbr@470
    81
morphbr@470
    82
    def Ack(self, command):
morphbr@470
    83
        msg = "[%s] Command %s received" % (lib.now(), command)
morphbr@470
    84
        self.sendMsg(msg + "\n")
morphbr@470
    85
morphbr@453
    86
    def getRequest(self):
morphbr@453
    87
        self.xmlrpc.handle_request()
morphbr@453
    88
        return (0, "RPC Client")
morphbr@453
    89
morphbr@453
    90
    def disconnect_client(self, connection):
morphbr@453
    91
        connection = 0
morphbr@453
    92
morphbr@453
    93
    def stop(self):
morphbr@453
    94
        self.xmlrpc.server_close()