gmyth-stream/server/plugins/comm/tcp.py
author morphbr
Mon Apr 09 16:22:37 2007 +0100 (2007-04-09)
branchtrunk
changeset 514 b5352888e3c2
parent 504 2b5355061b66
permissions -rw-r--r--
[svn r519] Gmyth-Streamer: Mencoder update
renatofilho@484
     1
import lib
renatofilho@484
     2
import time
renatofilho@484
     3
import socket
morphbr@511
     4
import logging as log
renatofilho@484
     5
morphbr@499
     6
class Server(object):
renatofilho@484
     7
renatofilho@484
     8
    def __init__(self, config):
morphbr@511
     9
        self.host = '0.0.0.0'
renatofilho@484
    10
        self.port = int(config.get("Comm", "port"))
renatofilho@484
    11
        self.finish = 0
renatofilho@484
    12
morphbr@511
    13
        addr = (self.host, self.port)
morphbr@511
    14
        log.debug("Setup TCP server at %s:%s" % addr)
renatofilho@484
    15
        self.tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
renatofilho@484
    16
        self.tcp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
morphbr@511
    17
        self.tcp.bind(addr)
renatofilho@484
    18
        self.tcp.listen(1)
morphbr@511
    19
        log.info("TCP server listening at %s:%s (sock=%d)" %
morphbr@511
    20
                 (self.host, self.port, self.tcp.fileno()))
renatofilho@484
    21
morphbr@511
    22
    def getMsg(self):
morphbr@511
    23
        bytes = []
morphbr@511
    24
        try:
morphbr@511
    25
            while 1:
morphbr@511
    26
                c = self.con.recv(1)
morphbr@511
    27
                bytes.append(c)
morphbr@511
    28
                if not c or c == "\n":
morphbr@511
    29
                    break
morphbr@511
    30
        except Exception, e:
morphbr@511
    31
            log.error("Error reading message from client: %s" % e)
morphbr@511
    32
            return None
morphbr@511
    33
morphbr@511
    34
        if not bytes or bytes[-1] != "\n":
morphbr@511
    35
            msg = "".join(bytes)
morphbr@511
    36
            log.error("Invalid message from client: %r" % msg)
morphbr@511
    37
            return None
morphbr@511
    38
morphbr@511
    39
        # remove \n and \r
morphbr@511
    40
        bytes.pop()
morphbr@511
    41
        if bytes[-1] == "\r":
morphbr@511
    42
            bytes.pop()
morphbr@511
    43
morphbr@511
    44
        msg = "".join(bytes)
morphbr@511
    45
        log.debug("RECV: %r" % msg)
morphbr@511
    46
        return msg
renatofilho@484
    47
renatofilho@484
    48
    def sendMsg(self, msg):
morphbr@511
    49
        log.debug("SEND: %r" % msg)
renatofilho@484
    50
        self.con.send(msg + "\n")
renatofilho@484
    51
morphbr@511
    52
    def sendOk(self, payload=None):
morphbr@511
    53
        self.sendMsg("OK %d" % bool(payload is not None))
morphbr@511
    54
        if payload is not None:
morphbr@511
    55
            if not isinstance(payload, (tuple, list)):
morphbr@511
    56
                payload = (payload,)
morphbr@511
    57
            for e in payload:
morphbr@511
    58
                self.sendMsg("+%s" % e)
morphbr@511
    59
            self.sendMsg(".")
morphbr@511
    60
morphbr@511
    61
    def sendNotOk(self, reason=""):
morphbr@511
    62
        self.sendMsg("NOTOK %r" % reason)
renatofilho@484
    63
renatofilho@484
    64
    def getRequest(self):
morphbr@511
    65
        log.debug("Wait for client request at %s:%s (sock=%d)" %
morphbr@511
    66
                  (self.host, self.port, self.tcp.fileno()))
renatofilho@484
    67
        self.con, self.client = self.tcp.accept()
morphbr@511
    68
        log.info("Incoming request from %s (con=%s)" %
morphbr@511
    69
                 (self.client, self.con.fileno()))
morphbr@504
    70
        return (self.con, self.client, self.port)
renatofilho@484
    71
renatofilho@484
    72
    def disconnect_client(self, connection):
morphbr@511
    73
        log.info("Closed request from %s (con=%s)" %
morphbr@511
    74
                 (self.client, self.con.fileno()))
renatofilho@484
    75
        connection.close()
renatofilho@484
    76
renatofilho@484
    77
    def stop(self):
morphbr@511
    78
        log.debug("Stop")
renatofilho@484
    79
        self.tcp.close()