-
Notifications
You must be signed in to change notification settings - Fork 0
/
connector.py
96 lines (79 loc) · 2.77 KB
/
connector.py
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
93
94
import threading
import time
class Connector(object):
CONNECTOR_NAME = 'unnamed connector'
CONNECTOR_VERSION = 0.1
CONNECTOR_LOGNAME = __name__
isrunning=False
def __init__(self,parent,instanceName):
self._parent = parent
self.WG = parent.WG
self.instanceName = instanceName
"""Overide"""
def start(self):
self.log("%s (%s) starting up" % (self.CONNECTOR_NAME, self.instanceName) ,'info','WireGate')
self.isrunning=True
self._thread = threading.Thread(target=self.run,name="%s_main" % self.instanceName)
self._thread.setDaemon(1)
self._thread.start()
def debug(self,msg):
self.log(msg,'debug')
def log(self,msg,severity='info',instance=False):
if not instance:
instance = self.instanceName
self._parent.log(msg,severity,instance)
def shutdown(self):
self.log("%s (%s) shutting down" % (self.CONNECTOR_NAME, self.instanceName) ,'info','WireGate')
self.isrunning=False
if hasattr(self,'_shutdown'):
self._shutdown()
if self._thread.isALive():
self._thread.join(2)
if self._thread.isAlive():
self.log("Shutdown Failed",'critical')
def idle(self,stime):
cnt = 0
while self.isrunning:
if cnt >= stime:
return
cnt+=.5
time.sleep(.5)
def run(self):
"""Overide"""
while self.isrunning:
pass
def setValue(self, dsobj):
self.log("unconfigured setValue in %r called for %s" % (self,dsobj.name) ,'warn','WireGate')
pass
def get_ds_defaults(self,id):
## the defualt config for new Datasotre Items
config = {
}
return config
import SocketServer
import socket
class ConnectorServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer,Connector):
allow_reuse_address = 1
def __init__(self, server_address, RequestHandlerClass):
SocketServer.BaseServer.__init__(self, server_address, RequestHandlerClass)
self.socket = socket.socket(self.address_family,self.socket_type)
self.socket.settimeout(1)
self.server_bind()
self.server_activate()
def run(self):
self.serve_forever()
def serve_forever(self):
while self.isrunning:
self.handle_request()
def get_request(self):
while self.isrunning:
try:
sock, addr = self.socket.accept()
sock.settimeout(None)
return (sock, addr)
except socket.timeout:
if not self.isrunning:
raise socket.error
def shutdown(self):
self.server_close()
Connector.shutdown(self)