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

diff --git a/server b/server
index 46ce5ed..8501373 100755 (executable)
--- a/server
+++ b/server
@@ -8,6 +8,8 @@ from twisted.internet import reactor
 import ConfigParser
 import ipaddress
 
+import collections
+
 import syslog
 
 clients = { }
@@ -42,9 +44,9 @@ def route(packet. daddr):
   try: client = clients[daddr]
   except KeyError: dclient = None
   if dclient is not None:
-    dclient.queue_outbound_data(packet)
+    dclient.queue_outbound(packet)
   else if daddr = server or daddr not in network:
-    queue_inbound_data(packet)
+    queue_inbound(packet)
   else:
     syslog.syslog(syslog.LOG_DEBUG, 'no client for %s' % daddr)
 
@@ -54,6 +56,8 @@ class Client():
     self._ip = ip
     self._cs = cs
     self.pw = cfg.get(cs, 'password')
+    self._rq = collections.deque() # requests
+    self._pq = collections.deque() # packets
     # plus from config:
     #  .max_batch_down
     #  .max_queue_time
@@ -76,10 +80,31 @@ class Client():
     def _req_error(self, err, request):
       self._req_cancel(request)
 
+    def queue_outbound(self, packet):
+      self._pq.append((time.time, packet))
+
     def http_request(self, request):
       request.setHeader('Content-Type','application/octet-stream')
       reactor.callLater(self.max_request_time, self._req_cancel, request)
       request.notifyFinish().addErrback(self._req_error, request)
+      self._rq.append(request)
+      self._check_outbound()
+
+    def _check_outbound(self):
+      while True:
+        try: request = self._rq[0]
+        except IndexError: request = None
+        if request and request.finished:
+          self._rq.popleft()
+          continue
+
+        # now request is an unfinished request, or None
+        try: (queuetime, packet) = self._pq[0]
+        except: IndexError:
+          # no packets, oh well
+          continue
+
+        age = time.time() - queuetime
 
 def process_cfg():
   global network