chiark / gitweb /
break out process_request
[hippotat.git] / server
diff --git a/server b/server
index b746bc26e543433e78e0f08ef0065d99a2b9ac19..e3fef89a02bf5300718b2e96cfedfdb886d39823 100755 (executable)
--- a/server
+++ b/server
@@ -2,7 +2,6 @@
 
 from hippotat import *
 
-import sys
 import os
 
 import twisted.internet
@@ -19,7 +18,7 @@ clients = { }
 
 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)
@@ -31,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():
@@ -108,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
 
@@ -132,25 +115,27 @@ class Client():
       request = self._rq.popleft()
       request.finish()
 
-class IphttpResource(twisted.web.resource.Resource):
-  isLeaf = True
-  def render_POST(self, 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')
+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 ...
+  if pw != cl.target_requests_outstanding:
+    raise ...
 
-    try: d = request.args['d']
-    except KeyError: d = ''
+  try: d = request.args['d']
+  except KeyError: d = ''
 
-    cl.process_arriving_data(d)
-    cl.new_request(request)
+  cl.process_arriving_data(d)
+  cl.new_request(request)
 
+class IphttpResource(twisted.web.resource.Resource):
+  isLeaf = True
+  def render_POST(self, request):
+    process_request(request)
   def render_GET(self, request):
     return b'<html><body>hippotat</body></html>'
 
@@ -184,7 +169,7 @@ def process_cfg():
                     ('peer', 'relay'),
                     ('rnets','network')))
 
-common_startup(defcfg)
+common_startup()
 process_cfg()
 start_ipif(c.ipif_command, route)
 start_http()