chiark / gitweb /
break out SlipProtocol
[hippotat.git] / client
1 #!/usr/bin/python3
2
3 from hippotat import *
4
5 client_cs = None
6
7 def set_client(ci,cs,pw):
8   global client_cs
9   global password
10   assert(client_cs is None)
11   client_cs = cs
12   c.client = ci
13   c.max_outstanding = cfg.getint(cs, 'max_requests_outstanding')
14   c.target_outstanding = cfg.getint(cs, 'target_requests_outstanding')
15   password = pw
16
17 def process_cfg():
18   global url
19   global max_requests_outstanding
20
21   process_cfg_common_always()
22   process_cfg_server()
23
24   try:
25     c.url = cfg.get('server','url')
26   except NoOptionError:
27     process_cfg_saddrs()
28     sa = c.saddrs[0].url()
29
30   process_cfg_clients(set_client)
31
32   c.routes = cfg.get('virtual','routes')
33   c.max_queue_time = cfg.getint(client_cs, 'max_queue_time')
34   c.max_batch_up   = cfg.getint(client_cs, 'max_batch_up')
35
36   process_cfg_ipif(client_cs,
37                    (('local', 'client'),
38                     ('peer',  'server'),
39                     ('rnets', 'routes')))
40
41 outstanding = 0
42
43 def start_client():
44   global queue
45   global agent
46   queue = PacketQueue(c.max_queue_time)
47   agent = twisted.web.client.Agent(reactor, connectTimeout = c.http_timeout)
48
49 def outbound(packet, saddr, daddr):
50   #print('OUT ', saddr, daddr, repr(packet))
51   queue.append(packet)
52   check_outbound()
53
54 def req_ok(data)
55
56 def req_err(err):
57   print(err, >>sys.stderr)
58   outstanding--
59
60 def req_fin(*args):  
61
62 def check_outbound():
63   while True:
64     if                         outstanding >= c.max_outstanding   : break
65     if not queue.nonempty() && outstanding >= c.target_outstanding: break
66
67     d = b''
68     queue.process((lambda: len(d)),
69                   (lambda s: d += s),
70                   c.max_batch_up)
71     assert(len(d))
72     
73     crlf = b'\r\n'
74     mime = (b'--b'                                      + crlf +
75             b'Content-Disposition: form-data; name="m"' + crlf +
76             password                                    + crlf +
77             c.client                                    + crlf +
78             c.target_outstanding                        + crlf +
79             b'--b'                                      + crlf +
80             b'Content-Disposition: form-data; name="d"' + crlf +
81             mime_translate(d)                           + crlf +
82             b'--b--'                                    + crlf)
83
84     hh = { 'User-Agent': ['hippotat'],
85            'Content-Type': ['multipart/form-data; boundary="b"'] }
86     req = agent.request('POST',
87                         c.url,
88                         twisted.web.client.Headers(hh))
89     req.addTimeout(c.http_timeout, 
90     req.addCallbacks(req_ok, req_err)
91     req.addBoth(req_fin)
92     outstanding++
93
94 common_startup()
95 process_cfg()
96 start_client()
97 start_ipif(c.ipif_command, outbound)
98 common_run()