Rev 737 | Go to most recent revision | Blame | Last modification | View Log | SVN | RSS feed
#!/bin/env pythonimport osimport sysimport logging as logimport getoptimport atexitimport timefrom CanPktHandlerBase import CanPktHandlerBasefrom CanPktHandler1 import CanPktHandler1from NodeIfBase import NodeIfBasefrom NodeIfCanStim import NodeIfCanStimfrom NodeIfSerial import NodeIfSerialfrom DaemonConfig import DaemonConfigdaemonApp = Noneclass CanDaemon():nodeIf = NoneifNotifier = Noneterminated = Falseclass IfNotifier():UNDEFINED = 0TERMINATE = 1def notify(self, msg):if msg == self.TERMINATE:print 'Main terminate notify'self.terminated = Trueelse:print 'UNDEFINED NOTIFY'def __init__ (self):passdef usage(self):passdef helpCommands(self):print 'quit, exit : shutdown CanDaemon'print 'help : display help'def exitHelper(self):if self.nodeIf is not None and self.nodeIf.running():self.nodeIf.stop()def run(self):try:demoPath = os.path.dirname(__file__)os.chdir(demoPath)except:returnprint 'INIT: Start logging'log.basicConfig(level=log.DEBUG)print 'INIT: Get config options from command line / config file'cfg = DaemonConfig()try:opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="])except getopt.GetoptError:print 'Invalid usage'usage()sys.exit(2)# output = None# verbose = Falsefor o, a in opts:if o == "-v":pass# verbose = Trueif o in ("-h", "--help"):usage()sys.exit()if o in ("-o", "--output"):pass# output = aprint 'INIT: Loading filters and spaces'# cfg.filterCfg.importFilter('DefaultFilter')# cfg.stateSpaceCfg.importSpace('DefaultStateSpace')cfg.stateSpaceCfg.loadSpaces()cfg.filterCfg.loadFilters()cfg.setupFilterBindings()cfg.setupFilterChain()print 'INIT: Starting selected can interface'''' nodeif thread launch '''self.ifNotifier = self.IfNotifier()pktHandler = CanPktHandler1(cfg)ifConfig = NodeIfCanStim.DEFAULT_CONFIGself.nodeIf = NodeIfCanStim(ifConfig, pktHandler, self.ifNotifier)# ifConfig = NodeIfSerial.DEFAULT_CONFIGatexit.register(self.exitHelper)# sys.excepthook = self.exceptHelper# self.nodeIf = NodeIfSerial(pktHandler, ifConfig)if not self.nodeIf.start():print 'FATAL: Failed to initialize node interface.'print 'This may indicate that the interface is unavailable, ' \'or that you do not have permission to access it.'sys.exit(-1)print 'Initialize/gather info on connected can nodes and load associated state'print 'Start selected TCP and UDP server(s)'# server threads launchprint 'CanDaemon v1 ready'print 'Enter command or type \"help\" for a list of commands'while not self.terminated:input = raw_input('> ').strip().lower()if input == 'quit' or input == 'exit':self.terminated = Trueelif input == 'help':self.helpCommands()else:print 'Unknown command: ' + inputprint 'Shutdown'if self.nodeIf.running():self.nodeIf.stop()cfg.stateSpaceCfg.saveSpaces()cfg.filterCfg.saveFilters()time.sleep(0.1) # wait for threadssys.exit(0) # will except if there are threads remaining#---------------------------------------------------------------------------def main():daemonApp = CanDaemon()# install_thread_excepthook()daemonApp.run()#---------------------------------------------------------------------------if __name__ == '__main__':__name__ = 'Main'main()