chiark / gitweb /
break out process_request
[hippotat.git] / server
diff --git a/server b/server
index 6da5be4d7acfe5596d3141e1d64124f378a221ad..e3fef89a02bf5300718b2e96cfedfdb886d39823 100755 (executable)
--- a/server
+++ b/server
@@ -2,7 +2,6 @@
 
 from hippotat import *
 
-import sys
 import os
 
 import twisted.internet
@@ -15,41 +14,14 @@ import syslog
 
 clients = { }
 
-defcfg = '''
-[DEFAULT]
-max_batch_down = 65536
-max_queue_time = 10
-max_request_time = 54
-target_requests_outstanding = 3
-ipif = userv root ipif %(local)s,%(peer)s,%(mtu)s,slip %(rnets)s
-
-[virtual]
-mtu = 1500
-# network
-# [server]
-# [relay]
-
-[server]
-addrs = 127.0.0.1 ::1
-port = 8099
-
-[limits]
-max_batch_down = 262144
-max_queue_time = 121
-max_request_time = 121
-target_requests_outstanding = 10
-'''
-
 #---------- "router" ----------
 
 def route(packet, saddr, daddr):
   print('TRACE ', saddr, daddr, packet)
-  try: client = clients[daddr]
+  try: dclient = clients[daddr]
   except KeyError: dclient = None
   if dclient is not None:
     dclient.queue_outbound(packet)
-  elif saddr.is_link_local or daddr.is_link_local:
-    log_discard(packet, saddr, daddr, 'link-local')
   elif daddr == c.server or daddr not in c.network:
     print('TRACE INBOUND ', saddr, daddr, packet)
     queue_inbound(packet)
@@ -58,11 +30,6 @@ def route(packet, saddr, daddr):
   else:
     log_discard(packet, saddr, daddr, 'no client')
 
-def log_discard(packet, saddr, daddr, why):
-  print('DROP ', saddr, daddr, why)
-#  syslog.syslog(syslog.LOG_DEBUG,
-#                'discarded packet %s -> %s (%s)' % (saddr, daddr, why))
-
 #---------- client ----------
 
 class Client():
@@ -87,6 +54,7 @@ class Client():
       req = cfg.getint(cs, k)
       limit = cfg.getint('limits',k)
       self.__dict__[k] = min(req, limit)
+
     self._pq = PacketQueue(self.max_queue_time)
 
     if ip in clients:
@@ -108,6 +76,7 @@ class Client():
 
   def queue_outbound(self, packet):
     self._pq.append(packet)
+    self._check_outbound()
 
   def http_request(self, request):
     request.setHeader('Content-Type','application/octet-stream')
@@ -133,23 +102,12 @@ class Client():
         break
 
       # request, and also some non-expired packets
-      while True:
-        packet = self.pq.popleft()
-        if packet is None: break
-
-        encoded = slip.encode(packet)
-        
-        if request.sentLength > 0:
-          if (request.sentLength + len(slip.delimiter)
-              + len(encoded) > self.max_batch_down):
-            break
-          request.write(slip.delimiter)
-
-        request.write(encoded)
-        self._pq.popLeft()
+      self._pq.process((lambda: request.sentLength),
+                       request.write,
+                       self.max_batch_down)
 
       assert(request.sentLength)
-      self._rq.popLeft()
+      self._rq.popleft()
       request.finish()
       # round again, looking for more to do
 
@@ -157,31 +115,27 @@ class Client():
       request = self._rq.popleft()
       request.finish()
 
+def process_request(request):
+  # find client, update config, etc.
+  metadata = request.args['m']
+  (ci_s, pw, tro) = metadata.split(b'\n')[0:3]
+  ci = ipaddr(ci_s)
+  cl = clients[ci]
+  if pw != cl.pw: raise ValueError('bad password')
+
+  if pw != cl.target_requests_outstanding:
+    raise ...
+
+  try: d = request.args['d']
+  except KeyError: d = ''
+
+  cl.process_arriving_data(d)
+  cl.new_request(request)
+
 class IphttpResource(twisted.web.resource.Resource):
   isLeaf = True
   def render_POST(self, request):
-    # find client, update config, etc.
-    ci = ipaddr(request.args['i'])
-    c = clients[ci]
-    pw = request.args['pw']
-    if pw != c.pw: raise ValueError('bad password')
-
-    # update config
-    for r, w in (('mbd', 'max_batch_down'),
-                 ('mqt', 'max_queue_time'),
-                 ('mrt', 'max_request_time'),
-                 ('tro', 'target_requests_outstanding')):
-      try: v = request.args[r]
-      except KeyError: continue
-      v = int(v)
-      c.__dict__[w] = v
-
-    try: d = request.args['d']
-    except KeyError: d = ''
-
-    c.process_arriving_data(d)
-    c.new_request(request)
-
+    process_request(request)
   def render_GET(self, request):
     return b'<html><body>hippotat</body></html>'
 
@@ -212,10 +166,10 @@ def process_cfg():
 
   process_cfg_ipif('server',
                    (('local','server'),
-                    ('peer','relay'),
+                    ('peer', 'relay'),
                     ('rnets','network')))
 
-common_startup(defcfg)
+common_startup()
 process_cfg()
 start_ipif(c.ipif_command, route)
 start_http()