Subversion Repositories HomeAutomation

Rev

Rev 746 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 746 Rev 747
1
#!/bin/env python
1
#!/bin/env python
2
 
2
 
3
import os
3
import os
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
 
9
 
10
from CanPktHandlerBase import CanPktHandlerBase
10
from CanPktHandlerBase import CanPktHandlerBase
11
from CanPktHandler1 import CanPktHandler1
11
from CanPktHandler1 import CanPktHandler1
12
 
12
 
13
from NodeIfBase import NodeIfBase
13
from NodeIfBase import NodeIfBase
14
from NodeIfCanStim import NodeIfCanStim
14
from NodeIfCanStim import NodeIfCanStim
15
from NodeIfSerial import NodeIfSerial
15
from NodeIfSerial import NodeIfSerial
16
from NodeIfTCP import NodeIfTCP
16
from NodeIfTCP import NodeIfTCP
17
 
17
 
18
from DaemonConfig import DaemonConfig
18
from DaemonConfig import DaemonConfig
19
 
19
 
20
daemonApp = None
20
daemonApp = None
21
 
21
 
22
class CanDaemon():
22
class CanDaemon():
23
   
23
   
24
    cfg = None
24
    cfg = None
25
    nodeIf = None
25
    nodeIf = None
26
    ifNotifier = None
26
    ifNotifier = None
27
    terminated = False
27
    terminated = False
28
   
28
   
29
    command_list = []
29
    command_list = []
30
    command_map = {}
30
    command_map = {}
31
    CMDLIST_NAME = 0
31
    CMDLIST_NAME = 0
32
    CMDLIST_HANDLER = 1
32
    CMDLIST_HANDLER = 1
33
    CMDLIST_PARAMINFO = 2
33
    CMDLIST_PARAMINFO = 2
34
    CMDLIST_HELP = 3
34
    CMDLIST_HELP = 3
35
 
35
 
36
    logHelp = 'usage:\t log [options]\n\n' \
36
    logHelp = 'usage:\t log [options]\n\n' \
37
              '\t options:\n' \
37
              '\t options:\n' \
38
              '\t level <num>, where: 0 < num < 3\n'
38
              '\t level <num>, where: 0 < num < 3\n'
39
    stateHelp = 'usage: state [name/add/rem]'
39
    stateHelp = 'usage: state [name/add/rem]'
40
    filterHelp = 'usage: filter [name/add/rem]'
40
    filterHelp = 'usage: filter [name/add/rem]'
41
    addifHelp = 'usage: addif [name] [options]'
41
    addifHelp = 'usage: addif [name] [options]'
42
    addbridgeHelp = 'usage: addbridge [interface names]'
42
    addbridgeHelp = 'usage: addbridge [interface names]'
43
    tcpdHelp = 'usage: tcpd [start/stop/configure] [options]'
43
    tcpdHelp = 'usage: tcpd [start/stop/configure] [options]'
44
    tlsdHelp = 'usage: tlsd [start/stop/configure] [options]'
44
    tlsdHelp = 'usage: tlsd [start/stop/configure] [options]'
45
    canctldHelp = 'usage: canctld [start/stop/configure] [options]'
45
    canctldHelp = 'usage: canctld [start/stop/configure] [options]'
46
    simHelp = 'usage: sim [record/play/add/rem/save/load] [options]'
46
    simHelp = 'usage: sim [record/play/add/rem/save/load] [options]'
47
    help_command_map = {}
47
    help_command_map = {}
48
   
48
   
49
    logOptions = {}
49
    logOptions = {}
50
    ifOptions = {}
50
    ifOptions = {}
51
    bridgeOptions = {}
51
    bridgeOptions = {}
52
    tcpdOptions = {}
52
    tcpdOptions = {}
53
    tlsdOptions = {}
53
    tlsdOptions = {}
54
    canctldOptions = {}
54
    canctldOptions = {}
55
    simOptions = {}
55
    simOptions = {}
56
 
56
 
57
    logValidOptions = {'level' : ['range', 0, 3]}
57
    logValidOptions = {'level' : ['range', 0, 3]}
58
    ifValidOptions = {'type' : ['alt', 'serial','udp']}
58
    ifValidOptions = {'type' : ['alt', 'serial','udp']}
59
   
59
   
60
    ifSerialOptions = {'' : []}
60
    ifSerialOptions = {'' : []}
61
    ifCanStimOptions = {'' : []}
61
    ifCanStimOptions = {'' : []}
62
    ifTelnetOptions = {'' : []}
62
    ifTelnetOptions = {'' : []}
63
    ifTcpTlsOptions = {'' : []}
63
    ifTcpTlsOptions = {'' : []}
64
    ifUDPOptions = {'' : []}
64
    ifUDPOptions = {'' : []}
65
   
65
   
66
    bridgeValidOptions = {'' : ['']}
66
    bridgeValidOptions = {'' : ['']}
67
    tcpdValidOptions = {'' : []}
67
    tcpdValidOptions = {'' : []}
68
    tlsdValidOptions = {'' : []}
68
    tlsdValidOptions = {'' : []}
69
    canctldValidOptions = {'' : []}
69
    canctldValidOptions = {'' : []}
70
    simValidOptions = {'' : []}
70
    simValidOptions = {'' : []}
71
 
71
 
72
    def __init__ (self):
72
    def __init__ (self):
73
       
73
       
74
        self.command_list = [['help', self.helpCmd, '', 'display help'],
74
        self.command_list = [['help', self.helpCmd, '', 'display help'],
75
                       ['log', self.logCmd, '<options>', 'log control: \"help log\"'],
75
                       ['log', self.logCmd, '<options>', 'log control: \"help log\"'],
76
                       ['exec', self.execCmd, '<filename>', 'run command listing\n'],
76
                       ['exec', self.execCmd, '<filename>', 'run command listing\n'],
77
                       ['filter', self.filterCmd, '<cmd>', 'filter commands: \"help filter\"'],
77
                       ['filter', self.filterCmd, '<cmd>', 'filter commands: \"help filter\"'],
78
                       ['state', self.stateCmd, '<cmd>', 'state commands: \"help state\"\n'],
78
                       ['state', self.stateCmd, '<cmd>', 'state commands: \"help state\"\n'],
79
                       ['addif', self.ifAddCmd, '<options>', 'add interface: \"help addif\"'],
79
                       ['addif', self.ifAddCmd, '<options>', 'add interface: \"help addif\"'],
80
                       ['remif', self.ifRemCmd, '<name>', 'remove interface'],
80
                       ['remif', self.ifRemCmd, '<name>', 'remove interface'],
81
                       ['ifup', self.ifUpCmd, '<name>', 'bring up interface'],
81
                       ['ifup', self.ifUpCmd, '<name>', 'bring up interface'],
82
                       ['ifdown', self.ifDownCmd, '<name>', 'bring down interface\n'],
82
                       ['ifdown', self.ifDownCmd, '<name>', 'bring down interface\n'],
83
                       ['addbrigde', self.addBridgeCmd, '<options>', 'add bridge: \"help addbridge\"'],
83
                       ['addbrigde', self.addBridgeCmd, '<options>', 'add bridge: \"help addbridge\"'],
84
                       ['rembridge', self.remBridgeCmd, '<id>', 'remove bridge\n'],
84
                       ['rembridge', self.remBridgeCmd, '<id>', 'remove bridge\n'],
85
                       ['tcpd', self.tcpdCmd, '<cmd>', 'tcpd server control: \"help tcpd\"'],
85
                       ['tcpd', self.tcpdCmd, '<cmd>', 'tcpd server control: \"help tcpd\"'],
86
                       ['tlsd', self.tlsdCmd, '<cmd>', 'tlsd server control: \"help tlsd\"'],
86
                       ['tlsd', self.tlsdCmd, '<cmd>', 'tlsd server control: \"help tlsd\"'],
87
                       ['canctld', self.canCtldCmd, '<cmd>', 'canctld server control: \"help canctld\"\n'],
87
                       ['canctld', self.canCtldCmd, '<cmd>', 'canctld server control: \"help canctld\"\n'],
88
                       ['status', self.statusCmd, '', 'show daemon status'],
88
                       ['status', self.statusCmd, '', 'show daemon status'],
89
                       ['stats', self.statsCmd, '', 'show statistics\n'],
89
                       ['stats', self.statsCmd, '', 'show statistics'],
90
                       ['nodelist', self.nodesCmd, '', 'list can nodes'],
90
                       ['nodelist', self.nodesCmd, '', 'list can nodes\n'],
91
                       ['filterlist', self.filterlistCmd, '', 'list active filters\n'],
-
 
92
                       ['sim', self.simCmd, '<cmd>', 'simulator commands: \"help sim\"']]
91
                       ['sim', self.simCmd, '<cmd>', 'simulator commands: \"help sim\"']]
93
       
92
       
94
        index = 0
93
        index = 0
95
        for cmd in self.command_list:
94
        for cmd in self.command_list:
96
            self.command_map[cmd[0]] = index
95
            self.command_map[cmd[0]] = index
97
            index += 1
96
            index += 1
98
 
97
 
99
        self.help_command_map = {'log' : self.logHelp,
98
        self.help_command_map = {'log' : self.logHelp,
100
                                'filter' : self.filterHelp,
99
                                'filter' : self.filterHelp,
101
                                'state' : self.stateHelp,
100
                                'state' : self.stateHelp,
102
                                'addif' : self.addifHelp,
101
                                'addif' : self.addifHelp,
103
                                'addbridge' : self.addbridgeHelp,
102
                                'addbridge' : self.addbridgeHelp,
104
                                'tcpd' : self.tcpdHelp,
103
                                'tcpd' : self.tcpdHelp,
105
                                'tlsd' : self.tlsdHelp,
104
                                'tlsd' : self.tlsdHelp,
106
                                'canctld' : self.canctldHelp,
105
                                'canctld' : self.canctldHelp,
107
                                'sim' : self.simHelp}
106
                                'sim' : self.simHelp}
108
 
107
 
109
        self.cfg = DaemonConfig()
108
        self.cfg = DaemonConfig()
110
   
109
   
111
    class IfNotifier():
110
    class IfNotifier():
112
        """ Used by if threads to send messages to the main program thread """
111
        """ Used by if threads to send messages to the main program thread """
113
       
112
       
114
        parent = None
113
        parent = None
115
        UNDEFINED = 0
114
        UNDEFINED = 0
116
        TERMINATE = 1
115
        TERMINATE = 1
117
       
116
       
118
        def __init__(self, parent):
117
        def __init__(self, parent):
119
            self.parent = parent
118
            self.parent = parent
120
   
119
   
121
        def notify(self, msg):
120
        def notify(self, msg):
122
            if msg == self.TERMINATE:
121
            if msg == self.TERMINATE:
123
                print 'Main terminate notify'
122
                print 'Main terminate notify'
124
            else:
123
            else:
125
                print 'UNDEFINED NOTIFY'
124
                print 'UNDEFINED NOTIFY'
126
       
125
       
127
   
126
   
128
    def __rightAdjust(self, refstr, str):
127
    def __rightAdjust(self, refstr, str):
129
        spcstr = ''
128
        spcstr = ''
130
        endpos = 25-len(refstr)
129
        endpos = 25-len(refstr)
131
        for i in range(1,endpos):
130
        for i in range(1,endpos):
132
            if i == endpos-5:
131
            if i == endpos-5:
133
                spcstr += ':'
132
                spcstr += ':'
134
            else:
133
            else:
135
                spcstr += ' '
134
                spcstr += ' '
136
        return (spcstr+str)
135
        return (spcstr+str)
137
   
136
   
138
    def helpCmd(self, *args):
137
    def helpCmd(self, *args):
139
        try:
138
        try:
140
            args = args[0]
139
            args = args[0]
141
        except:
140
        except:
142
            args = []
141
            args = []
143
        if len(args) < 1:
142
        if len(args) < 1:
144
            quitStr = 'quit, exit'
143
            quitStr = 'quit, exit'
145
            print quitStr, self.__rightAdjust(quitStr, 'shutdown pyCanDaemon')
144
            print quitStr, self.__rightAdjust(quitStr, 'shutdown pyCanDaemon')
146
            for cmd in self.command_list:
145
            for cmd in self.command_list:
147
                cmdstr = cmd[self.CMDLIST_NAME] + ' ' + cmd[self.CMDLIST_PARAMINFO]
146
                cmdstr = cmd[self.CMDLIST_NAME] + ' ' + cmd[self.CMDLIST_PARAMINFO]
148
                print cmdstr, \
147
                print cmdstr, \
149
                      self.__rightAdjust(cmdstr, cmd[self.CMDLIST_HELP])
148
                      self.__rightAdjust(cmdstr, cmd[self.CMDLIST_HELP])
150
        elif len(args) < 2:
149
        elif len(args) < 2:
151
            subCmd = args[0]
150
            subCmd = args[0]
152
            if self.help_command_map.has_key(subCmd):
151
            if self.help_command_map.has_key(subCmd):
153
                print self.help_command_map[subCmd]
152
                print self.help_command_map[subCmd]
154
            else:
153
            else:
155
                print 'no help for ' + subCmd
154
                print 'no help for ' + subCmd
156
        else:
155
        else:
157
            print 'usage: help [topic]'
156
            print 'usage: help [topic]'
158
 
157
 
159
    def filterCmd(self, *args):
158
    def filterCmd(self, *args):
160
        try:
159
        try:
161
            args = args[0]
160
            args = args[0]
162
        except:
161
        except:
163
            args = []
162
            args = []
164
        log.debug('filterCmd ' + str(args))
163
        log.debug('filterCmd ' + str(args))
165
        if len(args) < 2:
164
        if len(args) < 2:
166
            print 'Invalid arguments\n' + self.filterHelp
165
            print 'Invalid arguments\n' + self.filterHelp
167
            return
166
            return
168
        cmd = args[0]
167
        cmd = args[0]
169
        filterName = args[1]
168
        filterName = args[1]
170
        # FIXME: verify 
169
        # FIXME: verify 
171
        if cmd == 'add':
170
        if cmd == 'add':
172
            print 'Importing filter: ' + filterName
171
            print 'Importing filter: ' + filterName
173
            self.cfg.filterCfg.importFilter(filterName)
172
            self.cfg.filterCfg.importFilter(filterName)
174
        elif cmd == 'rem' or cmd == 'remove':
173
        elif cmd == 'rem' or cmd == 'remove':
175
            print 'Deleting filter: ' + filterName
174
            print 'Deleting filter: ' + filterName
176
            # FIXME: actually delete
175
            # FIXME: actually delete
177
            raise 'Not implemented'
176
            raise 'Not implemented'
178
   
177
   
179
    def stateCmd(self, *args):
178
    def stateCmd(self, *args):
180
        try:
179
        try:
181
            args = args[0]
180
            args = args[0]
182
        except:
181
        except:
183
            args = []
182
            args = []
184
        log.debug('stateCmd ' + str(args))
183
        log.debug('stateCmd ' + str(args))
185
        if len(args) < 2:
184
        if len(args) < 2:
186
            print 'Invalid arguments\n' + self.stateHelp
185
            print 'Invalid arguments\n' + self.stateHelp
187
            return
186
            return
188
        cmd = args[0]
187
        cmd = args[0]
189
        spaceName = args[1]
188
        spaceName = args[1]
190
        # FIXME: verify 
189
        # FIXME: verify 
191
        if cmd == 'add':
190
        if cmd == 'add':
192
            print 'Importing state space: ' + spaceName
191
            print 'Importing state space: ' + spaceName
193
            self.cfg.stateSpaceCfg.importSpace(spaceName)
192
            self.cfg.stateSpaceCfg.importSpace(spaceName)
194
        elif cmd == 'rem' or cmd == 'remove':
193
        elif cmd == 'rem' or cmd == 'remove':
195
            print 'Deleting state space: ' + spaceName
194
            print 'Deleting state space: ' + spaceName
196
            # FIXME: actually delete statespace
195
            # FIXME: actually delete statespace
197
            raise 'Not implemented'
196
            raise 'Not implemented'
198
   
197
   
199
    def ifAddCmd(self, *args):
198
    def ifAddCmd(self, *args):
200
        raise 'not implemented'
199
        raise 'not implemented'
201
   
200
   
202
    def ifRemCmd(self, *args):
201
    def ifRemCmd(self, *args):
203
        raise 'not implemented'
202
        raise 'not implemented'
204
 
203
 
205
    def ifUpCmd(self, *args):
204
    def ifUpCmd(self, *args):
206
        raise 'not implemented'
205
        raise 'not implemented'
207
 
206
 
208
    def ifDownCmd(self, *args):
207
    def ifDownCmd(self, *args):
209
        raise 'not implemented'
208
        raise 'not implemented'
210
   
209
   
211
    def addBridgeCmd(self, *args):
210
    def addBridgeCmd(self, *args):
212
        raise 'not implemented'
211
        raise 'not implemented'
213
 
212
 
214
    def remBridgeCmd(self, *args):
213
    def remBridgeCmd(self, *args):
215
        raise 'not implemented'
214
        raise 'not implemented'
216
   
215
   
217
    def tcpdCmd(self, *args):
216
    def tcpdCmd(self, *args):
218
        raise 'not implemented'
217
        raise 'not implemented'
219
   
218
   
220
    def tlsdCmd(self, *args):
219
    def tlsdCmd(self, *args):
221
        raise 'not implemented'
220
        raise 'not implemented'
222
   
221
   
223
    def canCtldCmd(self, *args):
222
    def canCtldCmd(self, *args):
224
        raise 'not implemented'
223
        raise 'not implemented'
225
   
224
   
226
    def statusCmd(self, *args):
225
    def statusCmd(self, *args):
227
        raise 'not implemented'
226
        raise 'not implemented'
228
   
227
   
229
    def statsCmd(self, *args):
228
    def statsCmd(self, *args):
230
        raise 'not implemented'
229
        raise 'not implemented'
231
   
230
   
232
    def nodesCmd(self, *args):
231
    def nodesCmd(self, *args):
233
        raise 'not implemented'
-
 
234
   
-
 
235
    def filterlistCmd(self, *args):
-
 
236
        raise 'not implemented'
232
        raise 'not implemented'
237
   
233
   
238
    def simCmd(self, *args):
234
    def simCmd(self, *args):
239
        raise 'not implemented'
235
        raise 'not implemented'
240
   
236
   
241
    def logCmd(self, *args):
237
    def logCmd(self, *args):
242
        raise 'not implemented'
238
        raise 'not implemented'
243
 
239
 
244
    def execCmd(self, *args):
240
    def execCmd(self, *args):
245
        raise 'not implemented'
241
        raise 'not implemented'
246
   
242
   
247
    def parseCommand(self, cmdstr):
243
    def parseCommand(self, cmdstr):
248
        incmd = cmdstr.strip()
244
        incmd = cmdstr.strip()
249
        cmd = incmd.split(' ')
245
        cmd = incmd.split(' ')
250
        if len(cmd[0]) < 2:
246
        if len(cmd[0]) < 2:
251
            return
247
            return
252
        cmd[0] = cmd[0].lower()
248
        cmd[0] = cmd[0].lower()
253
        if cmd[0] == 'quit' or cmd[0] == 'exit':
249
        if cmd[0] == 'quit' or cmd[0] == 'exit':
254
            self.terminated = True
250
            self.terminated = True
255
        elif self.command_map.has_key(cmd[0]):
251
        elif self.command_map.has_key(cmd[0]):
256
            self.command_list[self.command_map[cmd[0]]][self.CMDLIST_HANDLER](cmd[1:])
252
            self.command_list[self.command_map[cmd[0]]][self.CMDLIST_HANDLER](cmd[1:])
257
        else:
253
        else:
258
            print 'unknown command: ' + cmd[0]
254
            print 'unknown command: ' + cmd[0]
259
   
255
   
260
    def exitHelper(self):
256
    def exitHelper(self):
261
        if self.nodeIf is not None and self.nodeIf.running():
257
        if self.nodeIf is not None and self.nodeIf.running():
262
            self.nodeIf.stop()
258
            self.nodeIf.stop()
263
           
259
           
264
    def exceptHelper(self, type, value, tb):
260
    def exceptHelper(self, type, value, tb):
265
        import traceback
261
        import traceback
266
        traceback.print_exception(type, value, tb)
262
        traceback.print_exception(type, value, tb)
267
        print '\nException in main program, shutting down threads'
263
        print '\nException in main program, shutting down threads'
268
        self.terminated = True
264
        self.terminated = True
269
        self.exitHelper()
265
        self.exitHelper()
270
 #       sys.exit(-1)
266
 #       sys.exit(-1)
271
 
267
 
272
    def run(self):
268
    def run(self):
273
        try:
269
        try:
274
            myPath = os.path.dirname(__file__)
270
            myPath = os.path.dirname(__file__)
275
            os.chdir(myPath)
271
            os.chdir(myPath)
276
        except:
272
        except:
277
            return
273
            return
278
       
274
       
279
        print 'INIT: Start logging'
275
        print 'INIT: Start logging'
280
        log.basicConfig(level=log.DEBUG)
276
        log.basicConfig(level=log.DEBUG)
281
       
277
       
282
        """command parameters only dictate verbosity and use of config file"""
278
        """command parameters only dictate verbosity and use of config file"""
283
        print 'INIT: Get config options from command line / config file'
279
        print 'INIT: Get config options from command line / config file'
284
        try:
280
        try:
285
            opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="])
281
            opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="])
286
        except getopt.GetoptError:
282
        except getopt.GetoptError:
287
            print 'Invalid usage'
283
            print 'Invalid usage'
288
            usage()
284
            usage()
289
            sys.exit(2)
285
            sys.exit(2)
290
     #   output = None
286
     #   output = None
291
     #   verbose = False
287
     #   verbose = False
292
        for o, a in opts:
288
        for o, a in opts:
293
            if o == "-v":
289
            if o == "-v":
294
                pass
290
                pass
295
     #           verbose = True
291
     #           verbose = True
296
            if o in ("-h", "--help"):
292
            if o in ("-h", "--help"):
297
                usage()
293
                usage()
298
                sys.exit()
294
                sys.exit()
299
            if o in ("-o", "--output"):
295
            if o in ("-o", "--output"):
300
                pass
296
                pass
301
    #            output = a
297
    #            output = a
302
 
298
 
303
        """load config defaults, or last config if exists"""
299
        """load config defaults, or last config if exists"""
304
        """run startup.cfg"""
300
        """run startup.cfg"""
305
       
301
       
306
#        print 'INIT: Loading filters and spaces'
302
#        print 'INIT: Loading filters and spaces'
307
#       self.cfg.filterCfg.importFilter('DefaultFilter')
303
#       self.cfg.filterCfg.importFilter('DefaultFilter')
308
#       self.cfg.stateSpaceCfg.importSpace('DefaultStateSpace')
304
#       self.cfg.stateSpaceCfg.importSpace('DefaultStateSpace')
309
#        self.cfg.stateSpaceCfg.loadSpaces()
305
#        self.cfg.stateSpaceCfg.loadSpaces()
310
#        self.cfg.filterCfg.loadFilters()
306
#        self.cfg.filterCfg.loadFilters()
311
#        self.cfg.setupFilterBindings()
307
#        self.cfg.setupFilterBindings()
312
#        self.cfg.setupFilterChain()
308
#        self.cfg.setupFilterChain()
313
 
309
 
314
#        print 'INIT: Starting selected can interface'
310
#        print 'INIT: Starting selected can interface'
315
#        ''' nodeif thread launch '''
311
#        ''' nodeif thread launch '''
316
        self.ifNotifier = self.IfNotifier(self)
312
        self.ifNotifier = self.IfNotifier(self)
317
        pktHandler = CanPktHandler1(self.cfg)
313
        pktHandler = CanPktHandler1(self.cfg)
318
#        
314
#        
319
#        ifConfig = NodeIfCanStim.DEFAULT_CONFIG
315
#        ifConfig = NodeIfCanStim.DEFAULT_CONFIG
320
#        self.nodeIf = NodeIfCanStim(ifConfig, pktHandler, self.ifNotifier)
316
#        self.nodeIf = NodeIfCanStim(ifConfig, pktHandler, self.ifNotifier)
321
# #       ifConfig = NodeIfSerial.DEFAULT_CONFIG
317
# #       ifConfig = NodeIfSerial.DEFAULT_CONFIG
322
        ifConfig = NodeIfTCP.DEFAULT_CONFIG
318
        ifConfig = NodeIfTCP.DEFAULT_CONFIG
323
#    
319
#    
324
        atexit.register(self.exitHelper)
320
        atexit.register(self.exitHelper)
325
        sys.excepthook = self.exceptHelper
321
        sys.excepthook = self.exceptHelper
326
        self.nodeIf = NodeIfTCP(pktHandler, self.ifNotifier ,None)
322
        self.nodeIf = NodeIfTCP(pktHandler, self.ifNotifier, None)
327
#       self.nodeIf = NodeIfSerial(pktHandler, ifConfig)
323
#       self.nodeIf = NodeIfSerial(pktHandler, ifConfig)
328
#        
324
#        
329
        if not self.nodeIf.start():
325
        if not self.nodeIf.start():
330
            print 'FATAL: Failed to initialize node interface.'
326
            print 'FATAL: Failed to initialize node interface.'
331
            print 'This may indicate that the interface is unavailable, ' \
327
            print 'This may indicate that the interface is unavailable, ' \
332
                  'or that you do not have permission to access it.'
328
                  'or that you do not have permission to access it.'
333
            sys.exit(-1)
329
            sys.exit(-1)
334
 
330
 
335
        print 'Initialize/gather info on connected can nodes and load associated state'
331
        print 'Initialize/gather info on connected can nodes and load associated state'
336
        print 'Start selected TCP and UDP server(s)'
332
        print 'Start selected TCP and UDP server(s)'
337
        # server threads launch
333
        # server threads launch
338
       
334
       
339
        print 'CanDaemon v1 ready'
335
        print 'CanDaemon v1 ready'
340
        print 'Enter command or type \"help\" for a list of commands'
336
        print 'Enter command or type \"help\" for a list of commands'
341
        while not self.terminated:
337
        while not self.terminated:
342
            input = raw_input('> ')
338
            input = raw_input('> ')
343
            self.parseCommand(input)
339
            self.parseCommand(input)
344
 
340
 
345
        print 'Shutdown'
341
        print 'Shutdown'
346
#        if self.nodeIf.running():
342
#        if self.nodeIf.running():
347
#            print 'Stopping If thread'
343
#            print 'Stopping If thread'
348
#            self.nodeIf.stop()
344
#            self.nodeIf.stop()
349
 
345
 
350
#        self.cfg.stateSpaceCfg.saveSpaces()
346
#        self.cfg.stateSpaceCfg.saveSpaces()
351
#        self.cfg.filterCfg.saveFilters()
347
#        self.cfg.filterCfg.saveFilters()
352
       
348
       
353
        print 'Waiting 0.1..'
349
        print 'Waiting 0.1..'
354
        time.sleep(0.1) # wait for threads
350
        time.sleep(0.1) # wait for threads
355
        print 'Terminating'
351
        print 'Terminating'
356
        sys.exit(0) # will except if there are threads remaining
352
        sys.exit(0) # will except if there are threads remaining
357
 
353
 
358
#---------------------------------------------------------------------------
354
#---------------------------------------------------------------------------
359
 
355
 
360
def main():
356
def main():
361
    daemonApp = CanDaemon()
357
    daemonApp = CanDaemon()
362
#    install_thread_excepthook()
358
#    install_thread_excepthook()
363
    daemonApp.run()
359
    daemonApp.run()
364
 
360
 
365
#---------------------------------------------------------------------------
361
#---------------------------------------------------------------------------
366
 
362
 
367
if __name__ == '__main__':
363
if __name__ == '__main__':
368
    __name__ = 'Main'
364
    __name__ = 'Main'
369
    main()
365
    main()
370
 
366