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