morphbr@565: import lib morphbr@565: import time morphbr@565: import socket morphbr@565: import logging as log morphbr@565: morphbr@565: class Server(object): morphbr@565: morphbr@565: def __init__(self, config): morphbr@565: self.host = '0.0.0.0' morphbr@565: self.port = int(config.get("Comm", "port")) morphbr@565: self.finish = 0 morphbr@565: morphbr@565: addr = (self.host, self.port) morphbr@565: log.debug("Setup TCP server at %s:%s" % addr) morphbr@565: self.tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM) morphbr@565: self.tcp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) morphbr@565: self.tcp.bind(addr) morphbr@565: self.tcp.listen(1) morphbr@565: log.info("TCP server listening at %s:%s (sock=%d)" % morphbr@565: (self.host, self.port, self.tcp.fileno())) morphbr@565: morphbr@565: def getMsg(self): morphbr@565: bytes = [] morphbr@565: try: morphbr@565: while 1: morphbr@565: c = self.con.recv(1) morphbr@565: bytes.append(c) morphbr@565: if not c or c == "\n": morphbr@565: break morphbr@565: except Exception, e: morphbr@565: log.error("Error reading message from client: %s" % e) morphbr@565: return None morphbr@565: morphbr@565: if not bytes or bytes[-1] != "\n": morphbr@565: msg = "".join(bytes) morphbr@565: log.error("Invalid message from client: %r" % msg) morphbr@565: return None morphbr@565: morphbr@565: # remove \n and \r morphbr@565: bytes.pop() morphbr@565: if bytes[-1] == "\r": morphbr@565: bytes.pop() morphbr@565: morphbr@565: msg = "".join(bytes) morphbr@565: log.debug("RECV: %r" % msg) morphbr@565: return msg morphbr@565: morphbr@565: def sendMsg(self, msg): morphbr@565: log.debug("SEND: %r" % msg) morphbr@565: self.con.send(msg + "\n") morphbr@565: morphbr@565: def sendOk(self, payload=None): morphbr@565: self.sendMsg("OK %d" % bool(payload is not None)) morphbr@565: if payload is not None: morphbr@565: if not isinstance(payload, (tuple, list)): morphbr@565: payload = (payload,) morphbr@565: for e in payload: morphbr@565: self.sendMsg("+%s" % e) morphbr@565: self.sendMsg(".") morphbr@565: morphbr@565: def sendNotOk(self, reason=""): morphbr@565: self.sendMsg("NOTOK %r" % reason) morphbr@565: morphbr@565: def getRequest(self): morphbr@565: log.debug("Wait for client request at %s:%s (sock=%d)" % morphbr@565: (self.host, self.port, self.tcp.fileno())) morphbr@565: self.con, self.client = self.tcp.accept() morphbr@565: log.info("Incoming request from %s (con=%s)" % morphbr@565: (self.client, self.con.fileno())) morphbr@565: return (self.con, self.client, self.port) morphbr@565: morphbr@565: def disconnect_client(self, connection): morphbr@565: log.info("Closed request from %s (con=%s)" % morphbr@565: (self.client, self.con.fileno())) morphbr@565: connection.close() morphbr@565: morphbr@565: def stop(self): morphbr@565: log.debug("Stop") morphbr@565: self.tcp.close()