def logTo(self, logIP, logPort, liveIP, livePort): if liveIP is not None and livePort is not None: raise Exception('Cannot log to I3Live') self.__log = SocketWriter(logIP, logPort) self.__log.write_ts('Start of log at %s:%d' % (logIP, logPort)) self.__log.write_ts('Version info: unknown unknown unknown unknown' + ' unknown BRANCH 0:0')
def __logTo(self, logHost, logPort, liveHost, livePort): if logHost is not None and logHost == '': logHost = None if logPort is not None and logPort == 0: logPort = None if liveHost is not None and liveHost == '': liveHost = None if livePort is not None and livePort == 0: livePort = None if logPort != self.__expRunPort: raise Exception('Expected runlog port %d, not %d' % (self.__expRunPort, logPort)) if liveHost is not None and livePort is not None: raise Exception("Didn't expect I3Live logging") self.__logger = SocketWriter(logHost, logPort) self.__logger.write('Test msg') return 'OK'
def testLogSocketServer(self): port = 5432 cname = 'foo' logPath = os.path.join(TestDAQLog.DIR_PATH, cname + '.log') self.sockLog = LogSocketServer(port, cname, logPath, True) self.sockLog.startServing() for i in range(5): if self.sockLog.isServing(): break time.sleep(0.1) self.failUnless(os.path.exists(logPath), 'Log file was not created') self.failUnless(self.sockLog.isServing(), 'Log server was not started') now = datetime.datetime.now() msg = 'Test 1 2 3' client = SocketWriter('localhost', port) client.write_ts(msg, now) client.close() self.sockLog.stopServing() self.checkLog(logPath, ('%s - - [%s] %s' % (cname, str(now), msg), ))
class TinyClient(object): def __init__(self, name, num, host, port, mbeanPort, connectors): self.__name = name self.__num = num self.__connectors = connectors self.__id = DAQClient.ID.next() self.__host = host self.__port = port self.__mbeanPort = mbeanPort self.__state = 'idle' self.__order = None def __str__(self): if self.__mbeanPort == 0: mStr = '' else: mStr = ' M#%d' % self.__mbeanPort return 'ID#%d %s#%d at %s:%d%s' % \ (self.__id, self.__name, self.__num, self.__host, self.__port, mStr) def configure(self, cfgName=None): self.__state = 'ready' def connect(self, connList=None): self.__state = 'connected' def connectors(self): return self.__connectors[:] def fullName(self): if self.__num == 0: return self.__name return "%s#%d" % (self.__name, self.__num) def id(self): return self.__id def isComponent(self, name, num=-1): return self.__name == name and (num < 0 or self.__num == num) def isSource(self): return True def logTo(self, logIP, logPort, liveIP, livePort): if liveIP is not None and livePort is not None: raise Exception('Cannot log to I3Live') self.__log = SocketWriter(logIP, logPort) self.__log.write_ts('Start of log at LOG=log(%s:%d)' % (logIP, logPort)) self.__log.write_ts('Version info: unknown 000 unknown unknown' + ' unknown BRANCH 0:0') def map(self): return { "id": self.__id, "compName": self.__name, "compNum": self.__num, "host": self.__host, "rpcPort": self.__port, "mbeanPort": self.__mbeanPort, "state": self.__state } def name(self): return self.__name def num(self): return self.__num def order(self): return self.__order def reset(self): self.__state = 'idle' def resetLogging(self): pass def setOrder(self, orderNum): self.__order = orderNum def startRun(self, runNum): self.__state = 'running' def state(self): return self.__state def stopRun(self): self.__state = 'ready'
class RealComponent(object): APPENDERS = {} def __init__(self, name, num, cmdPort, mbeanPort, verbose=False): self.__name = name self.__num = num self.__state = 'FOO' self.__logger = None self.__expRunPort = None self.__cmd = RPCServer(cmdPort) self.__cmd.register_function(self.__configure, 'xmlrpc.configure') self.__cmd.register_function(self.__connect, 'xmlrpc.connect') self.__cmd.register_function(self.__getState, 'xmlrpc.getState') self.__cmd.register_function(self.__getVersionInfo, 'xmlrpc.getVersionInfo') self.__cmd.register_function(self.__logTo, 'xmlrpc.logTo') self.__cmd.register_function(self.__reset, 'xmlrpc.reset') self.__cmd.register_function(self.__resetLogging, 'xmlrpc.resetLogging') self.__cmd.register_function(self.__startRun, 'xmlrpc.startRun') self.__cmd.register_function(self.__stopRun, 'xmlrpc.stopRun') threading.Thread(target=self.__cmd.serve_forever, args=()).start() self.__mbean = RPCServer(mbeanPort) threading.Thread(target=self.__mbean.serve_forever, args=()).start() self.__cnc = xmlrpclib.ServerProxy('http://localhost:%d' % DAQPort.CNCSERVER, verbose=verbose) self.__cnc.rpc_register_component(self.__name, self.__num, 'localhost', cmdPort, mbeanPort, []) def __configure(self, cfgName=None): if self.__logger is None: raise Exception('No logging for %s#%d' % (self.__name, self.__num)) if cfgName is None: cfgStr = '' else: cfgStr = ' with %s' % cfgName self.__logger.write('Config %s#%d%s' % (self.__name, self.__num, cfgStr)) self.__state = 'ready' return 'CFG' def __connect(self, *args): self.__state = 'connected' return 'CONN' def __getState(self): return self.__state def __getVersionInfo(self): return '$Id: filename revision date time author xxx' def __logTo(self, logHost, logPort, liveHost, livePort): if logHost is not None and logHost == '': logHost = None if logPort is not None and logPort == 0: logPort = None if liveHost is not None and liveHost == '': liveHost = None if livePort is not None and livePort == 0: livePort = None if logPort != self.__expRunPort: raise Exception('Expected runlog port %d, not %d' % (self.__expRunPort, logPort)) if liveHost is not None and livePort is not None: raise Exception("Didn't expect I3Live logging") self.__logger = SocketWriter(logHost, logPort) self.__logger.write('Test msg') return 'OK' def __reset(self): self.__state = 'idle' return 'RESET' def __resetLogging(self): self.__expRunPort = None self.__logger = None self.__state = 'reset' return 'RLOG' def __startRun(self, runNum): if self.__logger is None: raise Exception('No logging for %s#%d' % (self.__name, self.__num)) self.__logger.write('Start #%d on %s#%d' % (runNum, self.__name, self.__num)) self.__state = 'running' return 'RUN#%d' % runNum def __stopRun(self): if self.__logger is None: raise Exception('No logging for %s#%d' % (self.__name, self.__num)) self.__logger.write('Stop %s#%d' % (self.__name, self.__num)) self.__state = 'stopped' return 'STOP' def close(self): self.__cmd.server_close() self.__mbean.server_close() def createCnCLogger(self, quiet=True): key = '%s#%d' % (self.__name, self.__num) if not RealComponent.APPENDERS.has_key(key): RealComponent.APPENDERS[key] = MockAppender('Mock-%s' % key) return MockCnCLogger(RealComponent.APPENDERS[key], quiet) def getState(self): return self.__getState() def setExpectedRunLogPort(self, port): self.__expRunPort = port
class TinyClient(object): def __init__(self, name, num, host, port, mbeanPort, connectors): self.name = name self.num = num self.connectors = connectors self.id = DAQClient.ID DAQClient.ID += 1 self.__host = host self.__port = port self.__mbeanPort = mbeanPort self.__state = 'idle' self.__order = None def __str__(self): if self.__mbeanPort == 0: mStr = '' else: mStr = ' M#%d' % self.__mbeanPort return 'ID#%d %s#%d at %s:%d%s' % \ (self.id, self.name, self.num, self.__host, self.__port, mStr) def configure(self, cfgName=None): self.__state = 'ready' def connect(self, connList=None): self.__state = 'connected' def getName(self): return self.name def getOrder(self): return self.__order def getState(self): return self.__state def isComponent(self, name, num=-1): return self.name == name and (num < 0 or self.num == num) def isSource(self): return True def logTo(self, logIP, logPort, liveIP, livePort): if liveIP is not None and livePort is not None: raise Exception('Cannot log to I3Live') self.__log = SocketWriter(logIP, logPort) self.__log.write_ts('Start of log at %s:%d' % (logIP, logPort)) self.__log.write_ts('Version info: unknown unknown unknown unknown' + ' unknown BRANCH 0:0') def reset(self): self.__state = 'idle' def resetLogging(self): pass def setOrder(self, orderNum): self.__order = orderNum def startRun(self, runNum): self.__state = 'running' def stopRun(self): self.__state = 'ready'