morphbr@470
|
1 |
import lib
|
morphbr@453
|
2 |
import time
|
morphbr@453
|
3 |
import socket
|
morphbr@453
|
4 |
|
morphbr@453
|
5 |
class Server:
|
morphbr@453
|
6 |
|
morphbr@453
|
7 |
def __init__(self, config):
|
morphbr@453
|
8 |
self.host = ''
|
morphbr@453
|
9 |
self.port = int(config.get("Comm", "port"))
|
morphbr@453
|
10 |
self.finish = 0
|
morphbr@453
|
11 |
|
morphbr@453
|
12 |
self.tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
morphbr@471
|
13 |
self.tcp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
morphbr@453
|
14 |
self.tcp.bind( (self.host, self.port) )
|
morphbr@453
|
15 |
self.tcp.listen(1)
|
morphbr@453
|
16 |
|
morphbr@470
|
17 |
def getMsg(self, size):
|
morphbr@470
|
18 |
return self.con.recv(size)
|
morphbr@453
|
19 |
|
morphbr@470
|
20 |
def sendMsg(self, msg):
|
morphbr@474
|
21 |
self.con.send(msg + "\n")
|
morphbr@470
|
22 |
|
morphbr@470
|
23 |
def Ack(self, command):
|
morphbr@470
|
24 |
msg = "[%s] Command %s received" % (lib.now(), command)
|
morphbr@474
|
25 |
self.sendMsg(msg)
|
morphbr@453
|
26 |
|
morphbr@453
|
27 |
def getRequest(self):
|
morphbr@470
|
28 |
self.con, self.client = self.tcp.accept()
|
morphbr@470
|
29 |
print "[%s] Received request from ip=%s" % (lib.now(), self.client )
|
morphbr@470
|
30 |
return (self.con, self.client)
|
morphbr@453
|
31 |
|
morphbr@453
|
32 |
def disconnect_client(self, connection):
|
morphbr@453
|
33 |
connection.close()
|
morphbr@453
|
34 |
|
morphbr@453
|
35 |
def stop(self):
|
morphbr@453
|
36 |
self.tcp.close()
|