Rev 747 | Rev 750 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 747 | Rev 749 | ||
|---|---|---|---|
| Line 4... | Line 4... | ||
| 4 | import sys |
4 | import sys |
| 5 | import logging as log |
5 | import logging as log |
| 6 | import getopt |
6 | import getopt |
| 7 | import atexit |
7 | import atexit |
| 8 | import time |
8 | import time |
| 9 | - | ||
| 10 | from CanPktHandlerBase import CanPktHandlerBase |
- | |
| 11 | from CanPktHandler1 import CanPktHandler1 |
- | |
| 12 | 9 | ||
| 13 | from NodeIfBase import NodeIfBase |
10 | from NodeIfBase import NodeIfBase |
| 14 | from NodeIfCanStim import NodeIfCanStim |
11 | from NodeIfCanStim import NodeIfCanStim |
| 15 | from NodeIfSerial import NodeIfSerial |
12 | from NodeIfSerial import NodeIfSerial |
| 16 | from NodeIfTCP import NodeIfTCP |
13 | from NodeIfTCP import NodeIfTCP |
| Line 20... | Line 17... | ||
| 20 | daemonApp = None |
17 | daemonApp = None |
| 21 | 18 | ||
| 22 | class CanDaemon(): |
19 | class CanDaemon(): |
| 23 | 20 | ||
| 24 | cfg = None |
21 | cfg = None |
| 25 | nodeIf = None |
- | |
| 26 | ifNotifier = None |
22 | ifNotifier = None |
| 27 | terminated = False |
23 | terminated = False |
| 28 | 24 | ||
| 29 | command_list = [] |
25 | command_list = [] |
| 30 | command_map = {} |
26 | command_map = {} |
| Line 237... | Line 233... | ||
| 237 | def logCmd(self, *args): |
233 | def logCmd(self, *args): |
| 238 | raise 'not implemented' |
234 | raise 'not implemented' |
| 239 | 235 | ||
| 240 | def execCmd(self, *args): |
236 | def execCmd(self, *args): |
| 241 | raise 'not implemented' |
237 | raise 'not implemented' |
| 242 | 238 | ||
| 243 | def parseCommand(self, cmdstr): |
239 | def parseCommand(self, cmdstr): |
| 244 | incmd = cmdstr.strip() |
240 | incmd = cmdstr.strip() |
| 245 | cmd = incmd.split(' ') |
241 | cmd = incmd.split(' ') |
| 246 | if len(cmd[0]) < 2: |
242 | if len(cmd[0]) < 2: |
| 247 | return |
243 | return |
| Line 252... | Line 248... | ||
| 252 | self.command_list[self.command_map[cmd[0]]][self.CMDLIST_HANDLER](cmd[1:]) |
248 | self.command_list[self.command_map[cmd[0]]][self.CMDLIST_HANDLER](cmd[1:]) |
| 253 | else: |
249 | else: |
| 254 | print 'unknown command: ' + cmd[0] |
250 | print 'unknown command: ' + cmd[0] |
| 255 | 251 | ||
| 256 | def exitHelper(self): |
252 | def exitHelper(self): |
| - | 253 | pass |
|
| 257 |
|
254 | # if self.nodeIf is not None and self.nodeIf.running(): |
| 258 |
|
255 | # self.nodeIf.stop() |
| 259 | 256 | ||
| 260 | def exceptHelper(self, type, value, tb): |
257 | def exceptHelper(self, type, value, tb): |
| 261 | import traceback |
258 | import traceback |
| 262 | traceback.print_exception(type, value, tb) |
259 | traceback.print_exception(type, value, tb) |
| 263 | print '\nException in main program, shutting down threads' |
260 | print '\nException in main program, shutting down threads' |
| 264 | self.terminated = True |
261 | self.terminated = True |
| 265 | self.exitHelper() |
262 | self.exitHelper() |
| 266 | # sys.exit(-1) |
- | |
| - | 263 | ||
| 267 | 264 | ||
| 268 | def run(self): |
265 | def run(self): |
| 269 | try: |
266 | try: |
| 270 | myPath = os.path.dirname(__file__) |
267 | myPath = os.path.dirname(__file__) |
| 271 | os.chdir(myPath) |
268 | os.chdir(myPath) |
| Line 296... | Line 293... | ||
| 296 | pass |
293 | pass |
| 297 | # output = a |
294 | # output = a |
| 298 | 295 | ||
| 299 | """load config defaults, or last config if exists""" |
296 | """load config defaults, or last config if exists""" |
| 300 | """run startup.cfg""" |
297 | """run startup.cfg""" |
| - | 298 | print 'INIT: Loading daemon configuration' |
|
| - | 299 | self.cfg = DaemonConfig() |
|
| - | 300 | self.cfg.load() |
|
| 301 | 301 | ||
| 302 | # print 'INIT: Loading filters and spaces' |
- | |
| 303 | # self.cfg.filterCfg.importFilter('DefaultFilter') |
- | |
| 304 | # self.cfg.stateSpaceCfg.importSpace('DefaultStateSpace') |
- | |
| 305 | # self.cfg.stateSpaceCfg.loadSpaces() |
- | |
| 306 | # self.cfg.filterCfg.loadFilters() |
- | |
| 307 | # self.cfg.setupFilterBindings() |
- | |
| 308 | # self.cfg.setupFilterChain() |
- | |
| 309 | - | ||
| 310 | # print 'INIT: Starting selected can interface' |
- | |
| 311 | # ''' nodeif thread launch ''' |
- | |
| 312 | self.ifNotifier = self.IfNotifier(self) |
302 | self.ifNotifier = self.IfNotifier(self) |
| 313 |
|
303 | for nodeIf in self.cfg.nodeInterfaces: |
| 314 | # |
- | |
| 315 | # ifConfig = NodeIfCanStim.DEFAULT_CONFIG |
- | |
| 316 |
|
304 | nodeIf.setIfNotifier(self.ifNotifier) |
| 317 | # # ifConfig = NodeIfSerial.DEFAULT_CONFIG |
- | |
| 318 | ifConfig = NodeIfTCP.DEFAULT_CONFIG |
- | |
| 319 |
|
305 | |
| 320 | atexit.register(self.exitHelper) |
306 | atexit.register(self.exitHelper) |
| 321 | sys.excepthook = self.exceptHelper |
307 | sys.excepthook = self.exceptHelper |
| - | 308 | ||
| 322 |
|
309 | print 'INIT: Starting configured node interfaces' |
| 323 |
|
310 | for nodeIf in self.cfg.nodeInterfaces: |
| 324 | # |
- | |
| 325 | if |
311 | if not nodeIf.start(): |
| 326 | print 'FATAL: Failed to initialize node interface.' |
312 | print 'FATAL: Failed to initialize node interface.' |
| 327 | print 'This may indicate that the interface is unavailable, ' \ |
313 | print 'This may indicate that the interface is unavailable, ' \ |
| 328 | 'or that you do not have permission to access it.' |
314 | 'or that you do not have permission to access it.' |
| 329 | sys.exit(-1) |
315 | sys.exit(-1) |
| 330 | 316 | ||
| 331 |
|
317 | print 'INIT: Initialize/gather info on connected can nodes and load associated state' |
| 332 |
|
318 | print 'INIT: Starting configured servers' |
| 333 | # server threads launch |
- | |
| 334 | 319 | ||
| 335 | print 'CanDaemon v1 ready' |
320 | print 'CanDaemon v1 ready' |
| 336 | print 'Enter command or type \"help\" for a list of commands' |
321 | print 'Enter command or type \"help\" for a list of commands' |
| 337 | while not self.terminated: |
322 | while not self.terminated: |
| 338 | input = raw_input('> ') |
323 | input = raw_input('> ') |
| 339 | self.parseCommand(input) |
324 | self.parseCommand(input) |
| 340 | 325 | ||
| 341 | print 'Shutdown' |
326 | print 'Shutdown' |
| 342 | # if self.nodeIf.running(): |
- | |
| 343 | # print 'Stopping If thread' |
- | |
| 344 |
|
327 | self.cfg.save() |
| 345 | 328 | ||
| 346 |
|
329 | for nodeIf in self.cfg.nodeInterfaces: |
| 347 |
|
330 | if self.nodeIf.running(): |
| - | 331 | print 'Stopping If thread' |
|
| - | 332 | self.nodeIf.stop() |
|
| 348 | 333 | ||
| 349 | print 'Waiting 0.1..' |
334 | print 'Waiting 0.1..' |
| 350 | time.sleep(0.1) # wait for threads |
335 | time.sleep(0.1) # wait for threads |
| 351 | print 'Terminating' |
336 | print 'Terminating' |
| 352 | sys.exit(0) # will except if there are threads remaining |
337 | sys.exit(0) # will except if there are threads remaining |