chiark / gitweb /
init debug
[hippotat.git] / server
diff --git a/server b/server
index ab92be0..3781d56 100755 (executable)
--- a/server
+++ b/server
@@ -12,8 +12,6 @@ from twisted.web.server import NOT_DONE_YET
 
 import syslog
 
-import traceback
-
 clients = { }
 
 #---------- "router" ----------
@@ -49,13 +47,12 @@ class Client():
     # plus from config:
     #  .max_batch_down
     #  .max_queue_time
-    #  .max_request_time
     #  .target_requests_outstanding
 
     if ip not in c.network:
       raise ValueError('client %s not in network' % ip)
 
-    for k in ('max_batch_down','max_queue_time','max_request_time',
+    for k in ('max_batch_down','max_queue_time','http_timeout',
               'target_requests_outstanding'):
       req = cfg.getint(cs, k)
       limit = cfg.getint('limits',k)
@@ -73,7 +70,7 @@ class Client():
     log_debug(dflag, ('client %s: ' % self._ip)+msg, **kwargs)
 
   def process_arriving_data(self, d):
-    self._log(DBG.FLOW, 'req data', d=d)
+    self._log(DBG.FLOW, "req data (enc'd)", d=d)
     if not len(d): return
     for packet in slip.decode(d):
       (saddr, daddr) = packet_addrs(packet)
@@ -95,11 +92,15 @@ class Client():
 
   def new_request(self, request):
     request.setHeader('Content-Type','application/octet-stream')
-    reactor.callLater(self.max_request_time, self._req_cancel, request)
+    reactor.callLater(self.http_timeout, self._req_cancel, request)
     request.notifyFinish().addErrback(self._req_error, request)
     self._rq.append(request)
     self._check_outbound()
 
+  def _req_write(self, req, d):
+    self._log(DBG.HTTP, 'req_write ', idof=req, d=d)
+    req.write(d)
+
   def _check_outbound(self):
     log_debug(DBG.HTTP_CTRL, 'CHKO')
     while True:
@@ -123,7 +124,7 @@ class Client():
       self._log(DBG.HTTP_CTRL, 'CHKO processing', idof=request)
       # request, and also some non-expired packets
       self._pq.process((lambda: request.sentLength),
-                       request.write,
+                       (lambda d: self._req_write(request, d)),
                        self.max_batch_down)
 
       assert(request.sentLength)
@@ -141,11 +142,11 @@ def process_request(request, desca):
   # find client, update config, etc.
   metadata = request.args[b'm'][0]
   metadata = metadata.split(b'\r\n')
-  (ci_s, pw, tro) = metadata[0:3]
-  desca['m[0,2]'] = [ci_s, tro]
+  (ci_s, pw, tro, cto) = metadata[0:4]
+  desca['m[0,2:3]'] = [ci_s, tro, cto]
   ci_s = ci_s.decode('utf-8')
-  tro = int(tro)
-  desca['tro']= tro
+  tro = int(tro); desca['tro']= tro
+  cto = int(cto); desca['cto']= cto
   ci = ipaddr(ci_s)
   desca['ci'] = ci
   cl = clients[ci]
@@ -155,21 +156,31 @@ def process_request(request, desca):
   if tro != cl.target_requests_outstanding:
     raise ValueError('tro must be %d' % cl.target_requests_outstanding)
 
+  if cto < cl.http_timeout:
+    raise ValueError('cto must be >= %d' % cl.http_timeout)
+
   try:
-    d = request.args[b'd']
+    d = request.args[b'd'][0]
     desca['d'] = d
-  except KeyError: d = b''
+    desca['dlen'] = len(d)
+  except KeyError:
+    d = b''
+    desca['dlen'] = None
+
+  log_http(desca, 'processing', idof=id(request), d=d)
+
+  d = mime_translate(d)
 
   cl.process_arriving_data(d)
   cl.new_request(request)
 
-def log_http(desca, msg):
+def log_http(desca, msg, **kwargs):
   try:
-    d = desca['d']
+    kwargs['d'] = desca['d']
     del desca['d']
   except KeyError:
-    d = None
-  log_debug(DBG.HTTP, msg + repr(desca), d=d)
+    pass
+  log_debug(DBG.HTTP, msg + repr(desca), **kwargs)
 
 class IphttpResource(twisted.web.resource.Resource):
   isLeaf = True
@@ -181,11 +192,11 @@ class IphttpResource(twisted.web.resource.Resource):
     try: process_request(request, desca)
     except Exception as e:
       emsg = traceback.format_exc()
-      log_http(desca, 'EXCEPTION ' + emsg)
+      log_http(desca, 'RETURNING EXCEPTION ' + emsg)
       request.setHeader('Content-Type','text/plain; charset="utf-8"')
       request.setResponseCode(400)
       return (emsg + ' # ' + repr(desca) + '\r\n').encode('utf-8')
-    log_http(desca, '... [%s]' % id(request))
+    log_debug(DBG.HTTP_CTRL, '...', idof=id(request))
     return NOT_DONE_YET
 
   def render_GET(self, request):
@@ -225,6 +236,6 @@ def process_cfg():
 
 common_startup()
 process_cfg()
-start_ipif(c.ipif_command, route)
+start_ipif(c.ipif_command, (lambda p,s,d: route(p,"[ipif]",s,d)))
 start_http()
 common_run()