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 00011 __facility__ = "Online" 00012 __abstract__ = "Command handler client" 00013 __author__ = "Amedeo Perazzo <perazzo@slac.stanford.edu> SLAC - GLAST I&T/Online" 00014 __date__ = ("$Date: 2005/07/11 17:07:08 $").split(' ')[1] 00015 __version__ = "$Revision: 2.7 $" 00016 __release__ = "$Name: R04-12-00 $" 00017 __credits__ = "SLAC" 00018 00019 import LATTE.copyright_SLAC 00020 import logging as log 00021 import struct 00022 00023 import gCommand 00024 import gResponse 00025 import gSocket 00026 import gException 00027 00028 class CmdCli(gSocket.Socket): 00029 PortNumber = 2000 00030 00031 def __init__(self, handler=None, debug=0, port=PortNumber): 00032 gSocket.Socket.__init__(self, port) 00033 self.__handler = handler 00034 self.cmdrsp = None 00035 self.setDebug(debug) 00036 00037 def setDebug(self, debug): 00038 if debug == 0: 00039 self.cmdrsp = self.__cmdrsp 00040 else: 00041 self.cmdrsp = self.__cmdrsp_debug 00042 00043 def getDebug(self): 00044 return self.cmdrsp == self.__cmdrsp_debug 00045 00046 def handler(self, newhandler): 00047 self.__handler = newhandler 00048 00049 def __command_response(self, command): 00050 self.lock() 00051 try: 00052 self.send(command) 00053 result = self.recv(1024) 00054 (remaining,) = struct.unpack('!I', result[0:4]) 00055 remaining = remaining+16-len(result) 00056 if remaining > 0: 00057 result += self.recvall(remaining) 00058 self.release() 00059 except: 00060 self.release() 00061 if self.__handler is not None: 00062 self.__handler.timedout() 00063 status = gException.LATInterfaceStatus.status('OCS_TMO') 00064 raise gException.LATInterfaceException(status) 00065 if len(result) == 0: 00066 if self.__handler is not None: 00067 self.__handler.disconnected() 00068 status = gException.LATInterfaceStatus.status('OCS_NOSRV') 00069 raise gException.LATInterfaceException(status) 00070 return result 00071 00072 def __cmdrsp(self, functionname, *arguments): 00073 command = gCommand.Command(functionname, *arguments) 00074 result = self.__command_response(command.packet()) 00075 response = gResponse.Response(command, result) 00076 return response 00077 00078 def __cmdrsp_debug(self, functionname, *arguments): 00079 log.debug('%s calling: %s(%s)' %(__name__, functionname, arguments)) 00080 command = gCommand.Command(functionname, *arguments) 00081 result = self.__command_response(command.packet()) 00082 response = gResponse.Response(command, result) 00083 statusmsg = gException.LATInterfaceStatus(response.status()) 00084 log.debug('%s returns: status: %s, value(s): %s' \ 00085 %(__name__, statusmsg, response.payloads())) 00086 return response