Project

General

Profile

Revision 261

updated tooltron server to hande being offset from cardbox (implicit ACKS) and ot handle ctrl-c presses

View differences:

tooltron.py
126 126
          while not self.stop_thread:
127 127
             # hang until we either get keyboard input or are told to stop
128 128
             [active,a,b] = select.select([sys.stdin,self.rpipe],[],[])
129

  
129 130
             if not self.stop_thread:
130 131
                l = raw_input()
132

  
131 133
                cv.acquire()
132 134
                try:
133 135
                   id = re.search('%([0-9]*)=.*', l)
......
146 148
           cv.acquire()
147 149
           idready = True
148 150
           lastid=None
151
           threadDoneExcp=ctrlDException("thread exiting cleanly")
149 152
           cv.notify()
150 153
           cv.release()
151
           threadDoneExcp=ctrlDException("thread exiting cleanly")
152 154
       except Exception, e:
153 155
          threadDoneExcp = e #TODO: provide a line number or something?
154 156
       finally:
......
215 217
          raise threadDoneExcp
216 218

  
217 219
       print ">>> wait"
218
       cv.wait()
220
       try:
221
          cv.wait()
222
       # if the user hits ctrl-c, cleanly exit
223
       except KeyboardInterrupt:
224
          print "cv.wait() got a keyboard interupt!"
225
          cv.release()
226
          return None
219 227

  
220 228
    print ">>> idready set"
221 229

  
......
278 286
         if id != None:
279 287
            print "\--------------------\n",strftime("%Y.%m.%d %H:%M:%S \n"),"id# ", id.group(1)
280 288

  
281
            # If we can't get through to the cardbox, restart the loop
282
            if common.sendKeyRequest() == False:
283
               print "ERROR: timeout waiting for key ack"
284
               logMessage("ERROR: exceeded max retries waiting for cardbox")
289

  
290
            # Send a key request
291
            common.sendMessage(2, common.TT_GET_KEY, "")
292
            
293
            # listen for an ACK or a key response
294
            msg = common.readMessage()
295
            if msg == None:
296
               print "ERROR: Timeout waiting for response from key request!"
285 297
               continue
298

  
299
            # resp hols the key response, so set it to None since we don't have one yet
300
            resp = None
301

  
302
            [src,dest,cmd,data] = msg
303
            #TODO: no magic number '2' for cardbox or '1' for server
304
            if src == 2 and dest == 1:
305
               if cmd == common.TT_ACK:
306
                  print "got ACK from cardbox"
307
               elif cmd == common.TT_SEND_KEY:
308
                  print "got key response instead of ACK from cardbox"
309
                  print "using key: ", data
310
                  resp = data[0]
311
               else:
312
                  print "ERROR: got some other packet type: ", cmd, " (data = {", data, "})"
313
                  continue
314
            else:
315
               print "ERROR: got confusing packet. Excpecting something from cardbox (2) but got:"
316
               common.printMsg(msg)
317
               continue
318

  
319
            # start the timer so we can timeout if we are still waiting for a key response
286 320
            startTime = time.time()
287 321

  
322
            # while we are (or might be) waiting for a key response, do the MySQL stuff
288 323
            if cursor != None:
289 324
               cursor.close()
290 325

  
......
343 378

  
344 379
            print user
345 380

  
346

  
347
            resp = None
381
            # while we don't have a valid response
382
            # Note that this might already be set above when we were heoping for an ACK
348 383
            while resp==None:
349 384
               try:
350 385
                  resp = common.readKey()
......
361 396
                  common.sendMessage(2,common.TT_TIMEOUT,'')
362 397
                  break
363 398

  
364
               #if we have a valid response
365
               #sometimes the FTDI chip seems to give a zero as a string when power is reset
366
               if resp != None and ord(resp) != 0 and resp != common.TT_TIMEOUT:
399
            #if we have a valid response
400
            #sometimes the FTDI chip seems to give a zero as a string when power is reset
401
            if resp != None and ord(resp) != 0 and resp != common.TT_TIMEOUT:
367 402

  
368
                  toolName = reverseTool(resp)
369
                  toolNameLong = toolName + " (k="+str(resp)+"("+str(ord(resp))+"), t="+str(keypad2toolID(resp))+")"
370
                  print "request:",resp,"(",ord(resp),") ",toolNameLong
403
               toolName = reverseTool(resp)
404
               toolNameLong = toolName + " (k="+str(resp)+"("+str(ord(resp))+"), t="+str(keypad2toolID(resp))+")"
405
               print "request:",resp,"(",ord(resp),") ",toolNameLong
371 406

  
372
                  if acl.count(resp) > 0:
373
                     common.sendAck(2)
374
                     time.sleep(0.5)
375
                     sendTool(resp)
376
                     print "ACCESS GRANTED"
377
                     logMessage(user+" ACCESSED tool "+toolNameLong)
378
                     if common.checkAck(resp):
379
                        print "ACK"
380
                        toolFails[toolName] = 0
381
                     else:
382
                        print "NO ACK!!!!"
383
                        logMessage("tool "+toolNameLong+" did not ACK")
384
                        toolFails[toolName] += 1
385
                        if toolFails[toolName] > common.MAX_TOOL_FAILS:
386
                           #TODO: send email
387
                           logMessage("WARNING: tool "+toolNameLong+
388
                                      " has failed to ACK "+
389
                                      str(common.MAX_TOOL_FAILS)+" times in a row.")
390

  
407
               if acl.count(resp) > 0:
408
                  common.sendAck(2)
409
                  time.sleep(0.5)
410
                  sendTool(resp)
411
                  print "ACCESS GRANTED"
412
                  logMessage(user+" ACCESSED tool "+toolNameLong)
413
                  if common.checkAck(resp):
414
                     print "ACK"
415
                     toolFails[toolName] = 0
391 416
                  else:
392
                     common.sendNack(2)
393
                     print "ACCESS DENIED!!"
394
                     logMessage(user + " DENIED on tool "+toolNameLong)
417
                     print "NO ACK!!!!"
418
                     logMessage("tool "+toolNameLong+" did not ACK")
419
                     toolFails[toolName] += 1
420
                     if toolFails[toolName] > common.MAX_TOOL_FAILS:
421
                        #TODO: send email
422
                        logMessage("WARNING: tool "+toolNameLong+
423
                                   " has failed to ACK "+
424
                                   str(common.MAX_TOOL_FAILS)+" times in a row.")
395 425

  
396
                  clear_id()
426
               else:
427
                  common.sendNack(2)
428
                  print "ACCESS DENIED!!"
429
                  logMessage(user + " DENIED on tool "+toolNameLong)
397 430

  
398
               elif resp == common.TT_TIMEOUT:
399
                  print "TIMEOUT sent from keypad"
400
                  continue
401
               else:
402
                  break
431
               clear_id()
432

  
433
            elif resp == common.TT_TIMEOUT:
434
               print "TIMEOUT sent from keypad"
435
               continue
436
            else:
437
               break
403 438
   except ctrlDException:
404 439
      print "got a ctrl-D, exiting cleanly"
405 440
      logMessage("NOTICE: tooltron going down because of ctrl-D press")
441
      
406 442
   finally:
407 443
      if not threadDone:
408 444
         print "stopping id reader thread..."

Also available in: Unified diff