6 import twisted.web.client
12 def set_client(ci,cs,pw):
15 assert(client_cs is None)
18 c.max_outstanding = cfg.getint(cs, 'max_requests_outstanding')
19 c.target_outstanding = cfg.getint(cs, 'target_requests_outstanding')
24 global max_requests_outstanding
26 process_cfg_common_always()
30 c.url = cfg.get('server','url')
33 c.url = c.saddrs[0].url()
35 process_cfg_clients(set_client)
37 c.routes = cfg.get('virtual','routes')
38 c.max_queue_time = cfg.getint(client_cs, 'max_queue_time')
39 c.max_batch_up = cfg.getint(client_cs, 'max_batch_up')
40 c.http_timeout = cfg.getint(client_cs, 'http_timeout')
41 c.http_retry = cfg.getint(client_cs, 'http_retry')
43 process_cfg_ipif(client_cs,
53 queue = PacketQueue('up', c.max_queue_time)
54 agent = twisted.web.client.Agent(reactor, connectTimeout = c.http_timeout)
56 def outbound(packet, saddr, daddr):
57 #print('OUT ', saddr, daddr, repr(packet))
61 class ResponseConsumer(twisted.internet.protocol.Protocol):
63 self._ssd = SlipStreamDecoder(queue_inbound)
64 def dataReceived(self, data):
65 try: self._ssd.inputdata(mime_translate(data))
66 except Exception as e: asyncfailure(e)
67 def connectionMade(self): pass
68 def connectionLost(self, reason):
69 if isinstance(reason, twisted.internet.error.ConnectionDone):
70 try: self._ssd.flush()
71 except Exception as e: asyncfailure(e)
76 resp.deliverBody(ResponseConsumer())
80 print(err, file=sys.stderr)
81 reactor.callLater(c.http_retry, req_fin)
88 def asyncfailure(reason):
97 if outstanding >= c.max_outstanding : break
98 if not queue.nonempty() and outstanding >= c.target_outstanding: break
101 def moredata(s): nonlocal d; d += s
102 queue.process((lambda: len(d)),
108 mime = (b'--b' + crlf +
109 b'Content-Type: text/plain; charset="utf-8"' + crlf +
110 b'Content-Disposition: form-data; name="m"' + crlf + crlf +
111 str(c.client) .encode('ascii') + crlf +
113 str(c.target_outstanding) .encode('ascii') + crlf +
116 b'Content-Type: application/octet-stream' + crlf +
117 b'Content-Disposition: form-data; name="d"' + crlf + crlf +
118 mime_translate(d) + crlf
119 ) if len(d) else b'') +
122 #df = open('data.dump.dbg', mode='wb')
125 # POST -use -c 'multipart/form-data; boundary="b"' http://localhost:8099/ <data.dump.dbg
127 log_debug(DBG.HTTP_FULL, 'requesting: ' + str(mime))
129 hh = { 'User-Agent': ['hippotat'],
130 'Content-Type': ['multipart/form-data; boundary="b"'],
131 'Content-Length': [str(len(mime))] }
133 bytesreader = io.BytesIO(mime)
134 producer = twisted.web.client.FileBodyProducer(bytesreader)
136 req = agent.request(b'POST',
138 twisted.web.client.Headers(hh),
140 req.addTimeout(c.http_timeout, reactor)
141 req.addCallbacks(req_ok, req_err)
147 start_ipif(c.ipif_command, outbound)