00001
00002
00003
00004
00005
00006
00007
00008
00009 __facility__ = "Online"
00010 __abstract__ = "Writes CCSDS packets to file"
00011 __author__ = "<perazzo@SLAC.Stanford.edu> SLAC - GLAST LAT I&T/Online"
00012 __version__ = "$Revision: 1.16 $"
00013 __credits__ = "SLAC"
00014
00015 import logging as log
00016 import threading
00017 import time
00018 import os
00019 from binascii import hexlify
00020
00021 import Queue
00022
00023 class VscArchiver(object):
00024 MinInterval = 10
00025 PrimaryHeaderSize = 6
00026 CCSDSLengthCorrection = PrimaryHeaderSize + 1
00027 def __init__(self, apIdGrp, path, granularity):
00028 self.__apIdGrp = apIdGrp
00029 self.__path = path
00030
00031 if (granularity < VscArchiver.MinInterval):
00032 log.warning('File creation interval increased: minimum is %d secs'
00033 ' requested was %d secs',
00034 VscArchiver.MinInterval, granularity)
00035 self.__granularity = VscArchiver.MinInterval
00036 else:
00037 self.__granularity = granularity
00038
00039 self.__shutdown = False
00040 self.__queue = Queue.Queue()
00041 self.__archivtask = threading.Thread(name="archive", target=self.__archive)
00042 self.__archivtask.start()
00043
00044 self.__expired = threading.Event()
00045 self.__timertask = threading.Thread(name="timer", target=self.__timer)
00046 self.__timertask.start()
00047
00048 def send(self, packet):
00049 data = packet.header()
00050 datacopy = data[:1] + data[1:packet.length()+self.CCSDSLengthCorrection]
00051 packet.unswap(datacopy)
00052 self.__queue.put(datacopy, block=True)
00053
00054 def send_nothrow(self, packet):
00055
00056 self.send(packet)
00057
00058 def abort(self):
00059 self.__shutdown = True
00060
00061 self.__expired.set()
00062 self.__timertask.join()
00063
00064 self.__queue.put('', block=True)
00065 self.__archivtask.join()
00066
00067 def __timer(self):
00068 timeout = self.__granularity
00069 while True:
00070 self.__expired.wait(timeout)
00071 if self.__expired.isSet():
00072 break
00073
00074 self.__queue.put('', block=False)
00075
00076 def __archive(self):
00077 self.__newfile()
00078 while True:
00079 data = self.__queue.get(block=True)
00080 datalen = len(data)
00081 if datalen > 0:
00082 self.__file.write(data)
00083 else:
00084 if self.__shutdown:
00085 break
00086 else:
00087 self.__close()
00088 self.__newfile()
00089 self.__close()
00090
00091 def __newfile(self):
00092 now = time.time()
00093 t = time.gmtime(now)
00094 name = 'GLAST_%04d%03d_%02d%02d%02d_%s' \
00095 %(t.tm_year, t.tm_yday, t.tm_hour, t.tm_min, t.tm_sec,
00096 self.__apIdGrp)
00097 self.__file = file(os.path.join(self.__path, name), 'w+b', 0)
00098
00099 def __close(self):
00100 name = self.__file.name
00101 self.__file.close()
00102 os.rename(name, '%s.pkt' %(name))
00103