ScriptClient.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 Client"
00012 __author__  = "Jim Panetta <panetta@slac.stanford.edu> SLAC - GLAST LAT I&T/Online"
00013 __date__     = ("$Date: 2006/01/28 05:35:26 $").split(' ')[1]
00014 __version__ = "$Revision: 1.9 $"
00015 __release__  = "$Name: HEAD $"
00016 __credits__ = "SLAC"
00017 
00018 import LICOS.copyright_SLAC
00019 
00020 import LICOS.util.gOptions as gOptions
00021 
00022 
00023 import socket, threading, time, struct
00024 from binascii import hexlify
00025 import cPickle as pickle
00026 
00027 from LICOS.lib.currValTable.CurrValTable import *
00028 
00029 class ScriptClient( socket.socket ):
00030   MAXLENGTH = 0x10000
00031   def __init__( self, serverHost, port ):
00032     socket.socket.__init__( self, socket.AF_INET, socket.SOCK_STREAM )
00033     
00034     self.connect( ( serverHost, port ) )
00035     self.__read()
00036     
00037     self.delimiter  = "\r\n"
00038     
00039   def __read( self ):
00040     # Receive a packet in the fomat "LL......" where LL is a ushort
00041     # designating the size of the string.
00042     resp = self.recv(2)
00043     lbuf = ord(resp[0])*256 + ord(resp[1])
00044     
00045     buf = self.recv(lbuf)
00046     resp = buf[0:3]
00047     pkt  = buf[3:]
00048     
00049     # print lbuf, len(buf)
00050     if resp == 'ok ':
00051       self.connectionState = True
00052       return
00053     if resp == 'rq ':
00054       # a pickled object is sent across the network in the byte format:
00055       # 'rq ...'
00056       # print hexlify(pkt)
00057       # REVISIT:   do something about handling the "no value" problem
00058       cv = decodeCurrentValue(pkt)
00059       self.__reply = cv
00060 
00061   def __sendLine( self, data ):
00062     # Send a packet in the fomat "LL......" where LL is a ushort
00063     # designating the size of the string.
00064     ld = len(data)
00065     if ld >= 0x10000:
00066       raise IOError, "data buffer too long! len(data) = %d" % ld
00067     ldd = divmod(ld,256)
00068     line = chr(ldd[0]) + chr(ldd[1]) + data 
00069     self.send(line)
00070 
00071       
00072   def request(self, mnem):
00073     query = 'request'
00074     query += ' ' + mnem
00075     self.__sendLine(query)
00076     self.__read()
00077     
00078     result = self.__reply
00079     return result
00080 
00081   def poll(self, mnem, timeout=None, maxAge=None):
00082     """!\brief poll member function
00083     
00084     Poll for a mnemonic where the age < maxAge (seconds) with a
00085     specified timeout.  If timeout is None (or 0) respond immediately.
00086     
00087     \param mnem     Mnemonic to request
00088     \param timeout  Maximum time to wait for a response
00089     \param maxAge   Maximum age of object  (timeout required parameter)
00090     """
00091     now = time.time()
00092     wait = 0.2 # seconds
00093     while True:  # always do the loop once
00094       rq = self.request(mnem)
00095       if timeout is None:                # no timeout
00096         break
00097       if rq is not None:                 # non empty response
00098         if maxAge is not None:
00099           if ( now - rq.age() ) < maxAge: # new enough!
00100             break
00101         else:                            # no age requirement
00102           break
00103       if timeout <= 0:                   # expired unsatisfied, return None
00104         rq = None
00105         break
00106       time.sleep(min(wait, timeout))
00107       timeout -= wait
00108       wait *= 1.5                        # exponential back off
00109     
00110     return rq
00111 
00112 
00113 def runScript(cvc):
00114   
00115   while cvc.connectionState is False:
00116     print "waiting for Godot..."
00117     time.sleep(1)
00118   
00119   flags = ['age', '']
00120   ml = ["LLFSDDMPFHDR31"]#, "LLFSRDMPARCFLG", "LLFSRDMPFFILE", "LLFSRDMPFSTIME", "LLFSSYSSLATUNIT", "LLFSSYSSXACTID", "LLFSSYSSFDEV"]
00121 
00122   for mnem in ml:
00123     rq = cvc.request(mnem)
00124     print mnem, rq.rawValue(), rq.name
00125     time.sleep(0.5)
00126     
00127 
00128 
00129 if __name__ == '__main__':
00130     
00131   options = gOptions.Options(['cvtHost', 'cvtPort'])
00132   try:
00133     options.parse()
00134   except Exception, msg:
00135     options.usage(str(msg))
00136     exit  
00137     
00138   cvtHost     = options.cvtHost
00139   cvtPort = int(options.cvtPort)
00140   
00141   
00142   cvc = ScriptClient( cvtHost, cvtPort )
00143 
00144   scriptThread = threading.Thread(name="scriptThread", target=runScript, args=(cvc,))
00145   scriptThread.start()
00146 
00147   scriptThread.join()
00148 
00149   cvc.close()
00150 
00151 
00152 
00153 #eof

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