chiark / gitweb /
wip, python3
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 19 Mar 2017 16:48:22 +0000 (16:48 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 19 Mar 2017 16:48:22 +0000 (16:48 +0000)
server

diff --git a/server b/server
index 8501373..6c229a9 100755 (executable)
--- a/server
+++ b/server
@@ -1,11 +1,11 @@
-#!/usr/bin/python2
+#!/usr/bin/python3
 
-from twisted.web.server import Site
+import twisted.web.server import Site
 from twisted.web.resource import Resource
 from twisted.web.server import NOT_DONE_YET
 from twisted.internet import reactor
 
-import ConfigParser
+import configparser
 import ipaddress
 
 import collections
@@ -29,15 +29,15 @@ def ipnetwork(input):
   return r
 
 defcfg = u'''
-[default]
-max_batch_down: 65536
-max_queue_time: 10
-max_request_time: 54
-
-[global]
-max_batch_down: 262144
-max_queue_time: 121
-max_request_time: 121
+[DEFAULT]
+max_batch_down = 65536
+max_queue_time = 10
+max_request_time = 54
+
+[limits]
+max_batch_down = 262144
+max_queue_time = 121
+max_request_time = 121
 '''
 
 def route(packet. daddr):
@@ -64,7 +64,7 @@ class Client():
     #  .max_request_time
     for k in ('max_batch_down','max_queue_time','max_request_time'):
       req = cfg.getint(cs, k)
-      limit = cfg.getint('global',k)
+      limit = cfg.getint('limits',k)
       self.__dict__[k] = min(req, limit)
 
     def process_arriving_data(self, d):
@@ -81,7 +81,7 @@ class Client():
       self._req_cancel(request)
 
     def queue_outbound(self, packet):
-      self._pq.append((time.time, packet))
+      self._pq.append((time.monotonic(), packet))
 
     def http_request(self, request):
       request.setHeader('Content-Type','application/octet-stream')
@@ -102,9 +102,37 @@ class Client():
         try: (queuetime, packet) = self._pq[0]
         except: IndexError:
           # no packets, oh well
+          break
+
+        age = time.monotonic() - queuetime
+        if age > self.max_queue_time:
+          self._pq.popleft()
           continue
 
-        age = time.time() - queuetime
+        if request is None:
+          # no request
+          break
+
+        # request, and also some non-expired packets
+        while True:
+          try: (dummy, packet) = self._pq[0]
+          except IndexError: 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()
+
+        assert(request.sentLength)
+        self._rq.popLeft()
+        request.finish()
+        # round again, looking for more to do
 
 def process_cfg():
   global network