Changeset 6
- Timestamp:
- 01/28/10 15:31:34 (12 years ago)
- Location:
- rc
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
rc/puzzlebox_brainstorms_client.py
r5 r6 21 21 22 22 import puzzlebox_brainstorms_configuration as configuration 23 #import puzzlebox_common_operations as common_operations24 23 #import puzzlebox_logger 25 26 24 27 25 ##################################################################### … … 33 31 34 32 MAX_CONNECTION_ATTEMPTS = configuration.MAX_CONNECTION_ATTEMPTS 35 #HEALTH_CHECK_CONNECTION_ATTEMPTS = puzzlebox_configuration.HEALTH_CHECK_CONNECTION_ATTEMPTS36 33 NO_REPLY_WAIT = configuration.NO_REPLY_WAIT 37 34 … … 46 43 47 44 class puzzlebox_brainstorms_client: 48 45 49 46 def __init__(self, log, hostname, port, \ 50 47 service_name = 'MCC', \ 51 48 target_name = 'MCP', \ 52 49 max_connection_attempts = MAX_CONNECTION_ATTEMPTS): 53 50 54 51 self.log = log 55 52 self.hostname = hostname … … 58 55 self.target_name = target_name 59 56 self.max_connection_attempts = max_connection_attempts 60 61 62 ################################################################## 63 57 58 59 ################################################################## 60 64 61 def test_drive(self): 65 62 66 63 instruction = {} 67 64 instruction['command'] = 'test_drive' 68 65 69 66 #self.log.debug("Requesting to hide_all_components") 70 67 71 68 return self.send_instruction(instruction) 72 73 74 ################################################################## 75 76 #def hide_all_components(self): 77 78 #instruction = {} 79 #instruction['command'] = 'hide_all_components' 80 81 #self.log.debug("Requesting to hide_all_components") 82 83 #return self.send_instruction(instruction) 84 85 86 ################################################################## 87 88 #def register_window(self, window_id): 89 90 #instruction = {} 91 #instruction['command'] = 'register_window' 92 #instruction['information'] = {} 93 #instruction['information']['window_id'] = window_id 94 95 #return self.send_instruction(instruction) 96 97 98 ################################################################## 99 100 #def get_window_data(self, window_id): 101 102 #instruction = {} 103 #instruction['command'] = 'get_window_data' 104 #instruction['information'] = {} 105 #instruction['information']['window_id'] = window_id 106 107 #return self.send_instruction(instruction) 108 109 110 ################################################################## 111 112 #def get_registry(self): 113 114 #registry = {} 115 116 #instruction = {} 117 #instruction['command'] = 'get_registry' 118 #instruction['information'] = {} 119 120 #return self.send_instruction(instruction) 121 122 123 ################################################################## 124 125 #def unregister_all_windows(self): 126 127 #instruction = {} 128 #instruction['command'] = 'unregister_all_windows' 129 #instruction['information'] = {} 130 131 #return self.send_instruction(instruction) 132 133 134 ################################################################## 135 136 #def unregister_component(self, component): 137 138 #instruction = {} 139 #instruction['command'] = 'unregister_component' 140 #instruction['information'] = {} 141 #instruction['information']['pid'] = component['pid'] 142 143 #return self.send_instruction(instruction) 144 145 146 ################################################################## 147 148 #def restart_component(self, component): 149 150 #instruction = {} 151 #instruction['command'] = 'restart_component' 152 #instruction['information'] = {} 153 #instruction['information']['pid'] = component['pid'] 154 155 #return self.send_instruction(instruction) 156 157 158 ################################################################## 159 160 #def exit_content_set(self): 161 162 #instruction = {} 163 #instruction['command'] = 'exit_content_set' 164 #instruction['information'] = {} 165 166 #return self.send_instruction(instruction) 167 168 169 ################################################################## 170 171 #def change_content_set(self, data): 172 173 #instruction = {} 174 #instruction['command'] = 'change_content_set' 175 #instruction['information'] = {} 176 #instruction['information']['content_set_id'] = data['content_set_id'] 177 178 #return self.send_instruction(instruction) 179 180 181 ################################################################## 182 183 #def end_current_entry(self, data): 184 185 #instruction = {} 186 #instruction['command'] = 'end_current_entry' 187 #instruction['information'] = {} 188 #instruction['information']['window_id'] = data['window_id'] 189 190 #return self.send_instruction(instruction) 191 192 193 ################################################################## 194 195 #def request_screenshot(self): 196 197 #instruction = {} 198 #instruction['command'] = 'request_screenshot' 199 #instruction['information'] = {} 200 201 #return self.send_instruction(instruction) 202 203 204 ################################################################## 205 206 #def upload_screenshot(self): 207 208 #instruction = {} 209 #instruction['command'] = 'upload_screenshot' 210 #instruction['information'] = {} 211 212 #return self.send_instruction(instruction) 213 214 215 ################################################################## 216 217 #def export_schedule(self, data): 218 219 #instruction = {} 220 #instruction['command'] = 'export_schedule' 221 #instruction['information'] = {} 222 223 #for key in data.keys(): 224 #instruction['information'][key] = data[key] 225 226 #return self.send_instruction(instruction) 227 228 229 ################################################################## 230 231 #def check_health(self, component={}): 232 233 #instruction = {} 234 #instruction['command'] = 'check_health' 235 #instruction['information'] = {} 236 237 #return self.send_instruction(instruction, component, \ 238 #max_connection_attempts=HEALTH_CHECK_CONNECTION_ATTEMPTS) 239 240 241 ################################################################## 242 69 70 71 ################################################################## 72 243 73 def send_instruction(self, instruction, component=None, \ 244 74 max_connection_attempts=MAX_CONNECTION_ATTEMPTS): 245 75 246 76 if not component: 247 77 component = {} … … 250 80 component['source'] = self.service_name 251 81 component['target'] = self.target_name 252 82 253 83 factory = puzzlebox_brainstorms_client_send_instruction_factory(self.log, \ 254 84 component, \ 255 85 instruction, \ 256 86 max_connection_attempts) 257 87 258 88 reactor.connectTCP(component['hostname'], component['port'], factory) 259 89 260 90 return factory.replyDefer 261 91 … … 266 96 267 97 class mcc_send_instruction_protocol(protocol.Protocol): 268 98 269 99 def __init__(self): 270 100 self.data_chunk = "" 271 272 273 ################################################################## 274 101 102 103 ################################################################## 104 275 105 def connectionMade(self): 276 106 data = pickle.dumps(self.factory.instruction) 277 107 self.transport.write(data) 278 108 279 109 #self.factory.log.debug("%s sent '%s' to remote %s component at %s:%s " % \ 280 110 #(self.factory.component['source'], \ … … 283 113 #self.factory.component['hostname'], \ 284 114 #self.factory.component['port'])) 285 115 286 116 self.factory.noReply = reactor.callLater(NO_REPLY_WAIT, self.noReply) 287 288 289 ################################################################## 290 117 118 119 ################################################################## 120 291 121 def noReply(self): 292 122 293 123 #self.factory.log.error('No reply from %s for instruction %s' % \ 294 124 #(self.factory.component['target'], self.factory.instruction)) 295 125 296 126 try: 297 127 self.factory.replyDefer.callback(('NO_REPLY', self.factory.component)) 298 128 except: 299 129 self.factory.log.error("noReply failed to call callback?") 300 130 301 131 self.transport.loseConnection() 302 303 304 ################################################################## 305 132 133 134 ################################################################## 135 306 136 def dataReceived(self, data): 307 137 308 138 try: 309 139 self.factory.noReply.cancel() 310 140 except: 311 141 self.factory.log.error("dataReceived after noReply triggered (or cancelled)?!") 312 142 313 143 self.data_chunk += data 314 144 try: … … 318 148 else: 319 149 self.data_chunk = "" 320 150 321 151 #self.factory.log.debug('%s received reply from %s: %s' % \ 322 152 #(self.factory.component['source'], \ 323 153 #self.factory.component['target'], \ 324 154 #reply)) 325 155 326 156 try: 327 157 self.factory.replyDefer.callback((reply, self.factory.component)) … … 329 159 #self.factory.log.error("dataReceived failed to call callback?") 330 160 pass 331 161 332 162 self.transport.loseConnection() 333 163 … … 338 168 339 169 class puzzlebox_brainstorms_client_send_instruction_factory(protocol.ClientFactory): 340 170 341 171 def __init__(self, log, \ 342 172 component, \ 343 173 instruction, \ 344 174 max_connection_attempts=MAX_CONNECTION_ATTEMPTS): 345 175 346 176 self.protocol = mcc_send_instruction_protocol 347 177 self.log = log 348 178 self.component = component 349 179 self.instruction = instruction 350 180 351 181 self.max_connection_attempts = max_connection_attempts 352 182 self.connection_attempt = 1 353 183 354 184 self.replyDefer = defer.Deferred() 355 356 357 ################################################################## 358 185 186 187 ################################################################## 188 359 189 def clientConnectionFailed(self, connector, reason): 360 190 … … 364 194 #self.component['hostname'], \ 365 195 #self.component['port'])) 366 196 367 197 reply='FAILED_TO_CONNECT' 368 198 369 199 self.connection_attempt = self.connection_attempt + 1 370 200 371 201 if self.max_connection_attempts == None or \ 372 202 self.connection_attempt <= self.max_connection_attempts: 373 203 374 204 # If connection failed retry after one second 375 205 reactor.callLater(1, connector.connect) 376 206 377 207 else: 378 208 #self.log.error("Maximum connection retries from %s to %s reached, aborting" % \ 379 209 #(self.component['source'], self.component['target'])) 380 210 pass 381 211 382 212 self.replyDefer.callback((reply, self.component)) 383 384 385 ################################################################## 386 213 214 215 ################################################################## 216 387 217 def clientConnectionLost(self, connector, reason): 388 218 389 219 # Losing Connection is expected after data exchange is complete 390 220 #self.log.debug("Connection to %s lost for instruction %s" % (self.component, self.instruction)) … … 400 230 401 231 class puzzlebox_brainstorms_client_command_line(puzzlebox_brainstorms_client): 402 232 403 233 def __init__(self, log, command_parameters, SERVER_HOST, SERVER_PORT): 404 234 … … 410 240 self.target_name = 'MCP' 411 241 self.max_connection_attempts = MAX_CONNECTION_ATTEMPTS 412 413 414 ################################################################## 415 242 243 244 ################################################################## 245 416 246 def execute_command_line(self): 417 247 418 248 (command, information) = self.parse_command_list(self.command_parameters) 419 249 420 250 instruction = {} 421 251 instruction['command'] = command 422 252 instruction['information'] = information 423 253 424 254 d = self.send_instruction(instruction) 425 255 d.addCallback(self.print_response_and_stop) 426 427 256 257 428 258 ################################################################## 429 259 430 260 def print_response_and_stop(self, response): 431 261 432 262 print response[0] 433 263 434 264 reactor.stop() 435 436 437 ################################################################## 438 265 266 267 ################################################################## 268 439 269 def parse_command_list(self, command_parameters): 440 270 441 271 command = None 442 272 data = {} 443 273 444 274 if (command_parameters[0] == 'test_drive'): 445 446 275 command = 'test_drive' 447 448 449 #if command_parameters[0] == '/usr/sbin/chroot': 450 451 #self.command_parameters = command_parameters[:5] 452 #command_parameters = command_parameters[5:] 453 #if self.DEBUG: 454 #print "Chroot command parameters:", 455 #print self.command_parameters 456 #print "Data command parameters:", 457 #print command_parameters 458 459 460 #elif (command_parameters[0] == 'hide_all_components'): 461 462 #command = 'hide_all_components' 463 464 465 #elif (command_parameters[0] == 'unregister_all_windows'): 466 467 #command = 'unregister_all_windows' 468 469 470 #elif (command_parameters[0] == 'unregister_component'): 471 472 #command = 'unregister_component' 473 #data['pid'] = int(command_parameters[-1]) 474 475 476 #elif (command_parameters[0] == 'restart_component'): 477 478 #command = 'restart_component' 479 #data['pid'] = int(command_parameters[-1]) 480 481 482 #elif (command_parameters[0] == 'animation_started'): 483 484 #command = 'animation_started' 485 #data['window_id'] = int(command_parameters[-1]) 486 487 488 #elif (command_parameters[0] == 'register_window'): 489 490 #command = 'register_window' 491 #data['window_id'] = int(command_parameters[-1]) 492 493 494 #elif (command_parameters[0] == 'register_channel_detection'): 495 496 #command = 'register_component' 497 #data['type'] = 'channel_detection' 498 #data['pid'] = int(command_parameters[-2]) 499 #data['device'] = command_parameters[-1] 500 501 502 #elif (command_parameters[0] == 'exit_content_set'): 503 504 #command = 'exit_content_set' 505 506 507 #elif (command_parameters[0] == 'change_content_set'): 508 509 #command = 'change_content_set' 510 #data['content_set_id'] = int(command_parameters[-1]) 511 512 513 #elif (command_parameters[0] == 'end_current_entry'): 514 515 #command = 'end_current_entry' 516 #data['window_id'] = int(command_parameters[-1]) 517 518 519 #elif (command_parameters[0] == 'request_screenshot'): 520 521 #command = 'request_screenshot' 522 523 524 #elif (command_parameters[0] == 'upload_screenshot'): 525 526 #command = 'upload_screenshot' 527 528 529 #elif (command_parameters[0] == 'check_health'): 530 531 #command = 'check_health' 532 533 #if self.DEBUG > 1: 534 #print "-->Check health command parameters:", command_parameters 535 536 #data['hostname'] = command_parameters[1] 537 #data['port'] = command_parameters[2] 538 #data['source'] = command_parameters[3] 539 #data['target'] = command_parameters[4] 540 #data['execute_inside_screen'] = False 541 542 543 #elif (command_parameters[0] == 'export_schedule'): 544 545 #command = 'export_schedule' 546 #data['content_set_sequence_schedule_id'] = int(command_parameters[-1]) 547 548 276 277 549 278 else: 550 551 279 self.log.error("Unrecognized command received: %s" % command_parameters[0]) 552 553 280 281 554 282 return (command, data) 555 556 557 ################################################################## 558 283 284 285 ################################################################## 286 559 287 #def run_blocking_mcc_command(self, instruction, \ 560 288 #component={}, \ 561 289 #max_connection_attempts = MAX_CONNECTION_ATTEMPTS): 562 290 563 291 #command = 'puzzlebox_master_control_client.py' 564 292 #command = '%s %s' % (command, instruction['command']) 565 293 566 294 #if 'hostname' in component.keys() and \ 567 295 #component['hostname'] != None: 568 296 #command = '%s %s' % (command, component['hostname']) 569 297 570 298 #if 'port' in component.keys() and \ 571 299 #component['port'] != None: 572 300 #command = '%s %s' % (command, component['port']) 573 301 574 302 #if 'source' in component.keys() and \ 575 303 #component['source'] != None: 576 304 #command = '%s %s' % (command, component['source']) 577 305 578 306 #if 'target' in component.keys() and \ 579 307 #component['target'] != None: 580 308 #command = '%s %s' % (command, component['target']) 581 309 582 310 #if 'information' in instruction.keys() and \ 583 311 #'window_id' in instruction['information'].keys(): 584 312 #command = '%s %s' % (command, instruction['information']['window_id']) 585 313 586 314 #self.log.info("Executing external command: [%s]" % command) 587 315 588 316 #result = os.popen(command, 'r').read() 589 317 #result = result.strip() 590 318 591 319 #print "MCC-CL Result:", 592 320 #print result 593 321 594 322 #return result 595 323 … … 600 328 601 329 if __name__ == '__main__': 602 330 603 331 #log = puzzlebox_logger.puzzlebox_logger(logfile='mcc') 604 332 log = None 605 333 606 334 command_parameters = sys.argv[1:] 607 335 608 336 #log.info("Command parameters: %s" % command_parameters) 609 610 client = puzzlebox_brainstorms_client_command_line(log, command_parameters, SERVER_HOST, SERVER_PORT) 337 338 client = puzzlebox_brainstorms_client_command_line(log, \ 339 command_parameters, \ 340 SERVER_HOST, \ 341 SERVER_PORT) 611 342 reactor.callWhenRunning(client.execute_command_line) 612 343 reactor.run() -
rc/puzzlebox_brainstorms_configuration.py
r5 r6 54 54 SERVER_PORT = 8194 55 55 56 #CONTENT_SET_HOST = '127.0.0.1'57 #CONTENT_SET_PORT = 1638458 59 #WEB_BROWSER_HOST = '127.0.0.1'60 #WEB_BROWSER_PORT = 1819261 62 56 63 57 ##################################################################### … … 72 66 73 67 ##################################################################### 74 # S pecificconfiguration68 # Server configuration 75 69 ##################################################################### 76 70 77 # content set 78 #CONTENT_SET_CHANGE_RETRIES = 3 79 #CONTENT_SET_INITIAL_DELAY = 10 71 MAX_COMPONENTS = 16 80 72 81 # initialize82 #DEFAULT_BACKGROUND_COLOUR = 'black'83 #CURSOR_PATH = '/home/apps/eyemagnet/images/system/empty_cursor.bmp'84 #EYEMAGNET_LOGO_PATH = '/home/client/media/images/system/eyemagnet_logo.png'85 73 86 # Master Control Program87 MAX_COMPONENTS = 16 88 # AUTOBIT_TIMEOUT = 5 # how long in seconds to give browser animations to start74 ##################################################################### 75 # Client configuration 76 ##################################################################### 89 77 90 # Master Control Client91 78 MAX_CONNECTION_ATTEMPTS = 5 92 79 HEALTH_CHECK_CONNECTION_ATTEMPTS = 5 93 80 NO_REPLY_WAIT = 10 # how many seconds before considering a component dead 94 81 95 # monitoring96 #HEALTH_CHECK_TIMER = 3097 #SCRIPT_CHECK_TIMER = 6098 #SCRIPT_CHECKLIST = { \99 #'compiz': { \100 #'check': '/usr/share/eyemagnet/eyemagnet_compiz_manager/eyemagnet_compiz_manager.py', \101 #'check_args': ['check_health'], \102 #'recover': '/usr/bin/eyemagnet_xorg_reset.py', \103 #'recover_args': [] \104 #} \105 #}106 107 # web browser108 #HIDE_ON_DURATION_EXPIRE = True109 #GTKMOZ_PROFILE_DIR = "/home/client/.gtkmozembed/"110 111 # window112 #DEINTERLACE_VIDEO = 1113 #TEMPORAL_FIELD_SEPARATION = 1114 #VIDEO_NICE = 2115 #PID_SEARCH_COUNT = 10116 #DUAL_SQUEEZE_DELAY = 7.5117 #INITIAL_TUNER_SQUEEZE_DELAY = 6118 -
rc/puzzlebox_brainstorms_server.py
r5 r6 33 33 SERVER_PORT = configuration.SERVER_PORT 34 34 35 #MAX_COMPONENTS = puzzlebox_configuration.MAX_COMPONENTS36 #GTKMOZ_PROFILE_DIR = puzzlebox_configuration.GTKMOZ_PROFILE_DIR37 #AUTOBIT_TIMEOUT = puzzlebox_configuration.AUTOBIT_TIMEOUT38 39 40 35 ##################################################################### 41 36 # Classes … … 43 38 44 39 class puzzlebox_master_control(protocol.ServerFactory): 45 40 46 41 def __init__(self, log, DEBUG=DEBUG): 47 42 48 43 self.protocol = puzzlebox_master_control_server_protocol 49 44 50 45 self.log = log 51 46 self.DEBUG = DEBUG 52 47 53 48 self.registry = {} 54 #self.registry['components'] = {} 55 #self.registry['windows'] = {} 56 57 #self.csc = puzzlebox_master_control_client.puzzlebox_master_control_client( \ 58 #self.log, \ 59 #puzzlebox_configuration.CONTENT_SET_HOST, \ 60 #puzzlebox_configuration.CONTENT_SET_PORT, \ 61 #service_name = 'MCP', \ 62 #target_name = 'CS') 63 64 49 50 65 51 ################################################################## 66 52 67 53 def process_instruction(self, instruction): 68 54 69 55 #self.log.debug("Received instruction: %s" % instruction) 70 56 #if self.DEBUG: 71 57 #print "Received instruction: %s" % instruction 72 58 73 59 d = defer.Deferred() 74 60 response = 'instruction received' 75 61 76 62 if 'command' in instruction: 77 63 response = '%s instruction received' % instruction['command'] 78 79 64 65 80 66 if 'information' in instruction: 81 67 information = instruction['information'] 82 83 68 69 84 70 if instruction['command'] == 'test_drive': 85 71 … … 87 73 88 74 os.system(command) 89 90 91 #if instruction['command'] == 'register_component': 92 93 #if information['pid'] in self.registry['components']: 94 #self.log.error("Duplicate component registering - replacing old component (assumed dead).") 95 #self.log.error("Old component: %s" % self.registry['components'][information['pid']]) 96 #self.log.error("New Component: %s" % information) 97 98 #if self.DEBUG: 99 #print "Duplicate component registering - replacing old component (assumed dead)." 100 #print "Old component: %s" % self.registry['components'][information['pid']] 101 #print "New Component: %s" % information 102 103 #self.registry['components'][information['pid']] = information 104 105 106 #elif instruction['command'] == 'unregister_component': 107 108 #if information['pid'] in self.registry['components']: 109 #self.log.debug("Removing component from registry: %s" % self.registry['components'][information['pid']]) 110 111 #if self.DEBUG: 112 #print "Removing component from registry: %s" % \ 113 #self.registry['components'][information['pid']] 114 115 #del(self.registry['components'][information['pid']]) 116 117 #else: 118 #self.log.error("Unknown component attempted to unregister") 119 #if self.DEBUG: 120 #print "Unknown component attempted to unregister" 121 122 123 #elif instruction['command'] == 'update_component': 124 125 #if information['pid'] in self.registry['components']: 126 127 #component = self.registry['components'][information['pid']] 128 129 #for key in information: 130 #component[key] = information[key] 131 132 #else: 133 #self.log.error("Unknown component attempted to update: %s" % information) 134 #if self.DEBUG: 135 #print "Unknown component attempted to update: %s" % information 136 137 138 #elif instruction['command'] == 'restart_component': 75 76 77 else: 139 78 140 #self.restart_component(information['pid'])141 142 143 #elif instruction['command'] == 'load_url':144 145 #found_component = False146 147 #for each in self.registry['components']:148 149 #component = self.registry['components'][each]150 151 #if component['type'] == information['type'] and \152 #component['display_pos_x'] == information['display_pos_x'] and \153 #component['display_pos_y'] == information['display_pos_y'] and \154 #component['window_size_x'] == information['window_size_x'] and \155 #component['window_size_y'] == information['window_size_y']:156 157 #found_component = True158 159 #if component['active']:160 #self.log.debug("Matched an active component, activating callback.")161 #if self.DEBUG:162 #print "Matched an active component, activating callback."163 #component['callback'].callback("browser exit")164 165 #component['callback'] = d166 167 #try:168 #component['autobit'].cancel()169 #except:170 #pass171 172 #if int(information['enable_autobit']) == 1:173 #self.log.debug("Window %i loading with autobit enabled." % information['window_id'])174 175 #if self.DEBUG:176 #print "Window %i loading with autobit enabled." % \177 #information['window_id']178 179 #component['autobit'] = \180 #reactor.callLater(AUTOBIT_TIMEOUT, self.restart_component, each)181 182 #component['active'] = True183 184 #for key in information:185 #component[key] = information[key]186 187 #component['source'] = 'MCP'188 #component['target'] = component['type']189 190 #dw = self.csc.send_instruction(instruction, component=component)191 192 #dw.addCallback(self.process_component_response, instruction)193 194 #break195 196 #if not found_component:197 198 #self.log.debug("No component found matching load_url information, spawning new.")199 #if self.DEBUG:200 #print "No component found matching load_url information, spawning new."201 #self.spawn_new_component(instruction, d)202 203 #response = None204 205 206 #elif instruction['command'] == 'animation_started':207 208 #window_id = information['window_id']209 210 #self.log.debug("Got animation started for window id %i" % window_id)211 #if self.DEBUG:212 #print "Got animation started for window id %i" % window_id213 214 #for each in self.registry['components']:215 #if 'window_id' in self.registry['components'][each] and \216 #int(self.registry['components'][each]['window_id']) == int(window_id):217 218 #self.log.debug("Found window %i" % window_id)219 #if self.DEBUG:220 #print "Found window %i" % window_id221 222 #if 'autobit' in self.registry['components'][each]:223 #self.log.debug("Window %i cancelling autobit restart." % window_id)224 #if self.DEBUG:225 #print "Window %i cancelling autobit restart." % window_id226 227 #try:228 #self.registry['components'][each]['autobit'].cancel()229 #except:230 #self.log.error("Window %i error trying to cancel autobit restart" % window_id)231 #if self.DEBUG:232 #print "Window %i error trying to cancel autobit restart" % window_id233 234 #self.log.debug("Window %i white flash fix unhiding browser." % window_id)235 #tinstr = {'command': 'unhide'}236 #self.csc.send_instruction(tinstr, component=self.registry['components'][each])237 238 239 #elif instruction['command'] == 'hide_all_components':240 241 #dlist = []242 243 #for each in self.registry['components']:244 245 #component = self.safe_component(self.registry['components'][each])246 247 #tinstr = {'command': 'hide'}248 249 #dlist.append(self.csc.send_instruction(tinstr, component))250 251 #if dlist:252 #response = None253 #dl = defer.DeferredList(dlist)254 #dl.addCallback(d.callback)255 256 257 #elif instruction['command'] == 'register_window':258 259 #if information['window_id'] in self.registry['windows']:260 #self.log.error("Duplicate window attempted to register: %s" % information)261 #if self.DEBUG:262 #print "Duplicate window attempted to register: %s" % information263 264 #else:265 #self.registry['windows'][information['window_id']] = information266 267 268 #elif instruction['command'] == 'get_window_data':269 270 #if information['window_id'] == 'all':271 272 #response = self.registry['windows']273 274 #elif information['window_id'] in self.registry['windows']:275 276 #response = self.registry['windows'][information['window_id']]277 278 #else:279 #self.log.error("Attempt to request data for unknown window: %s" % information)280 #if self.DEBUG:281 #print "Attempt to request data for unknown window: %s" % information282 283 284 #elif instruction['command'] == 'unregister_all_windows':285 286 #self.registry['windows'] = {}287 288 289 #elif instruction['command'] == 'exit_content_set':290 291 #self.log.debug("Requesting current content set to exit.")292 #if self.DEBUG:293 #print "Requesting current content set to exit."294 #self.csc.exit_content_set()295 296 297 #elif instruction['command'] == 'change_content_set':298 299 #self.log.debug("Requesting to change content set to content_set_id %i" % information['content_set_id'])300 #if self.DEBUG:301 #print "Requesting to change content set to content_set_id %i" % \302 #information['content_set_id']303 #self.csc.change_content_set(information)304 305 306 #elif instruction['command'] == 'end_current_entry':307 308 #self.log.debug("Requesting to end content in window_id %i" % information['window_id'])309 #if self.DEBUG:310 #print "Requesting to end content in window_id %i" % \311 #information['window_id']312 #self.csc.end_current_entry(information)313 314 315 #elif instruction['command'] == 'request_screenshot':316 317 #self.log.debug("Requesting content set to take screenshot")318 #if self.DEBUG:319 #print "Requesting content set to take screenshot"320 #self.csc.request_screenshot()321 322 323 #elif instruction['command'] == 'upload_screenshot':324 325 #self.log.debug("Requesting content set to upload system screenshot")326 #if self.DEBUG:327 #print "Requesting content set to upload system screenshot"328 #self.csc.upload_screenshot()329 330 331 #elif instruction['command'] == 'export_schedule':332 333 #self.log.debug("Requesting content set to export content_set_sequence_schedule_id %i" % \334 #information['content_set_sequence_schedule_id'])335 #if self.DEBUG:336 #print "Requesting content set to export content_set_sequence_schedule_id %i" % \337 #information['content_set_sequence_schedule_id']338 #self.csc.export_schedule(information)339 340 341 #elif instruction['command'] == 'check_health':342 343 #response = 'health_okay'344 345 346 #elif instruction['command'] == 'get_registry':347 348 # unfortunately registry needs to have volatile components removed349 # before it can be passed via pickle, e.g. the deferred objects in callback.350 # may end up changing the structure around so it's less hassle to pass351 352 #safe_registry = {}353 #safe_registry['components'] = {}354 #safe_registry['windows'] = {}355 356 #for each in self.registry['components']:357 #safe_registry['components'][each] = \358 #self.safe_component(self.registry['components'][each])359 360 #for each in self.registry['windows']:361 #safe_registry['windows'][each] = self.registry['windows'][each].copy()362 363 #response = safe_registry364 365 366 #elif instruction['command'] == '/usr/bin/puzzlebox_xorg_reset.py':367 368 #response = "EXECUTE OK"369 370 371 else:372 373 79 #self.log.error("Unrecognized command received: %s" % instruction) 374 80 if self.DEBUG: 375 81 print "Unrecognized command received: %s" % instruction 376 82 reponse = 'unrecognized instruction' 377 378 83 84 379 85 if response: 380 86 d.callback(response) 381 87 382 88 return d 383 384 89 90 385 91 ################################################################## 386 387 #def process_component_response(self, result, instruction): 388 389 #result, component = result[0], result[1] 390 391 #if result == 'OK': 392 393 #self.log.debug('%s responded OK' % component['type']) 394 #if self.DEBUG: 395 #print '%s responded OK' % component['type'] 396 397 398 #elif result == 'FAILED_TO_CONNECT' or result == 'NO_REPLY': 399 400 #self.log.error('%s responded %s' % (component['type'], result)) 401 #if self.DEBUG: 402 #print '%s responded %s' % (component['type'], result) 403 404 #if instruction['command'] == 'duration_expire': 405 ## not sure what else to do here, since duration expire is meant to make the browser go away 406 ## if it is already gone, no real point in bringing it back? 407 #self.log.debug("Failed to connect for duration_expire... well whatever then.") 408 #if self.DEBUG: 409 #print "Failed to connect for duration_expire... well whatever then." 410 411 #elif instruction['command'] == 'load_url': 412 #self.log.error("Failed to connect for load_url, restarting the web_browser.") 413 #if self.DEBUG: 414 #print "Failed to connect for load_url, restarting the web_browser." 415 #self.restart_component(component[pid]) 416 417 #else: 418 #self.log.error("%s" % result) 419 #if self.DEBUG: 420 #print "%s" % result 421 422 423 ################################################################## 424 425 #def safe_component(self, component): 426 427 ## remove volatile data from a component so it can be safely passed through pickle 92 93 def process_connection_lost(self, instruction): 428 94 429 #tcomp = component.copy() 430 #tcomp['callback'] = None 431 #tcomp['autobit'] = None 95 if not instruction: 96 97 #self.log.debug("Connection lost with no instruction") 98 99 if self.DEBUG: 100 print "Connection lost with no instruction" 432 101 433 #return tcomp 434 435 436 ################################################################## 437 438 #def restart_component(self, pid): 439 440 #if pid in self.registry['components']: 441 #try: 442 #os.kill(pid, signal.SIGKILL) 443 #self.log.debug("Killed component %s" % pid) 444 #if self.DEBUG: 445 #print "Killed component %s" % pid 446 #return True 447 #except: 448 #self.log.error("Failed to kill component %s" % pid) 449 #if self.DEBUG: 450 #print "Failed to kill component %s" % pid 451 #return False 452 453 454 ################################################################## 455 456 def process_connection_lost(self, instruction): 457 458 if not instruction: 459 460 self.log.debug("Connection lost with no instruction?") 102 103 else: 104 105 #self.log.debug("Connection lost with no instruction") 106 461 107 if self.DEBUG: 462 print "Connection lost with no instruction?" 463 return 464 465 #else: 466 467 #if instruction['command'] == 'load_url': 468 469 #found_component = False 470 #information = instruction['information'] 471 472 #for each in self.registry['components']: 473 474 #component = self.registry['components'][each] 475 476 ## should only ever be 1 of each type of component per window_id 477 #if component['type'] == information['type'] and \ 478 #component['window_id'] == information['window_id']: 479 480 #found_component = True 481 482 #component['active'] = False 483 #component['last_active'] = time.time() 484 485 #component['source'] = 'MCP' 486 #component['target'] = component['type'] 487 488 #instruction['command'] = 'duration_expire' 489 #instruction['information'] = None 490 491 #dw = self.csc.send_instruction(instruction, component=component) 492 493 #dw.addCallback(self.process_component_response, instruction) 494 495 #break 496 497 #if not found_component: 498 499 #self.log.debug("Connection lost for a non-existent component? Weird.") 500 #if self.DEBUG: 501 #print "Connection lost for a non-existent component? Weird." 502 503 504 ################################################################## 505 506 #def stop_oldest_component(self): 507 ## return True if a component can be stopped 508 #oldest = {'last_active': time.time()} 509 #for each in self.registry['components']: 510 #component = self.registry['components'][each] 511 512 #if not component['active'] and \ 513 #component['last_active'] < oldest['last_active']: 514 #oldest = component 515 516 #if 'pid' in oldest: 517 ## in this case restart just means kill since the component is not currently active 518 #return self.restart_component(oldest['pid']) 519 520 #return False 521 522 523 ################################################################## 524 525 #def spawn_new_component(self, instruction, d): 526 527 #if len(self.registry['components']) >= MAX_COMPONENTS and \ 528 #not self.stop_oldest_component(): 529 #self.log.error("Max components exceeded, not spawning any new ones.") 530 #if self.DEBUG: 531 #print "Max components exceeded, not spawning any new ones." 532 #return 533 534 #if instruction['command'] == 'load_url': 535 536 #information = instruction['information'] 537 538 ## command = '/usr/bin/screen -S web_browser ' + \ 539 ## '%s %i %i %i %i %i %i %i %i %i %s' % \ 540 #command = '%s %i %i %i %i %i %i %i %i %i %i %s' % \ 541 #('/usr/bin/puzzlebox_web_browser.py', \ 542 #information['window_id'], \ 543 #information['display_pos_x'], \ 544 #information['display_pos_y'], \ 545 #information['window_size_x'], \ 546 #information['window_size_y'], \ 547 #information['display_fullscreen'], \ 548 #information['refresh_interval'], \ 549 #information['duration'], \ 550 #information['hide_on_duration_expire'], \ 551 #information['autobit_white_flash_fix'], \ 552 #information['url']) 553 554 #args = command.strip().split() 555 #file = args[0] 556 557 #self.log.debug("Spawning process from command: [%s]" % command) 558 #if self.DEBUG: 559 #print "Spawning process from command: [%s]" % command 560 561 #pp = puzzlebox_mcp_browser_pp(self.log, instruction, self.registry, \ 562 #self.spawn_new_component, self.restart_component, d, self.DEBUG) 563 #process = reactor.spawnProcess(pp, file, args, os.environ, usePTY=True) 564 565 #else: 566 #self.log.debug("Not spawning process because command is '%s'" % instruction['command']) 567 #if self.DEBUG: 568 #print "Not spawning process because command is '%s'" % instruction['command'] 569 570 571 ################################################################## 572 573 #def spawn_new_process(self, instruction, d): 574 575 #process = reactor.spawnProcess(pp, file, args, os.environ, usePTY=True) 576 577 #if len(self.registry['components']) >= MAX_COMPONENTS and \ 578 #not self.stop_oldest_component(): 579 #self.log.error("Max components exceeded, not spawning any new ones.") 580 #if self.DEBUG: 581 #print "Max components exceeded, not spawning any new ones." 582 #return 583 584 #if instruction['command'] == 'load_url': 585 586 #information = instruction['information'] 587 588 ## command = '/usr/bin/screen -S web_browser ' + \ 589 ## '%s %i %i %i %i %i %i %i %i %i %s' % \ 590 #command = '%s %i %i %i %i %i %i %i %i %i %i %s' % \ 591 #('/usr/bin/puzzlebox_web_browser.py', \ 592 #information['window_id'], \ 593 #information['display_pos_x'], \ 594 #information['display_pos_y'], \ 595 #information['window_size_x'], \ 596 #information['window_size_y'], \ 597 #information['display_fullscreen'], \ 598 #information['refresh_interval'], \ 599 #information['duration'], \ 600 #information['hide_on_duration_expire'], \ 601 #information['autobit_white_flash_fix'], \ 602 #information['url']) 603 604 #args = command.strip().split() 605 #file = args[0] 606 607 #self.log.debug("Spawning process from command: [%s]" % command) 608 #if self.DEBUG: 609 #print "Spawning process from command: [%s]" % command 610 611 #pp = puzzlebox_mcp_browser_pp(self.log, instruction, self.registry, \ 612 #self.spawn_new_component, self.restart_component, d, self.DEBUG) 613 #process = reactor.spawnProcess(pp, file, args, os.environ, usePTY=True) 614 615 #else: 616 #self.log.debug("Not spawning process because command is '%s'" % instruction['command']) 617 #if self.DEBUG: 618 #print "Not spawning process because command is '%s'" % instruction['command' 619 620 621 ##################################################################### 622 # Process Protocol 623 ##################################################################### 624 625 #class puzzlebox_mcp_browser_pp(protocol.ProcessProtocol): 626 627 #def __init__(self, log, \ 628 #instruction, \ 629 #registry, \ 630 #spawn_new_component, \ 631 #restart_component, \ 632 #d, \ 633 #DEBUG=DEBUG): 634 635 #self.log = log 636 #self.instruction = instruction 637 #self.information = self.instruction['information'] 638 #self.registry = registry 639 #self.spawn_new_component = spawn_new_component 640 #self.restart_component = restart_component 641 #self.d = d 642 #self.DEBUG = DEBUG 643 644 645 ################################################################## 646 647 #def connectionMade(self): 648 #self.pid = self.transport.pid 649 #self.registry['components'][self.pid] = self.instruction['information'] 650 #self.registry['components'][self.pid]['pid'] = self.pid 651 #self.registry['components'][self.pid]['active'] = True 652 #self.registry['components'][self.pid]['callback'] = self.d 653 654 #if int(self.information['enable_autobit']) == 1: 655 #self.log.debug("Window %i started with autobit enabled." % self.information['window_id']) 656 #if self.DEBUG: 657 #print "Window %i started with autobit enabled." % self.information['window_id'] 658 #self.registry['components'][self.pid]['autobit'] = \ 659 #reactor.callLater(AUTOBIT_TIMEOUT, self.restart_component, self.pid) 660 661 #self.log.debug("Sub-process started with pid %i" % self.pid) 662 #if self.DEBUG: 663 #print "Sub-process started with pid %i" % self.pid 664 665 666 ################################################################## 667 668 #def childDataReceived(self, childFD, data): 669 670 ## attempt to log any errors... might not work for exceptions 671 #self.log.error(data) 672 673 #if self.DEBUG: 674 #print data 675 676 677 ################################################################## 678 679 #def processEnded(self, status_object): 680 681 #self.log.debug("Sub-process with pid %i ended" % self.pid) 682 #if self.DEBUG: 683 #print "Sub-process with pid %i ended" % self.pid 684 685 ## this is kind of unnecessary as i could just use 686 ## component = self.instruction['information'] 687 ## but for now i want to make sure it still exists in registry too 688 #if self.pid in self.registry['components']: 689 690 #component = self.registry['components'][self.pid] 691 692 #if component['active']: 693 #self.log.debug("Component is still active, respawning...") 694 #if self.DEBUG: 695 #print "Component is still active, respawning..." 696 ## for key in self.instruction['information']: 697 ## self.instruction['information'][key] = component[key] 698 #self.spawn_new_component(self.instruction, component['callback']) 699 #else: 700 #self.log.debug("Component inactive, not respawning.") 701 #if self.DEBUG: 702 #print "Component inactive, not respawning." 703 704 ## clean up the old component data in either case 705 ## and remove gtkmoz profile 706 #del(self.registry['components'][self.pid]) 707 #profile_dir = os.path.join(GTKMOZ_PROFILE_DIR, "%s" % self.pid) 708 #if os.path.exists(profile_dir): 709 #command = 'rm -rf %s' % profile_dir 710 #self.log.debug("Executing command: [%s]" % command) 711 #if self.DEBUG: 712 #print "Executing command: [%s]" % command 713 #os.system(command) 714 715 #else: 716 #self.log.error("Could not find component associated with this sub-process. Odd.") 717 #if self.DEBUG: 718 #print "Could not find component associated with this sub-process. Odd." 108 print "Connection lost instruction" 719 109 720 110 … … 724 114 725 115 class puzzlebox_master_control_server_protocol(protocol.Protocol): 726 116 727 117 def __init__(self): 728 118 self.instruction = {} 729 119 self.data_chunk = "" 730 731 120 121 732 122 ################################################################## 733 123 734 124 def dataReceived(self, data): 735 125 736 126 self.data_chunk += data 737 127 try: … … 741 131 else: 742 132 self.data_chunk = "" 743 133 744 134 d = self.factory.process_instruction(self.instruction.copy()) 745 135 d.addCallback(self.send_response) 746 747 136 137 748 138 ################################################################## 749 139 750 140 def send_response(self, response): 751 141 752 142 #if response == "browser exit": 753 143 #self.instruction['command'] = response 754 144 755 145 response = pickle.dumps(response) 756 146 757 147 self.transport.write(response) 758 759 148 149 760 150 ################################################################## 761 151 762 152 def connectionLost(self, reason): 763 153 764 154 self.factory.process_connection_lost(self.instruction) 765 155 … … 773 163 #log = puzzlebox_logger.puzzlebox_logger(logfile='master_control') 774 164 log = None 775 165 776 166 # Collect default settings and command line parameters 777 167 server_host = SERVER_HOST … … 784 174 if each.startswith("--port="): 785 175 server_port = each[ len("--port="): ] 786 176 177 787 178 mcp = puzzlebox_master_control(log, DEBUG) 788 179 reactor.listenTCP(port=server_port, factory=mcp, interface=server_host)
Note: See TracChangeset
for help on using the changeset viewer.