chiark / gitweb /
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()
84e763c7 31 c.url = 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')
4edf77a3 39 c.http_retry = cfg.getint(client_cs, 'http_retry')
034284c3
IJ
40
41 process_cfg_ipif(client_cs,
42 (('local', 'client'),
43 ('peer', 'server'),
44 ('rnets', 'routes')))
45
ca732796
IJ
46outstanding = 0
47
48def start_client():
49 global queue
7b07f0b5 50 global agent
297b3ebf 51 queue = PacketQueue('up', c.max_queue_time)
7b07f0b5 52 agent = twisted.web.client.Agent(reactor, connectTimeout = c.http_timeout)
ca732796 53
034284c3 54def outbound(packet, saddr, daddr):
ca732796
IJ
55 #print('OUT ', saddr, daddr, repr(packet))
56 queue.append(packet)
57 check_outbound()
58
62b51bcf
IJ
59class ResponseConsumer(twisted.internet.protocol.Protocol):
60 def __init__(self):
61 self._ssd = SlipStreamDecoder(queue_inbound)
62 def dataReceived(self, data):
4edf77a3
IJ
63 try: self._ssd.inputdata(mime_translate(data))
64 except Exception as e: asyncfailure(e)
62b51bcf
IJ
65 def connectionMade(self): pass
66 def connectionLost(self, reason):
67 if isinstance(reason, twisted.internet.error.ConnectionDone):
4edf77a3
IJ
68 try: self._ssd.flush()
69 except Exception as e: asyncfailure(e)
62b51bcf 70 else:
4edf77a3 71 asyncfailure(reason)
62b51bcf 72
3dbadade 73def req_ok(resp):
62b51bcf 74 resp.deliverBody(ResponseConsumer())
4edf77a3 75 req_fin()
7b07f0b5
IJ
76
77def req_err(err):
c0c90673 78 print(err, file=sys.stderr)
4edf77a3 79 reactor.callLater(c.http_retry, req_fin)
7b07f0b5 80
84e763c7
IJ
81def req_fin(*args):
82 global outstanding
c0c90673 83 outstanding -= 1
4edf77a3
IJ
84 check_outbound()
85
86def asyncfailure(reason):
87 global outstanding
88 outstanding += 1
89 req_err(reason)
7b07f0b5 90
ca732796 91def check_outbound():
84e763c7 92 global outstanding
4edf77a3 93
ca732796 94 while True:
c0c90673
IJ
95 if outstanding >= c.max_outstanding : break
96 if not queue.nonempty() and outstanding >= c.target_outstanding: break
7b07f0b5
IJ
97
98 d = b''
84e763c7 99 def moredata(s): nonlocal d; d += s
7b07f0b5 100 queue.process((lambda: len(d)),
c0c90673 101 moredata,
7b07f0b5 102 c.max_batch_up)
7b07f0b5
IJ
103
104 crlf = b'\r\n'
60dc70f9 105 lf = b'\n'
5e234983
IJ
106 mime = (b'--b' + crlf +
107 b'Content-Type: text/plain; charset="utf-8"' + crlf +
108 b'Content-Disposition: form-data; name="m"' + crlf + crlf +
109 str(c.client) .encode('ascii') + crlf +
110 password + crlf +
111 str(c.target_outstanding) .encode('ascii') + crlf +
60dc70f9 112 ((
5e234983
IJ
113 b'--b' + crlf +
114 b'Content-Type: application/octet-stream' + crlf +
115 b'Content-Disposition: form-data; name="d"' + crlf + crlf +
116 mime_translate(d) + crlf
60dc70f9 117 ) if len(d) else b'') +
5e234983 118 b'--b--' + crlf)
ca732796 119
a518aa4b
IJ
120 #df = open('data.dump.dbg', mode='wb')
121 #df.write(mime)
122 #df.close()
534f07df 123 # POST -use -c 'multipart/form-data; boundary="b"' http://localhost:8099/ <data.dump.dbg
60dc70f9 124
297b3ebf 125 log_debug(DBG.HTTP_FULL, 'requesting: ' + str(mime))
4edf77a3 126
7b07f0b5 127 hh = { 'User-Agent': ['hippotat'],
5e234983
IJ
128 'Content-Type': ['multipart/form-data; boundary="b"'],
129 'Content-Length': [str(len(mime))] }
3dbadade 130 req = agent.request(b'POST',
7b07f0b5
IJ
131 c.url,
132 twisted.web.client.Headers(hh))
84e763c7 133 req.addTimeout(c.http_timeout, reactor)
7b07f0b5 134 req.addCallbacks(req_ok, req_err)
c0c90673 135 outstanding += 1
034284c3 136
1321ad5f 137common_startup()
87a7c0c7 138process_cfg()
7b07f0b5 139start_client()
034284c3 140start_ipif(c.ipif_command, outbound)
4edf77a3 141check_outbound()
034284c3 142common_run()