-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTMoohIWebSocketLogger.py
137 lines (121 loc) · 4.49 KB
/
TMoohIWebSocketLogger.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import json
import copy
import asyncio
import threading
import datetime
import MoohLog
from MoohLog import statusmessage, eventmessage, MoohLogger
from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket
TEXT = 0x01
from TMoohIErrors import AlreadyDefinedError
websocketServer = None
def json_serial(obj):
if isinstance(obj, datetime.datetime):
serial = obj.isoformat()
return serial
raise TypeError ("Type not serializable")
class TMoohIWebsocketServer:
def __init__(self, logger, host, port, defaultfilter = []):
global websocketServer
if websocketServer != None:
raise AlreadyDefinedError("Websocket server already created.")
websocketServer = self
self.logger = logger
self.host = host
self.port = port
self.defaultfilter = defaultfilter
self.killing = False
self.clients = []
self.logger.info(MoohLog.eventmessage("websocket","WebSocketServer loading up on %s:%s!"%(self.host, self.port)))
self.server = SimpleWebSocketServer(self.host, self.port, websocketlogger)
self.serverthread = threading.Thread(target = self.runserver)
self.serverthread.start()
self.neweststatus = {}
def quit(self):
self.killing = True
self.logger.info(MoohLog.eventmessage("websocket","WebSocketServer shutting down!"))
self.server.close()
def runserver(self):
while not self.killing:
try:
self.logger.info(MoohLog.eventmessage("websocket","WebSocketServer starting!"))
self.server.serveforever()
except (KeyboardInterrupt):
self.killing = True
except OSError:
if not self.killing: # swallow errors when closing
self.logger.exception()
except Exception:
self.logger.exception()
finally:
self.logger.info(MoohLog.eventmessage("websocket","WebSocketServer shut down!"))
try:
self.server.close()
except OSError:
pass # swallow "An operation was attempted on something that is not a socket" errors
class websocketlogger(WebSocket, MoohLog.logwriter):
#def __init__(self, svr, sock, adr):
# super().__init__(svr, sock, adr)
# websocketServer.logger.debug(eventmessage("websocket","Websocket connecting: {}".format(adr)))
# self.filters = copy.deepcopy(websocketServer.defaultfilter)
def handleConnected(self):
self.filters = copy.deepcopy(websocketServer.defaultfilter)
websocketServer.clients.append(self)
websocketServer.logger.writers.append(self)
websocketServer.logger.debug(eventmessage("websocket","Websocket connected: {}".format(self.address[0])))
# when opening a connection, send the current state
self.inner_write(statusmessage(websocketServer.neweststatus,"status"))
def handleMessage(self):
if self.opcode != TEXT:
websocketServer.logger.debug(eventmessage("websocket","Websocket message received: {} bytes".format(len(self.data))))
else:
websocketServer.logger.debug(eventmessage("websocket","Websocket text message received: {}".format(self.data)))
try:
res = self.data.split(" ",1)
command = res[0]
data = ""
if len(res) == 2:
data = res[1]
jsondecoded = json.loads(data)
if command == "SETFILTER":
if data:
ok = True
if type(jsondecoded) == list:
for x in jsondecoded:
if type(x) != dict:
ok = False
if ok:
self.filters = jsondecoded
response = eventmessage("websocket","Filter updated to %s"%(self.filters,))
response.level = MoohLogger.DEBUG
self.inner_write(response)
else:
ok = False
if not ok:
response = eventmessage("websocket","Could not process filter %s"%(data,))
response.level = MoohLogger.ERROR
self.inner_write(response)
else:
response = eventmessage("websocket","Could not process empty filter")
response.level = MoohLogger.ERROR
self.inner_write(response)
else:
response = eventmessage("websocket","Unknown command %s"%(command,))
response.level = MoohLogger.ERROR
self.inner_write(response)
except Exception:
websocketServer.logger.exception()
def inner_write(self,message):
try:
self.sendMessage(json.dumps(message.serialize(), default=json_serial))#.encode("utf-8")
except Exception as e:
print(e)
def handleClose(self):
try:
websocketServer.logger.writers.remove(self)
websocketServer.clients.remove(self)
except ValueError:
pass
websocketServer.logger.debug(eventmessage("websocket","WebSocket connection closed"))
#else:
# websocketServer.logger.debug(eventmessage("websocket","WebSocket connection closed unexpectedly: {}".format(reason)))