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