-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsicmserver.py
More file actions
92 lines (66 loc) · 2.54 KB
/
sicmserver.py
File metadata and controls
92 lines (66 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from twisted.internet import protocol, reactor, defer, utils
#from twisted.protocols import basic
from twisted.internet.endpoints import TCP4ServerEndpoint
from twisted.protocols.basic import LineReceiver
from twisted.internet.protocol import Factory
from pySICM.setup import pysicmsetup as SETUP
import pySICM.helpers as Helpers
import pySICM.commands as Com
import pySICM.commanddef
import os
import json
class SicmXProt(LineReceiver, Com._CommandManager):
# knownCommands = ['GET','SET','SCAN','READY', 'FAKE', 'STOP']
def __init__(self):
super(SicmXProt, self).__init__()
pySICM.commanddef.add_command_list(self)
def connectionMade(self):
d = self.factory.check_connection_allowed()
def onError(err):
return 'Internal Server Error in SicmXProt::connectionMade'
d.addErrback(onError)
print "Connection made callback is "+str(d)
def wR(message):
# if isinstance (message, list):
self.transport.write(str(message)+"\r\n")
if message != "ACK":
self.transport.loseConnection()
d.addCallback(wR)
def connectionLost(self, reason):
self.factory.decrease_clients()
def writeResponse(self, message, newline = True):
if newline:
self.transport.write(str(message)+"\r\n")
else:
self.transport.write(str(message))
def lineReceived(self, line):
_id = None
if line.startswith("#"):
_id, _line = line.split(':')
line = _line
d = self.handle(line, _id = _id)
try:
d.addCallback(self.writeResponse)
except:
pass
class SicmXFactory(Factory):
protocol = SicmXProt
def __init__(self):
self.num_connections = 0
def check_connection_allowed (self):
if self.num_connections == 0:
self.num_connections = 1
return defer.succeed("ACK")
else:
return defer.succeed("NAK")
def decrease_clients(self):
self.num_connections -= 1
if self.protocol.modeobj is not None:
self.protocol.modeobj.setStop()
self.protocol.modeobj=None
# def writeResponse(self, message, newline=True):
# self.protocol.writeResponse(message, newline)
if __name__ == '__main__':
endpoint = TCP4ServerEndpoint(reactor, SETUP.server['port'])
endpoint.listen(SicmXFactory())
reactor.run()