test_vsc.py

Go to the documentation of this file.
00001 #
00002 #                               Copyright 2005
00003 #                                     by
00004 #                        The Board of Trustees of the
00005 #                     Leland Stanford Junior University.
00006 #                            All rights reserved.
00007 #
00008 
00009 __facility__ = "Online"
00010 __abstract__ = "Simple VSC simulator used for testing tele-commands"
00011 __author__   = "Selim Tuvi <stuvi@slac.stanford.edu> SLAC - GLAST I&T/Online"
00012 __date__     = "2005/07/23 00:08:27"
00013 __updated__  = "$Date: 2006/04/27 18:22:56 $"
00014 __version__  = "$Revision: 1.6 $"
00015 __release__  = "$Name: HEAD $"
00016 __credits__  = "SLAC"
00017 
00018 import LICOS.copyright_SLAC
00019 
00020 from LICOS.tools.proxy.VscProxyPorts import VscProxyPorts
00021 from LICOS.util.gOptions             import Options
00022 
00023 import socket
00024 import types
00025 import threading
00026 from   SocketServer import ThreadingTCPServer, StreamRequestHandler
00027 import errno
00028 import select
00029 from  time import sleep
00030 from binascii import hexlify
00031 from LCAT_decoder import *
00032 
00033 VSC_CTL_PORT = 3000
00034 VSC_CMD_PORT = 3001
00035 VSC_TLM_PORT = 3002
00036 LAT_TLM_PORT = 3003
00037 LAT_SCI_PORT = 4001
00038 
00039 def startListenerThread(name, port):
00040   """!\brief Start the VSC listener thread.
00041 
00042   \param port The port that the listener will listen on
00043   """
00044   tcpserver = VscSocketReceiver(port=port)
00045   runTCPThread = threading.Thread(target=runTCP, name=name, args=(tcpserver,))
00046   runTCPThread.start()
00047   return runTCPThread
00048 
00049 def runTCP(tcpserver):
00050   """!\brief Start the listener loop.
00051 
00052   """
00053   tcpserver.serve_until_stopped()
00054 
00055 class VscSocketHandler(StreamRequestHandler):
00056   """!\brief VSC packet handler.
00057 
00058   """
00059   def handle(self):
00060     """!\brief Handle the incoming packet.
00061 
00062     """
00063     while 1:
00064       try:
00065         ccsds = self.connection.recv(1024)
00066         if len(ccsds) == 0:
00067           break
00068         else:
00069           print "ccsds packet  = %s" % hexlify(ccsds)
00070           #ccsds = ccsdsPacket(ccsds)
00071           #print "header        = 0x%08x" % ccsds.header.ui
00072           #print "  apid        = 0x%x" % ccsds.header.apid
00073           #print "  secHead     = 0x%x" % ccsds.header.secHead
00074           #print "  type        = 0x%x" % ccsds.header.type
00075           #print "  version     = 0x%x" % ccsds.header.version
00076           #print "  seqCnt      = 0x%x" % ccsds.header.seqCnt
00077           #print "  seqFlg      = 0x%x" % ccsds.header.seqFlg
00078           #print "length        = 0x%x" % ccsds.length
00079           #print "seconds       = 0x%x" % ccsds.seconds
00080           #print "subSeconds    = 0x%x" % ccsds.subSeconds
00081           #print "payload       = %s" % hexlify(ccsds.payload)
00082           #print "-"*80
00083       except socket.error, e:
00084         if type(e.args) != types.TupleType:
00085           raise e
00086         else:
00087           errcode = e.args[0]
00088           if errcode != errno.ECONNRESET:
00089             raise e
00090           break
00091 
00092 
00093 class VscSocketReceiver(ThreadingTCPServer):
00094   """!\brief VSC socket receiver.
00095 
00096   """
00097   allow_reuse_address = 1
00098 
00099   def __init__(self, host='', port=None,
00100       handler=VscSocketHandler):
00101     """!\brief VscSocketReceiver constructor.
00102 
00103     \param host    Host name
00104     \param port    Listen port
00105     \param handler Packet handler
00106     """
00107     ThreadingTCPServer.__init__(self, (host, port), handler)
00108     self.__abort = 0
00109     self.timeout = 1
00110     self.handler = handler
00111 
00112   def abort(self):
00113     """!\brief Signal the abort.
00114 
00115     """
00116     self.__abort = 1
00117 
00118   def serve_until_stopped(self):
00119     """!\brief Main listener loop.
00120 
00121     """
00122     while not self.__abort:
00123       rd, wr, ex = select.select([self.socket.fileno()],
00124                                  [], [],
00125                                  self.timeout)
00126       if rd:
00127         self.handle_request()
00128 
00129 def usage():
00130   return """
00131 
00132 test_vsc usage:
00133 
00134 Optional options:
00135 --portbase    specify the port base
00136 
00137 Example:
00138 $ test_vsc --portbase 3000
00139 
00140 """
00141 
00142 if __name__ == "__main__":
00143   options = Options([],['portbase'])
00144   try:
00145     options.parse()
00146   except Exception, msg:
00147     options.usage(usage())
00148     raise Exception, msg
00149 
00150   if options.portbase is None:
00151     portbase = 3000
00152   else:
00153     portbase = int(options.portbase)
00154   ports = VscProxyPorts(portbase)
00155     
00156   ctlThread       = startListenerThread('VscControl',      ports.vsc())
00157   diagThread      = startListenerThread('VscDiagnostic',   ports.diagnostic())
00158   tlmThread       = startListenerThread('VscTelemetry',    ports.telemetry())
00159   #latTlmThread    = startListenerThread('VscLatTelemetry', LAT_TLM_PORT_OFFSET)
00160   #latScicmdThread = startListenerThread('VscLatScience',   LAT_SCI_PORT_OFFSET)
00161   
00162   print "Ready."
00163   while 1:
00164     sleep(1.0)

Generated on Thu Apr 27 20:52:44 2006 for LICOS L02-01-00 by doxygen 1.4.6-NO