chiark / gitweb /
wip, before optional args log_xxx
[hippotat.git] / hippotat / __init__.py
index 8694e97..abc2a9f 100644 (file)
@@ -18,11 +18,23 @@ from configparser import ConfigParser
 from configparser import NoOptionError
 
 import collections
+import time
 
 import re as regexp
 
+from twisted.python.constants import NamedConstant
+
 import hippotat.slip as slip
 
+class DBG(twisted.python.constants.Names):
+  ROUTE = NamedConstant()
+  FLOW = NamedConstant()
+  HTTP = NamedConstant()
+  HTTP_CTRL = NamedConstant()
+  INIT = NamedConstant()
+  QUEUE = NamedConstant()
+  QUEUE_CTRL = NamedConstant()
+
 defcfg = '''
 [DEFAULT]
 #[<client>] overrides
@@ -33,6 +45,7 @@ target_requests_outstanding = 3  # must match; subject to [limits] on server
 max_requests_outstanding = 4     # used by client
 max_batch_up = 4000              # used by client
 http_timeout = 30                # used by client
+http_retry = 5                   # used by client
 
 #[server] or [<client>] overrides
 ipif = userv root ipif %(local)s,%(peer)s,%(mtu)s,slip %(rnets)s
@@ -166,7 +179,8 @@ def start_ipif(command, router):
   ipif = _IpifProcessProtocol(router)
   reactor.spawnProcess(ipif,
                        '/bin/sh',['sh','-xc', command],
-                       childFDs={0:'w', 1:'r', 2:2})
+                       childFDs={0:'w', 1:'r', 2:2},
+                       env=None)
 
 def queue_inbound(packet):
   ipif.transport.write(slip.delimiter)
@@ -176,20 +190,26 @@ def queue_inbound(packet):
 #---------- packet queue ----------
 
 class PacketQueue():
-  def __init__(self, max_queue_time):
+  def __init__(self, desc, max_queue_time):
+    self._desc = desc
     self._max_queue_time = max_queue_time
     self._pq = collections.deque() # packets
 
+  def _log_debug(self, fn, pri, msg)
+    log_debug(pri, 
+
   def append(self, packet):
+    log_data(DBG.QUEUE, packet, 'pq %s: append' % self._desc)
     self._pq.append((time.monotonic(), packet))
 
   def nonempty(self):
+    log_debug(DBG.QUEUE, 'pq %s: nonempty ?' % self._desc)
     while True:
       try: (queuetime, packet) = self._pq[0]
       except IndexError: return False
 
       age = time.monotonic() - queuetime
-      if age > self.max_queue_time:
+      if age > self._max_queue_time:
         # strip old packets off the front
         self._pq.popleft()
         continue
@@ -212,7 +232,7 @@ class PacketQueue():
         moredata(slip.delimiter)
 
       moredata(encoded)
-      self._pq.popLeft()
+      self._pq.popleft()
 
 #---------- error handling ----------
 
@@ -263,17 +283,17 @@ class ServerAddr():
     try:
       self.addr = ipaddress.IPv4Address(addrspec)
       self._endpointfactory = twisted.internet.endpoints.TCP4ServerEndpoint
-      self._inurl = '%s'
+      self._inurl = b'%s'
     except AddressValueError:
       self.addr = ipaddress.IPv6Address(addrspec)
       self._endpointfactory = twisted.internet.endpoints.TCP6ServerEndpoint
-      self._inurl = '[%s]'
+      self._inurl = b'[%s]'
   def make_endpoint(self):
     return self._endpointfactory(reactor, self.port, self.addr)
   def url(self):
-    url = 'http://' + (self._inurl % self.addr)
-    if self.port != 80: url += ':%d' % self.port
-    url += '/'
+    url = b'http://' + (self._inurl % str(self.addr).encode('ascii'))
+    if self.port != 80: url += b':%d' % self.port
+    url += b'/'
     return url
     
 def process_cfg_saddrs():
@@ -291,6 +311,7 @@ def process_cfg_clients(constructor):
     if not (':' in cs or '.' in cs): continue
     ci = ipaddr(cs)
     pw = cfg.get(cs, 'password')
+    pw = pw.encode('utf-8')
     constructor(ci,cs,pw)
 
 #---------- startup ----------