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