wip
[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')
034284c3
IJ
38
39 process_cfg_ipif(client_cs,
40 (('local', 'client'),
41 ('peer', 'server'),
42 ('rnets', 'routes')))
43
ca732796
IJ
44outstanding = 0
45
46def start_client():
47 global queue
7b07f0b5 48 global agent
ca732796 49 queue = PacketQueue(c.max_queue_time)
7b07f0b5 50 agent = twisted.web.client.Agent(reactor, connectTimeout = c.http_timeout)
ca732796 51
034284c3 52def outbound(packet, saddr, daddr):
ca732796
IJ
53 #print('OUT ', saddr, daddr, repr(packet))
54 queue.append(packet)
55 check_outbound()
56
62b51bcf
IJ
57class ResponseConsumer(twisted.internet.protocol.Protocol):
58 def __init__(self):
59 self._ssd = SlipStreamDecoder(queue_inbound)
60 def dataReceived(self, data):
61 self._ssd.inputdata(mime_translate(data))
62 def connectionMade(self): pass
63 def connectionLost(self, reason):
64 if isinstance(reason, twisted.internet.error.ConnectionDone):
65 self._ssd.flush()
66 else:
67 print(reason, file=sys.stderr)
68
3dbadade 69def req_ok(resp):
62b51bcf 70 resp.deliverBody(ResponseConsumer())
7b07f0b5
IJ
71
72def req_err(err):
c0c90673 73 print(err, file=sys.stderr)
7b07f0b5
IJ
74
75def req_fin(*args):
c0c90673 76 outstanding -= 1
7b07f0b5 77
ca732796
IJ
78def check_outbound():
79 while True:
c0c90673
IJ
80 if outstanding >= c.max_outstanding : break
81 if not queue.nonempty() and outstanding >= c.target_outstanding: break
7b07f0b5
IJ
82
83 d = b''
c0c90673 84 def moredata(s): global d; d += s
7b07f0b5 85 queue.process((lambda: len(d)),
c0c90673 86 moredata,
7b07f0b5
IJ
87 c.max_batch_up)
88 assert(len(d))
89
90 crlf = b'\r\n'
91 mime = (b'--b' + crlf +
92 b'Content-Disposition: form-data; name="m"' + crlf +
93 password + crlf +
94 c.client + crlf +
95 c.target_outstanding + crlf +
96 b'--b' + crlf +
97 b'Content-Disposition: form-data; name="d"' + crlf +
98 mime_translate(d) + crlf +
99 b'--b--' + crlf)
ca732796 100
7b07f0b5
IJ
101 hh = { 'User-Agent': ['hippotat'],
102 'Content-Type': ['multipart/form-data; boundary="b"'] }
3dbadade 103 req = agent.request(b'POST',
7b07f0b5
IJ
104 c.url,
105 twisted.web.client.Headers(hh))
c0c90673 106 req.addTimeout(c.http_timeout)
7b07f0b5
IJ
107 req.addCallbacks(req_ok, req_err)
108 req.addBoth(req_fin)
c0c90673 109 outstanding += 1
034284c3 110
1321ad5f 111common_startup()
87a7c0c7 112process_cfg()
7b07f0b5 113start_client()
034284c3
IJ
114start_ipif(c.ipif_command, outbound)
115common_run()