diff --git a/TCPSserver/TCPS2tango.py b/TCPSserver/TCPS2tango.py index 23f2a0e0bcd7bb6d379aff2d24a349585236861f..0976bcacab1823ec48304a1aa53164c3ec4810f0 100644 --- a/TCPSserver/TCPS2tango.py +++ b/TCPSserver/TCPS2tango.py @@ -5,6 +5,8 @@ # import string import tango +import os.path as op + # module wide constants okmsg = "Command execution OK" errmsg_header = "*** ERROR" @@ -150,9 +152,12 @@ class ProxyContainer(object): """ @param string fname : - @return : + @return : [result , port, log_f] @author """ + + port_n = "20000" + log_f = "/dev/null" self.logger.debug("ProxyContainer.parse_conf_file() parsing: " + fname) try: conf_file = open(fname, "r") @@ -176,7 +181,7 @@ class ProxyContainer(object): ): # discard comment and empyt lines continue datum = line.split(" ") - if datum[0] != "GET" and datum[0] != "SET": + if not (datum[0] == "GET" or datum[0] == "SET" or datum[0] == "PORT_NUMBER" or datum[0] == "LOG_FILE"): result = False self.logger.error( "ProxyContainer: parse_conf_file error on line " @@ -184,7 +189,37 @@ class ProxyContainer(object): + " " + datum[0] ) - return result + return [result, port_n, log_f] + if datum[0] == "PORT_NUMBER": + port_str = datum[-1] + try: + portint = int(port_str) + except ValueError as e: + portint = -1 + if str(portint) != port_str: + result = False + self.logger.error( + "ProxyContainer: parse_conf_file error on line " + + repr(count) + + " : PORT_NUMBER " + + datum[1] + ) + return [result, port_n, log_f] + port_n = portint + + if datum[0] == "LOG_FILE": + log_name = datum[-1] + if not op.exists(op.dirname(log_name)): + result = False + self.logger.error( + "ProxyContainer: parse_conf_file error on line " + + repr(count) + + " : LOG_FILE " + + datum[1] + ) + return [result, port_n, log_f] + log_f = log_name + if datum[0] == "GET": if datum[1] not in self.get_dict: tangostring = datum[-1] @@ -201,7 +236,7 @@ class ProxyContainer(object): + " : SET duplicate " + datum[1] ) - return result + return [result, port_n, log_f] if datum[0] == "SET": # create special keys for searching - must include also list of parameter names @@ -229,9 +264,9 @@ class ProxyContainer(object): + " : SET duplicate " + repr(datum) ) - return result + return [result, port_n, log_f] - return result + return [result, port_n, log_f] def dispatch_call(self, data): """ @@ -381,25 +416,41 @@ class ProxyAttribute(Proxy): # resp='R'+','+'?'+','+rstat+','+0+term return "R,?,BEN------,0" + term try: - # self.logger.debug( 'ProxyAttribute.get(): reading : '+self.__devname) + self.logger.debug( 'ProxyAttribute.get(): reading : '+self.__devname + '/'+self.__attribute) attr = self.__device.read_attribute( self.__attribute ) # read_attribute_as_string ???? val = attr.value - # self.logger.debug( 'ProxyAttribute.get(): reading value ok') + if val is None: # some devices return INVALID if not correctly woarking - force to NaN and error + error = True + val = float("Nan") except tango.DevFailed as myex: - des0 = myex[0].desc + des0 = myex.args[0].reason self.logger.error( "GET: " + self.__devname + "/" + self.__attribute - + " failed with exception " + + " failed with exception: " + des0 ) + self.logger.debug('ProxyAttribute.GET(): reading value failed') # print 'ProxyAttribute: get() '+self.__attribute+' ERROR' val = 0 error = True + except Exception as e: + + self.logger.error( + "GET: " + + self.__devname + + "/" + + self.__attribute + + " failed with Exception" + ) + self.logger.debug('ProxyAttribute.get(): reading value excpetion') + val = 0 + error = True + # analyze result and prepare answer rtype = "?" @@ -483,8 +534,8 @@ class ProxyAttribute(Proxy): if self.__attrinfo == 0: try: self.__attrinfo = self.__device.get_attribute_config(self.__attribute) - except tango.DevFailed as e: - des = e[0].desc + except tango.DevFailed as myex: + des = myex.args[0].reason errmsg = errmsg_header + " in perform command : " + des self.logger.error( "ProxyAttribute.set(): failed to create self.__attrinfo for: " @@ -505,13 +556,15 @@ class ProxyAttribute(Proxy): + "/" + self.__attribute ) + self.logger.debug( 'writing '+self.__devname+'/'+self.__attribute) self.__device.write_attribute(self.__attrinfo, val) self.logger.info( "SET: ACCEPT" + self.__devname + "/" + self.__attribute + " " + data[0] ) return okmsg + term except tango.DevFailed as myex: - des0 = myex[0].desc + self.logger.debug( 'excpetion writing '+self.__devname+'/'+self.__attribute) + des0 = myex.args[0].reason self.logger.debug( "SET: " + self.__devname diff --git a/TCPSserver/TCPStwserver.py b/TCPSserver/TCPStwserver.py index f5933c82baa01bb251e42941432ff7f6c5654cb5..9a79c054e713253e14e25899e183860103f32939 100755 --- a/TCPSserver/TCPStwserver.py +++ b/TCPSserver/TCPStwserver.py @@ -5,6 +5,7 @@ from twisted.internet.protocol import Factory from twisted.internet import reactor # import time +import os.path as op import sys import TCPS2tango import logging @@ -66,11 +67,85 @@ def makeTCPSFactory( factory._timeout = timeout factory.handler = TCPS2tango.ProxyContainer() factory.handler.add_logger(logger) - factory.handler.parse_conf_file(fname) + [res, port, log] = factory.handler.parse_conf_file(fname) + # print(res) + # print(port) + # print(log) return factory +def pre_parse_conf_file(fname): + """ + @extract port number , name of log file form configuration + @param string fname : + @return : [result , port, log_f] + """ + port_n = 20000 # default + log_f = "/dev/null" + result = True + try: + conf_file = open(fname, "r") + except Exception: + print( + "pre_parse_conf_file() fatal error opening conf_file: " + + fname + ) + raise # impossible to continue.... + count = 0 + result = True + for rawline in conf_file.readlines(): + if not rawline.strip(): + continue # discard empty lines + count = count + 1 + line = " ".join( + rawline.split() + ) # collapse multiple white spaces into single white space + if ( + line[0] == "#" or line[0] == "*" or not line.strip() + ): # discard comment and empyt lines + continue + datum = line.split(" ") + if not (datum[0] == "GET" or datum[0] == "SET" or datum[0] == "PORT_NUMBER" or datum[0] == "LOG_FILE"): + result = False + print( + "pre_parse_conf_file error on line " + + repr(count) + + " " + + datum[0] + ) + return [result, port_n, log_f] + if datum[0] == "PORT_NUMBER": + port_str = datum[-1] + try: + portint = int(port_str) + except ValueError as e: + portint = -1 + if str(portint) != port_str: + result = False + print( + "pre_parse_conf_file error on line " + + repr(count) + + " : PORT_NUMBER " + + datum[1] + ) + return [result, port_n, log_f] + port_n = portint + + if datum[0] == "LOG_FILE": + log_name = datum[-1] + if not op.exists(op.dirname(log_name)): + result = False + print( + "pre_parse_conf_file error on line " + + repr(count) + + " : LOG_FILE " + + datum[1] + ) + return [result, port_n, log_f] + log_f = log_name + return [result, port_n, log_f] + # ----------------------------------------------------------------- # remeber to switch use argparse for configuring cmd line options with python > 2.7 @@ -98,13 +173,17 @@ def main(argv=None): if options.port: tcpport = int(options.port) + force_port = True else: tcpport = 20000 + force_port = False if options.logfile: lfile = options.logfile + force_logfile = True else: lfile = "/dev/null" + force_logfile = False if options.loglevel: lev = options.loglevel @@ -135,6 +214,13 @@ def main(argv=None): else: verbose = False + # pre-parse the configuration file to extract port number and name of log gfile + [res, port_n, log_f] = pre_parse_conf_file(fname) + if not force_logfile: + lfile = log_f # if log file name is not forced on cmd line get it from config file (or default) + if not force_port: + tcpport = port_n # if port number is not forced on cmd line get it from config file (or default) + # Set up a specific logger with our desired output level # use INFO for for tracking all SET operations # use ERROR and CRITICAL for errors (non-fatal and fatal) diff --git a/TCPSserver/TODO b/TCPSserver/TODO index c727ff6401a3fe4f5684c716659e4d93ea49a3ea..e0b920e53f381e2de5e143fd127480862757612c 100644 --- a/TCPSserver/TODO +++ b/TCPSserver/TODO @@ -1,2 +1 @@ -- analizzare "exception processing data" -- Transient CORBA excpetion:come si alza il timeout? \ No newline at end of file +- Transient CORBA excpetion:come si alza il timeout?