-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathreportserver.py
50 lines (43 loc) · 1.48 KB
/
reportserver.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
import logging
import gevent
import gevent.monkey
import gevent.socket as socket
from gevent.server import StreamServer
from server import Server
gevent.monkey.patch_all()
class ReportServer(Server):
def __init__(self, port, task_queue):
self._port = port
self._serving = False
self._serving_greenlet = None
self._report_server = None
self._logger = logging.getLogger(__name__)
self._task_queue = task_queue
def __report_receiver(self, sock, address):
report = ""
fp = sock.makefile("rb")
while True:
line = fp.readline()
if not line:
break
report += line
fp.flush()
sock.shutdown(socket.SHUT_WR)
sock.close()
self._task_queue.put((address[0], report))
def __serve(self):
self._logger.info("[ReportServer] initialized on port " + str(self._port) + " ...")
self._report_server = StreamServer(('', self._port), self.__report_receiver)
self._report_server.serve_forever()
gevent.sleep(0)
def start_server(self):
if not self._serving:
self._serving_greenlet = gevent.spawn(self.__serve)
self._serving = True
gevent.sleep(0)
def stop_server(self):
if self._serving:
gevent.kill(self._serving_greenlet)
self._serving = False
self._report_server.close()
self._logger.info("[ReportServer] shut down")