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