Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | Related Pages

ancillarySocket.py

00001 #!/usr/local/bin/python
00002 #
00003 #                               Copyright 2006
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__ = "Read ancillary event contributions from a socket"
00012 __author__   = "R. Claus <Claus@SLAC.Stanford.edu> SLAC - GLAST LAT I&T/Online"
00013 __date__     = "3/30/2006"
00014 __updated__  = ("$Date: 2006/07/14 22:34:28 $").split(' ')[1]
00015 __version__  = "$Revision: 1.1 $"
00016 __release__  = "$Name: R04-12-00 $"
00017 __credits__  = "SLAC"
00018 
00019 import LATTE.copyright_SLAC
00020 
00021 import struct
00022 from   array                              import array
00023 import logging                            as     log
00024 
00025 import LATTE.merger.geb                   as     geb
00026 from   LATTE.merger.ancillaryContribution import AncillaryContribution
00027 from   LATTE.client.gSocket               import Socket
00028 
00029 
00030 class AncillaryContributor(geb.GebContributor):
00031   __typeIds = [AncillaryContribution.HeaderId,
00032                AncillaryContribution.TrailerId,
00033                AncillaryContribution.EventId]
00034   def __init__(self, (host, port), offset = 0):
00035     geb.GebContributor.__init__(self)
00036     self.__socket    = Socket(port)
00037     self.__host      = host
00038     self.__fakeEvent = struct.pack("<3L", AncillaryContribution.EventId, 3*4, 0xdeadbeefL)
00039     self.__offset    = offset
00040 
00041   def name(self):
00042     return "Anc"
00043 
00044   def offset(self):
00045     return self.__offset
00046 
00047   def open(self):
00048     self.__socket.connect(self.__host)
00049     self.__socket.settimeout(9999)
00050     print "AncContrib.open: socket opened"
00051 
00052   def close(self):
00053     self.__socket.disconnect()
00054 
00055   def read(self):
00056     """!\brief Method to read an ancillary contribution from a file.
00057     """
00058     data   = self.__socket.recvall(8)
00059     (vTypeId, length) = struct.unpack('<2L', data)
00060     typeId = vTypeId & 0x000fffff
00061     if typeId not in AncillaryContributor.__typeIds:
00062       # Don't try to continue if bad typeId: we're lost or it's undefined
00063       raise KeyError, "Unrecognized contribution %08x"%(typeId)
00064     length &= 0x0fffffff
00065     #print "AncContrib.read:   id = %08x, len = %08x" % (typeId,length)
00066     data  += self.__socket.recvall(length - 8)
00067     data = array('I', data)
00068     data.byteswap()
00069     data = data.tostring()
00070 
00071     status = 0
00072     if typeId == AncillaryContribution.EventId:
00073       return geb.Geb.Build,     status, AncillaryContribution(self, data)
00074     else:
00075       return geb.Geb.DontBuild, status, AncillaryContribution(self, data)
00076 
00077   def fake(self):
00078     return self.__fakeEvent

Generated on Fri Jul 21 13:26:26 2006 for LATTE R04-12-00 by doxygen 1.4.3