wip log iface
[hippotat] / client
... / ...
CommitLineData
1#!/usr/bin/python3
2
3from hippotat import *
4
5import twisted.web
6import twisted.web.client
7
8client_cs = None
9
10def set_client(ci,cs,pw):
11 global client_cs
12 global password
13 assert(client_cs is None)
14 client_cs = cs
15 c.client = ci
16 c.max_outstanding = cfg.getint(cs, 'max_requests_outstanding')
17 c.target_outstanding = cfg.getint(cs, 'target_requests_outstanding')
18 password = pw
19
20def process_cfg():
21 global url
22 global max_requests_outstanding
23
24 process_cfg_common_always()
25 process_cfg_server()
26
27 try:
28 c.url = cfg.get('server','url')
29 except NoOptionError:
30 process_cfg_saddrs()
31 c.url = c.saddrs[0].url()
32
33 process_cfg_clients(set_client)
34
35 c.routes = cfg.get('virtual','routes')
36 c.max_queue_time = cfg.getint(client_cs, 'max_queue_time')
37 c.max_batch_up = cfg.getint(client_cs, 'max_batch_up')
38 c.http_timeout = cfg.getint(client_cs, 'http_timeout')
39 c.http_retry = cfg.getint(client_cs, 'http_retry')
40
41 process_cfg_ipif(client_cs,
42 (('local', 'client'),
43 ('peer', 'server'),
44 ('rnets', 'routes')))
45
46outstanding = 0
47
48def start_client():
49 global queue
50 global agent
51 queue = PacketQueue(c.max_queue_time)
52 agent = twisted.web.client.Agent(reactor, connectTimeout = c.http_timeout)
53
54def outbound(packet, saddr, daddr):
55 #print('OUT ', saddr, daddr, repr(packet))
56 queue.append(packet)
57 check_outbound()
58
59class ResponseConsumer(twisted.internet.protocol.Protocol):
60 def __init__(self):
61 self._ssd = SlipStreamDecoder(queue_inbound)
62 def dataReceived(self, data):
63 try: self._ssd.inputdata(mime_translate(data))
64 except Exception as e: asyncfailure(e)
65 def connectionMade(self): pass
66 def connectionLost(self, reason):
67 if isinstance(reason, twisted.internet.error.ConnectionDone):
68 try: self._ssd.flush()
69 except Exception as e: asyncfailure(e)
70 else:
71 asyncfailure(reason)
72
73def req_ok(resp):
74 resp.deliverBody(ResponseConsumer())
75 req_fin()
76
77def req_err(err):
78 print(err, file=sys.stderr)
79 reactor.callLater(c.http_retry, req_fin)
80
81def req_fin(*args):
82 global outstanding
83 outstanding -= 1
84 check_outbound()
85
86def asyncfailure(reason):
87 global outstanding
88 outstanding += 1
89 req_err(reason)
90
91def check_outbound():
92 global outstanding
93
94 while True:
95 if outstanding >= c.max_outstanding : break
96 if not queue.nonempty() and outstanding >= c.target_outstanding: break
97
98 d = b''
99 def moredata(s): nonlocal d; d += s
100 queue.process((lambda: len(d)),
101 moredata,
102 c.max_batch_up)
103
104 crlf = b'\r\n'
105 mime = (b'--b' + crlf +
106 b'Content-Disposition: form-data; name="m"' + crlf +
107 password + crlf +
108 str(c.client) .encode('ascii') + crlf +
109 str(c.target_outstanding) .encode('ascii') + crlf +
110 b'--b' + crlf +
111 b'Content-Disposition: form-data; name="d"' + crlf +
112 mime_translate(d) + crlf +
113 b'--b--' + crlf)
114
115 print('REQUESTING ', mime, file=sys.stderr)
116
117 hh = { 'User-Agent': ['hippotat'],
118 'Content-Type': ['multipart/form-data; boundary="b"'] }
119 req = agent.request(b'POST',
120 c.url,
121 twisted.web.client.Headers(hh))
122 req.addTimeout(c.http_timeout, reactor)
123 req.addCallbacks(req_ok, req_err)
124 outstanding += 1
125
126common_startup()
127process_cfg()
128start_client()
129start_ipif(c.ipif_command, outbound)
130check_outbound()
131common_run()