6 import twisted.web.client
10 def set_client(ci,cs,pw):
13 assert(client_cs is None)
16 c.max_outstanding = cfg.getint(cs, 'max_requests_outstanding')
17 c.target_outstanding = cfg.getint(cs, 'target_requests_outstanding')
22 global max_requests_outstanding
24 process_cfg_common_always()
28 c.url = cfg.get('server','url')
31 c.url = c.saddrs[0].url()
33 process_cfg_clients(set_client)
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')
41 process_cfg_ipif(client_cs,
51 queue = PacketQueue(c.max_queue_time)
52 agent = twisted.web.client.Agent(reactor, connectTimeout = c.http_timeout)
54 def outbound(packet, saddr, daddr):
55 #print('OUT ', saddr, daddr, repr(packet))
59 class ResponseConsumer(twisted.internet.protocol.Protocol):
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)
74 resp.deliverBody(ResponseConsumer())
78 print(err, file=sys.stderr)
79 reactor.callLater(c.http_retry, req_fin)
86 def asyncfailure(reason):
95 if outstanding >= c.max_outstanding : break
96 if not queue.nonempty() and outstanding >= c.target_outstanding: break
99 def moredata(s): nonlocal d; d += s
100 queue.process((lambda: len(d)),
105 mime = (b'--b' + crlf +
106 b'Content-Disposition: form-data; name="m"' + crlf +
108 str(c.client) .encode('ascii') + crlf +
109 str(c.target_outstanding) .encode('ascii') + crlf +
111 b'Content-Disposition: form-data; name="d"' + crlf +
112 mime_translate(d) + crlf +
115 print('REQUESTING ', mime, file=sys.stderr)
117 hh = { 'User-Agent': ['hippotat'],
118 'Content-Type': ['multipart/form-data; boundary="b"'] }
119 req = agent.request(b'POST',
121 twisted.web.client.Headers(hh))
122 req.addTimeout(c.http_timeout, reactor)
123 req.addCallbacks(req_ok, req_err)
129 start_ipif(c.ipif_command, outbound)