Changeset 26
- Timestamp:
- 02/03/10 23:05:48 (12 years ago)
- Location:
- rc
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
rc/puzzlebox_brainstorms_client.py
r25 r26 11 11 # For more information please refer to http://www.gnu.org/copyleft/gpl.htm 12 12 # 13 # Last Update: 2010.02.0 213 # Last Update: 2010.02.03 14 14 # 15 15 ##################################################################### … … 49 49 self.server_host = server_host 50 50 self.server_port = server_port 51 #self.service_name = service_name52 #self.target_name = target_name53 51 self.max_connection_attempts = max_connection_attempts 54 52 … … 115 113 self.factory.noReply.cancel() 116 114 except: 117 self.factory.log.error("dataReceived after noReply triggered (or cancelled)") 115 if self.DEBUG: 116 print "dataReceived after noReply triggered (or cancelled)" 117 #self.factory.log.error("dataReceived after noReply triggered (or cancelled)") 118 118 119 119 self.data_chunk += data … … 122 122 reply = pickle.loads(self.data_chunk) 123 123 except Exception, e: 124 self.factory.log.error("Partial data received (or error: %s)." % e) 124 if self.DEBUG: 125 print "Partial data received (or error: %s)." % e 126 #self.factory.log.error("Partial data received (or error: %s)." % e) 125 127 else: 126 128 self.data_chunk = "" … … 142 144 143 145 class puzzlebox_brainstorms_client_send_command_factory(protocol.ClientFactory): 144 146 145 147 def __init__(self, log, \ 146 148 command, \ … … 173 175 (self.server_host, self.server_port) 174 176 175 reply ='FAILED_TO_CONNECT'177 reply = 'FAILED_TO_CONNECT' 176 178 177 179 self.connection_attempt = self.connection_attempt + 1 … … 179 181 if ((self.max_connection_attempts == None) or \ 180 182 (self.connection_attempt <= self.max_connection_attempts)): 181 183 182 184 # If connection failed retry after one second 183 185 reactor.callLater(1, connector.connect) … … 278 280 server_port=SERVER_PORT, \ 279 281 DEBUG=DEBUG) 282 280 283 reactor.callWhenRunning(client.execute_command_line) 281 284 reactor.run() -
rc/puzzlebox_brainstorms_client_interface.py
r25 r26 11 11 # For more information please refer to http://www.gnu.org/copyleft/gpl.htm 12 12 # 13 # Last Update: 2010.02.0 213 # Last Update: 2010.02.03 14 14 # 15 15 ##################################################################### … … 25 25 26 26 import puzzlebox_brainstorms_configuration as configuration 27 import puzzlebox_brainstorms_client 27 import puzzlebox_brainstorms_client as client 28 28 import puzzlebox_brainstorms_client_interface_component as component 29 29 import puzzlebox_brainstorms_client_interface_button as button … … 58 58 class puzzlebox_brainstorms_client_interface( \ 59 59 component.puzzlebox_brainstorms_client_interface_component, \ 60 protocol.ClientFactory):60 client.puzzlebox_brainstorms_client): 61 61 62 62 def __init__(self, log, \ … … 103 103 #configuration.SERVER_PORT, \ 104 104 #configuration.MAX_CONNECTION_ATTEMPTS) 105 106 self.server_host = configuration.SERVER_HOST 107 self.server_port = configuration.SERVER_PORT 108 self.max_connection_attempts = configuration.MAX_CONNECTION_ATTEMPTS 105 109 106 110 … … 228 232 229 233 def send_commands_to_server(self, commands): 230 234 231 235 for command in commands: 232 236 237 d = self.send_command(command) 238 239 233 240 #Command Line Client version 234 command_line = 'python puzzlebox_brainstorms_client.py %s' % command 235 236 if self.DEBUG: 237 print "executing: %s" % command_line 238 239 os.system(command_line) 240 241 242 ################################################################## 243 244 def start(self): 245 246 update_screen_on_next_pass = False 247 clock = self.pygame.time.Clock() 248 249 while 1: 250 251 if (update_screen_on_next_pass): 252 # Redraw Background 253 self.draw_background() 241 #command_line = 'python puzzlebox_brainstorms_client.py %s' % command 242 243 #if self.DEBUG: 244 #print "executing: %s" % command_line 245 246 #os.system(command_line) 247 248 249 ################################################################## 250 251 def print_response_and_stop(self, response): 252 253 if self.DEBUG: 254 print "---> [Client] Server Response:", 255 print response 256 257 #try: 258 #reactor.stop() 259 #except: 260 #print "ERROR: Can't stop reactor that isn't running." 261 262 263 ################################################################## 264 265 def update_screen(self): 266 267 print "here" 268 269 # Redraw Background 270 self.draw_background() 271 272 for each in self.buttons: 273 each.activated = False 274 each.display() 275 276 277 self.update_display() 278 279 280 ################################################################## 281 282 def test(self): 283 284 print "test" 285 286 287 ################################################################## 288 289 def checkEvents(self): 290 291 self.test() 292 293 # Redraw Background 294 self.draw_background() 295 296 for each in self.buttons: 297 each.activated = False 298 each.display() 299 300 301 self.update_display() 302 303 commands = [] 304 305 for event in self.pygame.event.get(): 306 307 if (event.type is self.pygame.locals.QUIT): 308 reactor.stop() 309 sys.exit() 310 311 312 elif (event.type == self.pygame.locals.KEYDOWN): 254 313 255 for each in self.buttons: 256 each.activated = False 257 each.display() 258 259 260 self.update_display() 261 262 update_screen_on_next_pass = False 263 264 265 commands = [] 266 267 for event in self.pygame.event.get(): 268 269 if (event.type is self.pygame.locals.QUIT): 270 #reactor.stop() 271 sys.exit() 272 273 274 elif (event.type == self.pygame.locals.KEYDOWN): 275 276 if 'key' in event.dict.keys(): 277 278 if (event.dict['key'] == self.pygame.K_ESCAPE): 279 sys.exit() 280 281 elif ((event.dict['key'] == self.pygame.K_UP) or \ 282 (event.dict['key'] == self.pygame.K_w)): 283 command = 'drive_forward' 284 self.update_button(command) 285 commands.append(command) 286 287 elif ((event.dict['key'] == self.pygame.K_DOWN) or \ 288 (event.dict['key'] == self.pygame.K_s) or \ 289 (event.dict['key'] == self.pygame.K_x)): 290 command = 'drive_reverse' 291 self.update_button(command) 292 commands.append(command) 293 294 elif ((event.dict['key'] == self.pygame.K_LEFT) or \ 295 (event.dict['key'] == self.pygame.K_a) or \ 296 (event.dict['key'] == self.pygame.K_q)): 297 command = 'turn_left' 298 self.update_button(command) 299 commands.append(command) 300 301 elif ((event.dict['key'] == self.pygame.K_RIGHT) or \ 302 (event.dict['key'] == self.pygame.K_d) or \ 303 (event.dict['key'] == self.pygame.K_e)): 304 command = 'turn_right' 305 self.update_button(command) 306 commands.append(command) 307 308 elif (event.dict['key'] == self.pygame.K_z): 309 command = 'turn_left_in_reverse' 310 self.update_button(command) 311 commands.append(command) 312 313 elif ((event.dict['key'] == self.pygame.K_PAGEDOWN) or \ 314 (event.dict['key'] == self.pygame.K_c)): 315 command = 'turn_right_in_reverse' 316 self.update_button(command) 317 commands.append(command) 318 319 320 if commands != []: 321 self.update_display() 322 self.send_commands_to_server(commands) 323 update_screen_on_next_pass = True 324 325 326 clock.tick(60) 314 if 'key' in event.dict.keys(): 315 316 if (event.dict['key'] == self.pygame.K_ESCAPE): 317 sys.exit() 318 319 elif ((event.dict['key'] == self.pygame.K_UP) or \ 320 (event.dict['key'] == self.pygame.K_w)): 321 command = 'drive_forward' 322 self.update_button(command) 323 commands.append(command) 324 325 elif ((event.dict['key'] == self.pygame.K_DOWN) or \ 326 (event.dict['key'] == self.pygame.K_s) or \ 327 (event.dict['key'] == self.pygame.K_x)): 328 command = 'drive_reverse' 329 self.update_button(command) 330 commands.append(command) 331 332 elif ((event.dict['key'] == self.pygame.K_LEFT) or \ 333 (event.dict['key'] == self.pygame.K_a) or \ 334 (event.dict['key'] == self.pygame.K_q)): 335 command = 'turn_left' 336 self.update_button(command) 337 commands.append(command) 338 339 elif ((event.dict['key'] == self.pygame.K_RIGHT) or \ 340 (event.dict['key'] == self.pygame.K_d) or \ 341 (event.dict['key'] == self.pygame.K_e)): 342 command = 'turn_right' 343 self.update_button(command) 344 commands.append(command) 345 346 elif (event.dict['key'] == self.pygame.K_z): 347 command = 'turn_left_in_reverse' 348 self.update_button(command) 349 commands.append(command) 350 351 elif ((event.dict['key'] == self.pygame.K_PAGEDOWN) or \ 352 (event.dict['key'] == self.pygame.K_c)): 353 command = 'turn_right_in_reverse' 354 self.update_button(command) 355 commands.append(command) 356 357 358 if commands != []: 359 self.update_display() 360 self.send_commands_to_server(commands) 361 #self.update_screen() 362 363 364 reactor.callLater(0.03, self.checkEvents) 327 365 328 366 … … 379 417 DEBUG=DEBUG) 380 418 381 interface.start() 382 383 reactor.callWhenRunning(interface.start) 384 #print "bing" 419 reactor.callWhenRunning(interface.checkEvents) 385 420 reactor.run() 386 421 -
rc/puzzlebox_brainstorms_configuration.py
r23 r26 50 50 ##################################################################### 51 51 52 #SERVER_HOST = '127.0.0.1' 52 SERVER_INTERFACE = '' # listen on all of server's network interfaces 53 53 SERVER_HOST = '192.168.1.150' 54 #SERVER_HOST = ''55 54 SERVER_PORT = 8194 56 55 -
rc/puzzlebox_brainstorms_server.py
r25 r26 11 11 # For more information please refer to http://www.gnu.org/copyleft/gpl.htm 12 12 # 13 # Last Update: 2010.02.0 213 # Last Update: 2010.02.03 14 14 # 15 15 ##################################################################### … … 30 30 DEBUG = 1 31 31 32 SERVER_ HOST = configuration.SERVER_HOST32 SERVER_INTERFACE = configuration.SERVER_INTERFACE 33 33 SERVER_PORT = configuration.SERVER_PORT 34 34 … … 41 41 def __init__(self, log, DEBUG=DEBUG): 42 42 43 self.protocol = puzzlebox_brainstorms_server_protocol44 45 43 self.log = log 46 44 self.DEBUG = DEBUG 47 45 48 self. registry = {}46 self.protocol = puzzlebox_brainstorms_server_protocol 49 47 50 48 … … 58 56 59 57 if self.DEBUG: 60 print '--> [Server] %s command received' % command58 print '--> [Server] %s' % response 61 59 62 command = 'python puzzlebox_brainstorms_rc.py --command=%s' % command 63 os.system(command) 60 61 command_line = 'python puzzlebox_brainstorms_rc.py --command=%s' % command 62 63 os.system(command_line) 64 64 65 65 … … 150 150 151 151 # Collect default settings and command line parameters 152 server_ host = SERVER_HOST152 server_interface = SERVER_INTERFACE 153 153 server_port = SERVER_PORT 154 154 155 155 for each in sys.argv: 156 156 157 if each.startswith("-- host="):158 server_ host = each[ len("--host="): ]157 if each.startswith("--interface="): 158 server_interface = each[ len("--interface="): ] 159 159 if each.startswith("--port="): 160 160 server_port = each[ len("--port="): ] 161 161 162 162 163 mcp = puzzlebox_brainstorms_server(log, DEBUG) 164 reactor.listenTCP(port=server_port, factory=mcp, interface=server_host) 163 server = puzzlebox_brainstorms_server(log, DEBUG=DEBUG) 164 165 if DEBUG: 166 print "--> [Server] Initializing server on %s:%i" % \ 167 (server_interface, server_port) 168 169 reactor.listenTCP(interface=server_interface, port=server_port, factory=server) 170 165 171 reactor.run() 166 172
Note: See TracChangeset
for help on using the changeset viewer.