@@ -77,6 +77,9 @@ def run_testing(self):
7777 if not self .options .android and not self .options .iOS :
7878 message_server = MessageServer ()
7979 message_server .start ()
80+ if not message_server .is_ok ():
81+ logging .error ("Unable to start the local message server" )
82+ return
8083 while not self .must_exit :
8184 try :
8285 if os .path .isfile (exit_file ):
@@ -293,6 +296,13 @@ def startup(self):
293296 "and make sure it is in the path."
294297 ret = False
295298
299+ try :
300+ import tornado as _
301+ except ImportError :
302+ from internal .os_util import run_elevated
303+ logging .debug ('Trying to install tornado...' )
304+ run_elevated (sys .executable , '-m pip install tornado' )
305+
296306 if platform .system () == "Linux" :
297307 try :
298308 subprocess .check_output (['traceroute' , '--version' ])
@@ -366,7 +376,6 @@ def startup(self):
366376 ret = False
367377 return ret
368378
369-
370379class HandleMessage (BaseHTTPRequestHandler ):
371380 """Handle a single message from the extension"""
372381 # 1.1 keep-alive is pretty buggy, leave this disabled for now
@@ -480,7 +489,8 @@ def __init__(self):
480489 self .thread = None
481490 self .messages = Queue .Queue ()
482491 self .config = None
483- self .__is_started = threading .Event ()
492+ self .is_started = threading .Event ()
493+ self .using_tornado = False
484494
485495 def get_message (self , timeout ):
486496 """Get a single message from the queue"""
@@ -503,17 +513,21 @@ def handle_message(self, message):
503513
504514 def start (self ):
505515 """Start running the server in a background thread"""
506- self .__is_started .clear ()
516+ self .is_started .clear ()
507517 self .thread = threading .Thread (target = self .run )
508518 self .thread .daemon = True
509519 self .thread .start ()
510- self .__is_started .wait (timeout = 30 )
520+ self .is_started .wait (timeout = 30 )
511521
512522 def stop (self ):
513523 """Stop running the server"""
514524 logging .debug ("Shutting down extension server" )
515525 self .must_exit = True
516- if self .server is not None :
526+ if self .using_tornado :
527+ from internal .tornado_responder import stop_tornado
528+ stop_tornado ()
529+ self .thread .join ()
530+ elif self .server is not None :
517531 try :
518532 self .server .shutdown ()
519533 except Exception :
@@ -543,18 +557,22 @@ def run(self):
543557 handler = handler_template (self )
544558 logging .debug ('Starting extension server on port 8888' )
545559 try :
546- self .server = HTTPServer (('127.0.0.1' , 8888 ), handler )
547- self .server .timeout = 10
548- self .__is_started .set ()
549- self .server .serve_forever ()
550- except Exception :
551- logging .exception ("Message server main loop exception" )
552- self .__is_started .set ()
553- if self .server is not None :
560+ from internal .tornado_responder import start_tornado
561+ start_tornado (self )
562+ except ImportError :
554563 try :
555- self .server .socket .close ()
564+ self .server = HTTPServer (('127.0.0.1' , 8888 ), handler )
565+ self .server .timeout = 10
566+ self .is_started .set ()
567+ self .server .serve_forever ()
556568 except Exception :
557- pass
569+ logging .exception ("Message server main loop exception" )
570+ self .is_started .set ()
571+ if self .server is not None :
572+ try :
573+ self .server .socket .close ()
574+ except Exception :
575+ pass
558576
559577def parse_ini (ini ):
560578 """Parse an ini file and convert it to a dictionary"""
0 commit comments