gmyth-stream/server/0.1/plugins/comm/tcp.py
branchtrunk
changeset 830 0e159c5e2d32
parent 829 0a4e6b811acc
child 831 e2baa6947dbf
     1.1 --- a/gmyth-stream/server/0.1/plugins/comm/tcp.py	Tue Aug 28 15:41:35 2007 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,79 +0,0 @@
     1.4 -import lib
     1.5 -import time
     1.6 -import socket
     1.7 -import logging as log
     1.8 -
     1.9 -class Server(object):
    1.10 -
    1.11 -    def __init__(self, config):
    1.12 -        self.host = '0.0.0.0'
    1.13 -        self.port = int(config.get("Comm", "port"))
    1.14 -        self.finish = 0
    1.15 -
    1.16 -        addr = (self.host, self.port)
    1.17 -        log.debug("Setup TCP server at %s:%s" % addr)
    1.18 -        self.tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    1.19 -        self.tcp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    1.20 -        self.tcp.bind(addr)
    1.21 -        self.tcp.listen(1)
    1.22 -        log.info("TCP server listening at %s:%s (sock=%d)" %
    1.23 -                 (self.host, self.port, self.tcp.fileno()))
    1.24 -
    1.25 -    def getMsg(self):
    1.26 -        bytes = []
    1.27 -        try:
    1.28 -            while 1:
    1.29 -                c = self.con.recv(1)
    1.30 -                bytes.append(c)
    1.31 -                if not c or c == "\n":
    1.32 -                    break
    1.33 -        except Exception, e:
    1.34 -            log.error("Error reading message from client: %s" % e)
    1.35 -            return None
    1.36 -
    1.37 -        if not bytes or bytes[-1] != "\n":
    1.38 -            msg = "".join(bytes)
    1.39 -            log.error("Invalid message from client: %r" % msg)
    1.40 -            return None
    1.41 -
    1.42 -        # remove \n and \r
    1.43 -        bytes.pop()
    1.44 -        if bytes[-1] == "\r":
    1.45 -            bytes.pop()
    1.46 -
    1.47 -        msg = "".join(bytes)
    1.48 -        log.debug("RECV: %r" % msg)
    1.49 -        return msg
    1.50 -
    1.51 -    def sendMsg(self, msg):
    1.52 -        log.debug("SEND: %r" % msg)
    1.53 -        self.con.send(msg + "\n")
    1.54 -
    1.55 -    def sendOk(self, payload=None):
    1.56 -        self.sendMsg("OK %d" % bool(payload is not None))
    1.57 -        if payload is not None:
    1.58 -            if not isinstance(payload, (tuple, list)):
    1.59 -                payload = (payload,)
    1.60 -            for e in payload:
    1.61 -                self.sendMsg("+%s" % e)
    1.62 -            self.sendMsg(".")
    1.63 -
    1.64 -    def sendNotOk(self, reason=""):
    1.65 -        self.sendMsg("NOTOK %r" % reason)
    1.66 -
    1.67 -    def getRequest(self):
    1.68 -        log.debug("Wait for client request at %s:%s (sock=%d)" %
    1.69 -                  (self.host, self.port, self.tcp.fileno()))
    1.70 -        self.con, self.client = self.tcp.accept()
    1.71 -        log.info("Incoming request from %s (con=%s)" %
    1.72 -                 (self.client, self.con.fileno()))
    1.73 -        return (self.con, self.client, self.port)
    1.74 -
    1.75 -    def disconnect_client(self, connection):
    1.76 -        log.info("Closed request from %s (con=%s)" %
    1.77 -                 (self.client, self.con.fileno()))
    1.78 -        connection.close()
    1.79 -
    1.80 -    def stop(self):
    1.81 -        log.debug("Stop")
    1.82 -        self.tcp.close()