chiark / gitweb /
wip debug
[hippotat.git] / hippotat / __init__.py
index ae13eece753bb87b17131c48a32f3173aabe1ef8..e13b31f642429f443da389eb7095e350155d0baa 100644 (file)
@@ -20,6 +20,8 @@ from optparse import OptionParser
 from configparser import ConfigParser
 from configparser import NoOptionError
 
 from configparser import ConfigParser
 from configparser import NoOptionError
 
+from functools import partial
+
 import collections
 import time
 import codecs
 import collections
 import time
 import codecs
@@ -30,15 +32,17 @@ import re as regexp
 import hippotat.slip as slip
 
 class DBG(twisted.python.constants.Names):
 import hippotat.slip as slip
 
 class DBG(twisted.python.constants.Names):
+  INIT = NamedConstant()
   ROUTE = NamedConstant()
   DROP = NamedConstant()
   FLOW = NamedConstant()
   HTTP = NamedConstant()
   ROUTE = NamedConstant()
   DROP = NamedConstant()
   FLOW = NamedConstant()
   HTTP = NamedConstant()
-  HTTP_CTRL = NamedConstant()
-  INIT = NamedConstant()
+  TWISTED = NamedConstant()
   QUEUE = NamedConstant()
   QUEUE = NamedConstant()
+  HTTP_CTRL = NamedConstant()
   QUEUE_CTRL = NamedConstant()
   HTTP_FULL = NamedConstant()
   QUEUE_CTRL = NamedConstant()
   HTTP_FULL = NamedConstant()
+  CTRL_DUMP = NamedConstant()
   SLIP_FULL = NamedConstant()
 
 _hex_codec = codecs.getencoder('hex_codec')
   SLIP_FULL = NamedConstant()
 
 _hex_codec = codecs.getencoder('hex_codec')
@@ -46,9 +50,10 @@ _hex_codec = codecs.getencoder('hex_codec')
 log = twisted.logger.Logger()
 
 def log_debug(dflag, msg, idof=None, d=None):
 log = twisted.logger.Logger()
 
 def log_debug(dflag, msg, idof=None, d=None):
+  if dflag > DBG.HTTP: return
   #print('---------------->',repr((dflag, msg, idof, d)), file=sys.stderr)
   if idof is not None:
   #print('---------------->',repr((dflag, msg, idof, d)), file=sys.stderr)
   if idof is not None:
-    msg = '[%d] %s' % (id(idof), msg)
+    msg = '[%#x] %s' % (id(idof), msg)
   if d is not None:
     #d = d[0:64]
     d = _hex_codec(d)[0].decode('ascii')
   if d is not None:
     #d = d[0:64]
     d = _hex_codec(d)[0].decode('ascii')
@@ -60,11 +65,11 @@ defcfg = '''
 #[<client>] overrides
 max_batch_down = 65536           # used by server, subject to [limits]
 max_queue_time = 10              # used by server, subject to [limits]
 #[<client>] overrides
 max_batch_down = 65536           # used by server, subject to [limits]
 max_queue_time = 10              # used by server, subject to [limits]
-max_request_time = 54            # used by server, subject to [limits]
 target_requests_outstanding = 3  # must match; subject to [limits] on server
 target_requests_outstanding = 3  # must match; subject to [limits] on server
+http_timeout = 30                # used by both } must be
+http_timeout_grace = 5           # used by both }  compatible
 max_requests_outstanding = 4     # used by client
 max_batch_up = 4000              # used by client
 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
 http_retry = 5                   # used by client
 
 #[server] or [<client>] overrides
@@ -93,7 +98,7 @@ port = 80                  # used by server
 [limits]
 max_batch_down = 262144           # used by server
 max_queue_time = 121              # used by server
 [limits]
 max_batch_down = 262144           # used by server
 max_queue_time = 121              # used by server
-max_request_time = 121            # used by server
+http_timeout = 121                # used by server
 target_requests_outstanding = 10  # used by server
 '''
 
 target_requests_outstanding = 10  # used by server
 '''
 
@@ -164,25 +169,24 @@ class SlipStreamDecoder():
     self._log('__init__')
 
   def _log(self, msg, **kwargs):
     self._log('__init__')
 
   def _log(self, msg, **kwargs):
-    log_debug(DBG.SLIP_FULL, 'slip '+msg, **kwargs)
+    log_debug(DBG.SLIP_FULL, 'slip %s: %s' % (self._desc, msg), **kwargs)
 
   def inputdata(self, data):
     self._log('inputdata', d=data)
 
   def inputdata(self, data):
     self._log('inputdata', d=data)
-    data = self._buffer + data
-    self._buffer = b''
     packets = slip.decode(data)
     packets = slip.decode(data)
+    packets[0] = self._buffer + packets[0]
     self._buffer = packets.pop()
     for packet in packets:
       self._maybe_packet(packet)
     self._buffer = packets.pop()
     for packet in packets:
       self._maybe_packet(packet)
-    self._log('inputdata bufremain', d=self._buffer)
+    self._log('bufremain', d=self._buffer)
 
   def _maybe_packet(self, packet):
 
   def _maybe_packet(self, packet):
-    self._log('inputdata maybepacket', d=packet)
+    self._log('maybepacket', d=packet)
     if len(packet):
       self._on_packet(packet)
 
   def flush(self):
     if len(packet):
       self._on_packet(packet)
 
   def flush(self):
-    self._log('inputdata flush')
+    self._log('flush')
     self._maybe_packet(self._buffer)
     self._buffer = b''
 
     self._maybe_packet(self._buffer)
     self._buffer = b''
 
@@ -285,7 +289,8 @@ _crashing = False
 def crash(err):
   global _crashing
   _crashing = True
 def crash(err):
   global _crashing
   _crashing = True
-  print('CRASH ', err, file=sys.stderr)
+  print('========== CRASH ==========', err,
+        '===========================', file=sys.stderr)
   try: reactor.stop()
   except twisted.internet.error.ReactorNotRunning: pass
 
   try: reactor.stop()
   except twisted.internet.error.ReactorNotRunning: pass
 
@@ -366,7 +371,12 @@ def process_cfg_clients(constructor):
 
 def common_startup():
   log_formatter = twisted.logger.formatEventAsClassicLogText
 
 def common_startup():
   log_formatter = twisted.logger.formatEventAsClassicLogText
-  log_observer = twisted.logger.FileLogObserver(sys.stderr, log_formatter)
+  stdout_obs = twisted.logger.FileLogObserver(sys.stdout, log_formatter)
+  stderr_obs = twisted.logger.FileLogObserver(sys.stderr, log_formatter)
+  pred = twisted.logger.LogLevelFilterPredicate(LogLevel.error)
+  log_observer = twisted.logger.FilteringLogObserver(
+    stderr_obs, [pred], stdout_obs
+  )
   twisted.logger.globalLogBeginner.beginLoggingTo(
     [ log_observer, crash_on_critical ]
     )
   twisted.logger.globalLogBeginner.beginLoggingTo(
     [ log_observer, crash_on_critical ]
     )