Rev 747 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 747 | Rev 749 | ||
|---|---|---|---|
| 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 | - | ||
| 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 |
| 17 | 14 | ||
| 18 | from DaemonConfig import DaemonConfig |
15 | from DaemonConfig import DaemonConfig |
| 19 | 16 | ||
| 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 = {} |
| 31 | CMDLIST_NAME = 0 |
27 | CMDLIST_NAME = 0 |
| 32 | CMDLIST_HANDLER = 1 |
28 | CMDLIST_HANDLER = 1 |
| 33 | CMDLIST_PARAMINFO = 2 |
29 | CMDLIST_PARAMINFO = 2 |
| 34 | CMDLIST_HELP = 3 |
30 | CMDLIST_HELP = 3 |
| 35 | 31 | ||
| 36 | logHelp = 'usage:\t log [options]\n\n' \ |
32 | logHelp = 'usage:\t log [options]\n\n' \ |
| 37 | '\t options:\n' \ |
33 | '\t options:\n' \ |
| 38 | '\t level <num>, where: 0 < num < 3\n' |
34 | '\t level <num>, where: 0 < num < 3\n' |
| 39 | stateHelp = 'usage: state [name/add/rem]' |
35 | stateHelp = 'usage: state [name/add/rem]' |
| 40 | filterHelp = 'usage: filter [name/add/rem]' |
36 | filterHelp = 'usage: filter [name/add/rem]' |
| 41 | addifHelp = 'usage: addif [name] [options]' |
37 | addifHelp = 'usage: addif [name] [options]' |
| 42 | addbridgeHelp = 'usage: addbridge [interface names]' |
38 | addbridgeHelp = 'usage: addbridge [interface names]' |
| 43 | tcpdHelp = 'usage: tcpd [start/stop/configure] [options]' |
39 | tcpdHelp = 'usage: tcpd [start/stop/configure] [options]' |
| 44 | tlsdHelp = 'usage: tlsd [start/stop/configure] [options]' |
40 | tlsdHelp = 'usage: tlsd [start/stop/configure] [options]' |
| 45 | canctldHelp = 'usage: canctld [start/stop/configure] [options]' |
41 | canctldHelp = 'usage: canctld [start/stop/configure] [options]' |
| 46 | simHelp = 'usage: sim [record/play/add/rem/save/load] [options]' |
42 | simHelp = 'usage: sim [record/play/add/rem/save/load] [options]' |
| 47 | help_command_map = {} |
43 | help_command_map = {} |
| 48 | 44 | ||
| 49 | logOptions = {} |
45 | logOptions = {} |
| 50 | ifOptions = {} |
46 | ifOptions = {} |
| 51 | bridgeOptions = {} |
47 | bridgeOptions = {} |
| 52 | tcpdOptions = {} |
48 | tcpdOptions = {} |
| 53 | tlsdOptions = {} |
49 | tlsdOptions = {} |
| 54 | canctldOptions = {} |
50 | canctldOptions = {} |
| 55 | simOptions = {} |
51 | simOptions = {} |
| 56 | 52 | ||
| 57 | logValidOptions = {'level' : ['range', 0, 3]} |
53 | logValidOptions = {'level' : ['range', 0, 3]} |
| 58 | ifValidOptions = {'type' : ['alt', 'serial','udp']} |
54 | ifValidOptions = {'type' : ['alt', 'serial','udp']} |
| 59 | 55 | ||
| 60 | ifSerialOptions = {'' : []} |
56 | ifSerialOptions = {'' : []} |
| 61 | ifCanStimOptions = {'' : []} |
57 | ifCanStimOptions = {'' : []} |
| 62 | ifTelnetOptions = {'' : []} |
58 | ifTelnetOptions = {'' : []} |
| 63 | ifTcpTlsOptions = {'' : []} |
59 | ifTcpTlsOptions = {'' : []} |
| 64 | ifUDPOptions = {'' : []} |
60 | ifUDPOptions = {'' : []} |
| 65 | 61 | ||
| 66 | bridgeValidOptions = {'' : ['']} |
62 | bridgeValidOptions = {'' : ['']} |
| 67 | tcpdValidOptions = {'' : []} |
63 | tcpdValidOptions = {'' : []} |
| 68 | tlsdValidOptions = {'' : []} |
64 | tlsdValidOptions = {'' : []} |
| 69 | canctldValidOptions = {'' : []} |
65 | canctldValidOptions = {'' : []} |
| 70 | simValidOptions = {'' : []} |
66 | simValidOptions = {'' : []} |
| 71 | 67 | ||
| 72 | def __init__ (self): |
68 | def __init__ (self): |
| 73 | 69 | ||
| 74 | self.command_list = [['help', self.helpCmd, '', 'display help'], |
70 | self.command_list = [['help', self.helpCmd, '', 'display help'], |
| 75 | ['log', self.logCmd, '<options>', 'log control: \"help log\"'], |
71 | ['log', self.logCmd, '<options>', 'log control: \"help log\"'], |
| 76 | ['exec', self.execCmd, '<filename>', 'run command listing\n'], |
72 | ['exec', self.execCmd, '<filename>', 'run command listing\n'], |
| 77 | ['filter', self.filterCmd, '<cmd>', 'filter commands: \"help filter\"'], |
73 | ['filter', self.filterCmd, '<cmd>', 'filter commands: \"help filter\"'], |
| 78 | ['state', self.stateCmd, '<cmd>', 'state commands: \"help state\"\n'], |
74 | ['state', self.stateCmd, '<cmd>', 'state commands: \"help state\"\n'], |
| 79 | ['addif', self.ifAddCmd, '<options>', 'add interface: \"help addif\"'], |
75 | ['addif', self.ifAddCmd, '<options>', 'add interface: \"help addif\"'], |
| 80 | ['remif', self.ifRemCmd, '<name>', 'remove interface'], |
76 | ['remif', self.ifRemCmd, '<name>', 'remove interface'], |
| 81 | ['ifup', self.ifUpCmd, '<name>', 'bring up interface'], |
77 | ['ifup', self.ifUpCmd, '<name>', 'bring up interface'], |
| 82 | ['ifdown', self.ifDownCmd, '<name>', 'bring down interface\n'], |
78 | ['ifdown', self.ifDownCmd, '<name>', 'bring down interface\n'], |
| 83 | ['addbrigde', self.addBridgeCmd, '<options>', 'add bridge: \"help addbridge\"'], |
79 | ['addbrigde', self.addBridgeCmd, '<options>', 'add bridge: \"help addbridge\"'], |
| 84 | ['rembridge', self.remBridgeCmd, '<id>', 'remove bridge\n'], |
80 | ['rembridge', self.remBridgeCmd, '<id>', 'remove bridge\n'], |
| 85 | ['tcpd', self.tcpdCmd, '<cmd>', 'tcpd server control: \"help tcpd\"'], |
81 | ['tcpd', self.tcpdCmd, '<cmd>', 'tcpd server control: \"help tcpd\"'], |
| 86 | ['tlsd', self.tlsdCmd, '<cmd>', 'tlsd server control: \"help tlsd\"'], |
82 | ['tlsd', self.tlsdCmd, '<cmd>', 'tlsd server control: \"help tlsd\"'], |
| 87 | ['canctld', self.canCtldCmd, '<cmd>', 'canctld server control: \"help canctld\"\n'], |
83 | ['canctld', self.canCtldCmd, '<cmd>', 'canctld server control: \"help canctld\"\n'], |
| 88 | ['status', self.statusCmd, '', 'show daemon status'], |
84 | ['status', self.statusCmd, '', 'show daemon status'], |
| 89 | ['stats', self.statsCmd, '', 'show statistics'], |
85 | ['stats', self.statsCmd, '', 'show statistics'], |
| 90 | ['nodelist', self.nodesCmd, '', 'list can nodes\n'], |
86 | ['nodelist', self.nodesCmd, '', 'list can nodes\n'], |
| 91 | ['sim', self.simCmd, '<cmd>', 'simulator commands: \"help sim\"']] |
87 | ['sim', self.simCmd, '<cmd>', 'simulator commands: \"help sim\"']] |
| 92 | 88 | ||
| 93 | index = 0 |
89 | index = 0 |
| 94 | for cmd in self.command_list: |
90 | for cmd in self.command_list: |
| 95 | self.command_map[cmd[0]] = index |
91 | self.command_map[cmd[0]] = index |
| 96 | index += 1 |
92 | index += 1 |
| 97 | 93 | ||
| 98 | self.help_command_map = {'log' : self.logHelp, |
94 | self.help_command_map = {'log' : self.logHelp, |
| 99 | 'filter' : self.filterHelp, |
95 | 'filter' : self.filterHelp, |
| 100 | 'state' : self.stateHelp, |
96 | 'state' : self.stateHelp, |
| 101 | 'addif' : self.addifHelp, |
97 | 'addif' : self.addifHelp, |
| 102 | 'addbridge' : self.addbridgeHelp, |
98 | 'addbridge' : self.addbridgeHelp, |
| 103 | 'tcpd' : self.tcpdHelp, |
99 | 'tcpd' : self.tcpdHelp, |
| 104 | 'tlsd' : self.tlsdHelp, |
100 | 'tlsd' : self.tlsdHelp, |
| 105 | 'canctld' : self.canctldHelp, |
101 | 'canctld' : self.canctldHelp, |
| 106 | 'sim' : self.simHelp} |
102 | 'sim' : self.simHelp} |
| 107 | 103 | ||
| 108 | self.cfg = DaemonConfig() |
104 | self.cfg = DaemonConfig() |
| 109 | 105 | ||
| 110 | class IfNotifier(): |
106 | class IfNotifier(): |
| 111 | """ Used by if threads to send messages to the main program thread """ |
107 | """ Used by if threads to send messages to the main program thread """ |
| 112 | 108 | ||
| 113 | parent = None |
109 | parent = None |
| 114 | UNDEFINED = 0 |
110 | UNDEFINED = 0 |
| 115 | TERMINATE = 1 |
111 | TERMINATE = 1 |
| 116 | 112 | ||
| 117 | def __init__(self, parent): |
113 | def __init__(self, parent): |
| 118 | self.parent = parent |
114 | self.parent = parent |
| 119 | 115 | ||
| 120 | def notify(self, msg): |
116 | def notify(self, msg): |
| 121 | if msg == self.TERMINATE: |
117 | if msg == self.TERMINATE: |
| 122 | print 'Main terminate notify' |
118 | print 'Main terminate notify' |
| 123 | else: |
119 | else: |
| 124 | print 'UNDEFINED NOTIFY' |
120 | print 'UNDEFINED NOTIFY' |
| 125 | 121 | ||
| 126 | 122 | ||
| 127 | def __rightAdjust(self, refstr, str): |
123 | def __rightAdjust(self, refstr, str): |
| 128 | spcstr = '' |
124 | spcstr = '' |
| 129 | endpos = 25-len(refstr) |
125 | endpos = 25-len(refstr) |
| 130 | for i in range(1,endpos): |
126 | for i in range(1,endpos): |
| 131 | if i == endpos-5: |
127 | if i == endpos-5: |
| 132 | spcstr += ':' |
128 | spcstr += ':' |
| 133 | else: |
129 | else: |
| 134 | spcstr += ' ' |
130 | spcstr += ' ' |
| 135 | return (spcstr+str) |
131 | return (spcstr+str) |
| 136 | 132 | ||
| 137 | def helpCmd(self, *args): |
133 | def helpCmd(self, *args): |
| 138 | try: |
134 | try: |
| 139 | args = args[0] |
135 | args = args[0] |
| 140 | except: |
136 | except: |
| 141 | args = [] |
137 | args = [] |
| 142 | if len(args) < 1: |
138 | if len(args) < 1: |
| 143 | quitStr = 'quit, exit' |
139 | quitStr = 'quit, exit' |
| 144 | print quitStr, self.__rightAdjust(quitStr, 'shutdown pyCanDaemon') |
140 | print quitStr, self.__rightAdjust(quitStr, 'shutdown pyCanDaemon') |
| 145 | for cmd in self.command_list: |
141 | for cmd in self.command_list: |
| 146 | cmdstr = cmd[self.CMDLIST_NAME] + ' ' + cmd[self.CMDLIST_PARAMINFO] |
142 | cmdstr = cmd[self.CMDLIST_NAME] + ' ' + cmd[self.CMDLIST_PARAMINFO] |
| 147 | print cmdstr, \ |
143 | print cmdstr, \ |
| 148 | self.__rightAdjust(cmdstr, cmd[self.CMDLIST_HELP]) |
144 | self.__rightAdjust(cmdstr, cmd[self.CMDLIST_HELP]) |
| 149 | elif len(args) < 2: |
145 | elif len(args) < 2: |
| 150 | subCmd = args[0] |
146 | subCmd = args[0] |
| 151 | if self.help_command_map.has_key(subCmd): |
147 | if self.help_command_map.has_key(subCmd): |
| 152 | print self.help_command_map[subCmd] |
148 | print self.help_command_map[subCmd] |
| 153 | else: |
149 | else: |
| 154 | print 'no help for ' + subCmd |
150 | print 'no help for ' + subCmd |
| 155 | else: |
151 | else: |
| 156 | print 'usage: help [topic]' |
152 | print 'usage: help [topic]' |
| 157 | 153 | ||
| 158 | def filterCmd(self, *args): |
154 | def filterCmd(self, *args): |
| 159 | try: |
155 | try: |
| 160 | args = args[0] |
156 | args = args[0] |
| 161 | except: |
157 | except: |
| 162 | args = [] |
158 | args = [] |
| 163 | log.debug('filterCmd ' + str(args)) |
159 | log.debug('filterCmd ' + str(args)) |
| 164 | if len(args) < 2: |
160 | if len(args) < 2: |
| 165 | print 'Invalid arguments\n' + self.filterHelp |
161 | print 'Invalid arguments\n' + self.filterHelp |
| 166 | return |
162 | return |
| 167 | cmd = args[0] |
163 | cmd = args[0] |
| 168 | filterName = args[1] |
164 | filterName = args[1] |
| 169 | # FIXME: verify |
165 | # FIXME: verify |
| 170 | if cmd == 'add': |
166 | if cmd == 'add': |
| 171 | print 'Importing filter: ' + filterName |
167 | print 'Importing filter: ' + filterName |
| 172 | self.cfg.filterCfg.importFilter(filterName) |
168 | self.cfg.filterCfg.importFilter(filterName) |
| 173 | elif cmd == 'rem' or cmd == 'remove': |
169 | elif cmd == 'rem' or cmd == 'remove': |
| 174 | print 'Deleting filter: ' + filterName |
170 | print 'Deleting filter: ' + filterName |
| 175 | # FIXME: actually delete |
171 | # FIXME: actually delete |
| 176 | raise 'Not implemented' |
172 | raise 'Not implemented' |
| 177 | 173 | ||
| 178 | def stateCmd(self, *args): |
174 | def stateCmd(self, *args): |
| 179 | try: |
175 | try: |
| 180 | args = args[0] |
176 | args = args[0] |
| 181 | except: |
177 | except: |
| 182 | args = [] |
178 | args = [] |
| 183 | log.debug('stateCmd ' + str(args)) |
179 | log.debug('stateCmd ' + str(args)) |
| 184 | if len(args) < 2: |
180 | if len(args) < 2: |
| 185 | print 'Invalid arguments\n' + self.stateHelp |
181 | print 'Invalid arguments\n' + self.stateHelp |
| 186 | return |
182 | return |
| 187 | cmd = args[0] |
183 | cmd = args[0] |
| 188 | spaceName = args[1] |
184 | spaceName = args[1] |
| 189 | # FIXME: verify |
185 | # FIXME: verify |
| 190 | if cmd == 'add': |
186 | if cmd == 'add': |
| 191 | print 'Importing state space: ' + spaceName |
187 | print 'Importing state space: ' + spaceName |
| 192 | self.cfg.stateSpaceCfg.importSpace(spaceName) |
188 | self.cfg.stateSpaceCfg.importSpace(spaceName) |
| 193 | elif cmd == 'rem' or cmd == 'remove': |
189 | elif cmd == 'rem' or cmd == 'remove': |
| 194 | print 'Deleting state space: ' + spaceName |
190 | print 'Deleting state space: ' + spaceName |
| 195 | # FIXME: actually delete statespace |
191 | # FIXME: actually delete statespace |
| 196 | raise 'Not implemented' |
192 | raise 'Not implemented' |
| 197 | 193 | ||
| 198 | def ifAddCmd(self, *args): |
194 | def ifAddCmd(self, *args): |
| 199 | raise 'not implemented' |
195 | raise 'not implemented' |
| 200 | 196 | ||
| 201 | def ifRemCmd(self, *args): |
197 | def ifRemCmd(self, *args): |
| 202 | raise 'not implemented' |
198 | raise 'not implemented' |
| 203 | 199 | ||
| 204 | def ifUpCmd(self, *args): |
200 | def ifUpCmd(self, *args): |
| 205 | raise 'not implemented' |
201 | raise 'not implemented' |
| 206 | 202 | ||
| 207 | def ifDownCmd(self, *args): |
203 | def ifDownCmd(self, *args): |
| 208 | raise 'not implemented' |
204 | raise 'not implemented' |
| 209 | 205 | ||
| 210 | def addBridgeCmd(self, *args): |
206 | def addBridgeCmd(self, *args): |
| 211 | raise 'not implemented' |
207 | raise 'not implemented' |
| 212 | 208 | ||
| 213 | def remBridgeCmd(self, *args): |
209 | def remBridgeCmd(self, *args): |
| 214 | raise 'not implemented' |
210 | raise 'not implemented' |
| 215 | 211 | ||
| 216 | def tcpdCmd(self, *args): |
212 | def tcpdCmd(self, *args): |
| 217 | raise 'not implemented' |
213 | raise 'not implemented' |
| 218 | 214 | ||
| 219 | def tlsdCmd(self, *args): |
215 | def tlsdCmd(self, *args): |
| 220 | raise 'not implemented' |
216 | raise 'not implemented' |
| 221 | 217 | ||
| 222 | def canCtldCmd(self, *args): |
218 | def canCtldCmd(self, *args): |
| 223 | raise 'not implemented' |
219 | raise 'not implemented' |
| 224 | 220 | ||
| 225 | def statusCmd(self, *args): |
221 | def statusCmd(self, *args): |
| 226 | raise 'not implemented' |
222 | raise 'not implemented' |
| 227 | 223 | ||
| 228 | def statsCmd(self, *args): |
224 | def statsCmd(self, *args): |
| 229 | raise 'not implemented' |
225 | raise 'not implemented' |
| 230 | 226 | ||
| 231 | def nodesCmd(self, *args): |
227 | def nodesCmd(self, *args): |
| 232 | raise 'not implemented' |
228 | raise 'not implemented' |
| 233 | 229 | ||
| 234 | def simCmd(self, *args): |
230 | def simCmd(self, *args): |
| 235 | raise 'not implemented' |
231 | raise 'not implemented' |
| 236 | 232 | ||
| 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 |
| 248 | cmd[0] = cmd[0].lower() |
244 | cmd[0] = cmd[0].lower() |
| 249 | if cmd[0] == 'quit' or cmd[0] == 'exit': |
245 | if cmd[0] == 'quit' or cmd[0] == 'exit': |
| 250 | self.terminated = True |
246 | self.terminated = True |
| 251 | elif self.command_map.has_key(cmd[0]): |
247 | elif self.command_map.has_key(cmd[0]): |
| 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) |
| 272 | except: |
269 | except: |
| 273 | return |
270 | return |
| 274 | 271 | ||
| 275 | print 'INIT: Start logging' |
272 | print 'INIT: Start logging' |
| 276 | log.basicConfig(level=log.DEBUG) |
273 | log.basicConfig(level=log.DEBUG) |
| 277 | 274 | ||
| 278 | """command parameters only dictate verbosity and use of config file""" |
275 | """command parameters only dictate verbosity and use of config file""" |
| 279 | print 'INIT: Get config options from command line / config file' |
276 | print 'INIT: Get config options from command line / config file' |
| 280 | try: |
277 | try: |
| 281 | opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="]) |
278 | opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="]) |
| 282 | except getopt.GetoptError: |
279 | except getopt.GetoptError: |
| 283 | print 'Invalid usage' |
280 | print 'Invalid usage' |
| 284 | usage() |
281 | usage() |
| 285 | sys.exit(2) |
282 | sys.exit(2) |
| 286 | # output = None |
283 | # output = None |
| 287 | # verbose = False |
284 | # verbose = False |
| 288 | for o, a in opts: |
285 | for o, a in opts: |
| 289 | if o == "-v": |
286 | if o == "-v": |
| 290 | pass |
287 | pass |
| 291 | # verbose = True |
288 | # verbose = True |
| 292 | if o in ("-h", "--help"): |
289 | if o in ("-h", "--help"): |
| 293 | usage() |
290 | usage() |
| 294 | sys.exit() |
291 | sys.exit() |
| 295 | if o in ("-o", "--output"): |
292 | if o in ("-o", "--output"): |
| 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 |
| 353 | 338 | ||
| 354 | #--------------------------------------------------------------------------- |
339 | #--------------------------------------------------------------------------- |
| 355 | 340 | ||
| 356 | def main(): |
341 | def main(): |
| 357 | daemonApp = CanDaemon() |
342 | daemonApp = CanDaemon() |
| 358 | # install_thread_excepthook() |
343 | # install_thread_excepthook() |
| 359 | daemonApp.run() |
344 | daemonApp.run() |
| 360 | 345 | ||
| 361 | #--------------------------------------------------------------------------- |
346 | #--------------------------------------------------------------------------- |
| 362 | 347 | ||
| 363 | if __name__ == '__main__': |
348 | if __name__ == '__main__': |
| 364 | __name__ = 'Main' |
349 | __name__ = 'Main' |
| 365 | main() |
350 | main() |
| 366 | 351 | ||