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