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