SocketCVT.py

Go to the documentation of this file.
00001 #!/usr/local/bin/python
00002 #
00003 #                               Copyright 2004
00004 #                                     by
00005 #                        The Board of Trustees of the
00006 #                     Leland Stanford Junior University.
00007 #                            All rights reserved.
00008 #
00009 
00010 __facility__ = "Online"
00011 __abstract__ = "Current Value Table Server"
00012 __author__  = "Jim Panetta <panetta@slac.stanford.edu> SLAC - GLAST LAT I&T/Online"
00013 __date__     = ("$Date: 2005/11/30 18:44:22 $").split(' ')[1]
00014 __version__ = "$Revision: 1.5 $"
00015 __release__  = "$Name: HEAD $"
00016 __credits__ = "SLAC"
00017 
00018 import LICOS.copyright_SLAC
00019 
00020 import struct, time
00021 import asyncore, threading, socket
00022 
00023 
00024 from LICOS.lib.currValTable.CurrValServer         import CurrentValueFactory
00025 from LICOS.lib.currValTable.CurrValTable          import CurrentValueTable
00026 from LICOS.lib.currValTable.StringTcpProtocol     import Int16StringProtocol
00027 import LICOS.util.gOptions as gOptions
00028 from LICOS.lib.cmdTlmDb.LCATtlmDb           import ccsdsTlmHdr, LICOS_VscTlmPacketFactory
00029 
00030 
00031 killEvent = threading.Event()
00032 killEvent.clear()
00033 
00034 def asyncore_loop(timeout=1.0, use_poll=False, map=None):
00035   """replace the asyncore central loop and run it its own thread.
00036      Valid for Python 2.3.x
00037      Python 2.4 has an extra agument for loop, count.
00038   """
00039   if map is None:
00040       map = asyncore.socket_map
00041 
00042   if use_poll:
00043     if hasattr(select, 'poll'):
00044       poll_fun = asyncore.poll3
00045     else:
00046       poll_fun = asyncore.poll2
00047   else:
00048     poll_fun = asyncore.poll
00049 
00050   while map and not killEvent.isSet():
00051     poll_fun(timeout, map)
00052 
00053 class SocketCVTClient(Int16StringProtocol):
00054   def __init__(self, cvt, host, port):
00055     super(SocketCVTClient, self).__init__()
00056 
00057     # build the main connection socket
00058     self.create_socket(socket.AF_INET,socket.SOCK_STREAM)
00059 
00060     self.__host = host
00061     self.__port = port
00062     self.__cvt  = cvt
00063     self.__connect()
00064 
00065   def __connect(self):
00066     self.connect( (self.__host, self.__port) )
00067     time.sleep(0.05)
00068     self.send("ok CCSDS\n")
00069 
00070   def handle_connect(self):
00071     # print "handle_connect"
00072     pass
00073 
00074   def handle_close(self):
00075     # Try an exponential reconnect?  ****REVISIT****
00076     self.close()
00077 
00078   def stringReceived(self, line):
00079     self.__cvt.dispatch(line)
00080 
00081 class ccsdsCVT(CurrentValueTable):
00082   def dispatch(self, packet):
00083     apIdPkt = LICOS_TlmPacketFactory(packet)
00084     if apIdPkt is None:
00085       hdr = ccsdsTlmHdr(struct.unpack('!H', packet[0:2])[0])
00086       print "ccsdsCVT received apID %d = 0x%03x which is unrecognized as telemetry" % (hdr.apid, hdr.apid)
00087 
00088     aTime = apIdPkt.secs + apIdPkt.usecs/1E6
00089     # print apIdPkt, aTime
00090 
00091     apIdPkt.decode_payload()
00092     for mnem in apIdPkt.get_payload_list():
00093       # print mnem, apIdPkt.get_payload(mnem)
00094       self.setValue(mnem, apIdPkt.get_payload(mnem), aTime)
00095     # add in the packet as well
00096     self.setValue(apIdPkt.name.upper(), packet, aTime)
00097 
00098     self.setValue("LASTPACKET", apIdPkt.name.upper(), aTime)
00099 
00100 if __name__ == '__main__':
00101   options = gOptions.Options(['pktHost', 'pktPort', 'cvtPort'])
00102   try:
00103     options.parse()
00104   except Exception, msg:
00105     options.usage(str(msg))
00106     exit
00107 
00108   pktHost = options.pktHost
00109   pktPort = int(options.pktPort)
00110   cvtPort = int(options.cvtPort)
00111 
00112   cvt = ccsdsCVT()
00113   cvc = SocketCVTClient(cvt, pktHost, pktPort)
00114   cvs = CurrentValueFactory(cvtPort, cvt)
00115 
00116   asyncore_loop(0.5)
00117 
00118   killEvent.set()
00119 
00120 
00121 
00122 
00123 
00124 
00125 #eof

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