StringTcpProtocol.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/09/27 21:43:26 $").split(' ')[1]
00014 __version__ = "$Revision: 1.3 $"
00015 __release__  = "$Name: HEAD $"
00016 __credits__ = "SLAC"
00017 
00018 import LICOS.copyright_SLAC
00019 
00020 import struct, threading, asyncore
00021 # from binascii import hexlify
00022 
00023 # import LICOS.util.gOptions as gOptions
00024 
00025 
00026 class Int16StringProtocol(asyncore.dispatcher, object):
00027   """Defines a protocol where the messages passed are of the format
00028      "XX<string>" where <string> is an arbitrary string and XX the
00029      length of this string expressed as a ushort.  The max length of
00030      <string> is 0xffff bytes.  Locks are used to prevent buffer overwrites.
00031   """
00032   MAXSTRINGLEN  = 0xffff
00033   def __init__(self, port=None):
00034     super(Int16StringProtocol, self).__init__(port)
00035     
00036     self.__readBuffer = ''
00037     self.__writeBuffer = ''
00038     self.__writeLock = threading.Lock()
00039   
00040   def readable(self):
00041     return True
00042     
00043   def writable(self):
00044     self.__writeLock.acquire()
00045     retVal = len(self.__writeBuffer) > 0
00046     self.__writeLock.release()
00047     return retVal
00048     
00049   def handle_read(self):
00050     """ Read the socket and parse into messages.
00051     """
00052     self.__readBuffer += self.recv(8192)
00053     # print "handle_read", len(self.__readBuffer)
00054     while len(self.__readBuffer) > 1:
00055       length = (ord(self.__readBuffer[0])*0x100) + ord(self.__readBuffer[1])
00056       if len(self.__readBuffer) < length+2:
00057         break
00058       dataString = self.__readBuffer[2:length+2]
00059       self.__readBuffer = self.__readBuffer[length+2:]
00060       # print "dispatch string", len(dataString)
00061       self.stringReceived(dataString)
00062   
00063   def handle_write(self):
00064     self.__writeLock.acquire()
00065     sent = self.send(self.__writeBuffer)
00066     self.__writeBuffer = self.__writeBuffer[sent:]
00067     self.__writeLock.release()  
00068   
00069   def stringReceived(self, line):
00070     """!\brief Receive a string from the socket
00071     
00072     \param line  The string from the socket
00073     """
00074     # print line
00075     raise NotImplementedError, "stringReceived must be overridden in a derived class"
00076   
00077   def sendString(self, data):
00078     """!\brief Send a string through the socket
00079     
00080     \param data  String data to send through the socket.
00081     
00082     If len(data) > 0xffff, ValueError is raised.
00083     """
00084     if len(data) > self.MAXSTRINGLEN:
00085       raise ValueError, "sendString: Max string length %x exceeded.  String length attempted = %x", (self.MAXSTRINGLEN, len(data))
00086     message = struct.pack("!H", len(data)) + data
00087     self.__writeLock.acquire()
00088     self.__writeBuffer += message
00089     self.__writeLock.release()
00090     
00091 
00092 
00093 
00094 if __name__ == '__main__':
00095   pass
00096 
00097 
00098 
00099 
00100 
00101 
00102 
00103 #eof

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