-
Notifications
You must be signed in to change notification settings - Fork 0
/
TcpConnector.py
56 lines (34 loc) · 1.35 KB
/
TcpConnector.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
'''
Thread to handle Tcp connection requests
At init time returns the port via a queue to the thread (the Env) that created it.
Loops on accepting connection requests and placing requests to RequestRelay() on the ThreadPool's WorkQ to
service the connections (requests from client Workers).
'''
import threading
from TcpHdlr import TcpServer
from MsgRelay import RequestRelay
from CorpsMsgHdlr import CorpsMsgHdlr
from logging import info, warning
class TcpConnector(threading.Thread):
def __init__(self, Host, TheThreadPool, TheAddr2Conc, EnvQueue):
super().__init__()
self.Id = threading.get_ident()
self.Host = Host
self.TheThreadPool = TheThreadPool
self.TheAddr2Conc = TheAddr2Conc
self.NetwHdlr = TcpServer(Host)
self.Port = self.NetwHdlr.get_port()
info(f'TcpConnector: Host {self.Host} Port {self.Port}')
EnvQueue.put(self.Port)
self.start()
def run(self):
while True:
NetwHdlr = None
try:
NetwHdlr = self.NetwHdlr.accept()
except:
warning(f'\n\nTcpConnector: accept exception\n\n')
continue
MsgHdlr = CorpsMsgHdlr(NetwHdlr)
cmd = lambda : RequestRelay(MsgHdlr, self.TheAddr2Conc)
self.TheThreadPool.put_cmd(cmd)