def _retry_read(self): """Retry read connections. Intended to be called by _connect_rover_fired. """ i = 0 repeats = 5 _rover_pragma = self.rover_pragma while self.http and not self.http.connect_read(pragma=_rover_pragma): print "Attempting to read observation from Skylark..." time.sleep(0.1) i += 1 if i >= repeats: msg = ( "\nUnable to receive observations from Skylark!\n\n" "Please check that:\n" " - you have a network connection\n" " - your Piksi has a single-point position\n" " - a Skylark-connected Piksi receiver \n is nearby (within 5km)" ) self._prompt_networking_error(msg) self.http.read_close() return print "Connected as a rover!" with Handler(Framer(self.http.read, self.http.write)) as net_link: self.net_link = net_link self.fwd = Forwarder(net_link, swriter(self.link)) self.fwd.start() while True: time.sleep(1) if not net_link.is_alive(): sys.stderr.write( "Network observation stream disconnected!") break
def _retry_read(self): """Retry read connections. Intended to be called by _connect_rover_fired. """ i = 0 repeats = 5 _rover_pragma = self.rover_pragma _rover_device_uid = self.rover_device_uid or self.device_uid while self.http and not self.http.connect_read( device_uid=_rover_device_uid, pragma=_rover_pragma): print "Attempting to read observation from Skylark..." time.sleep(0.1) i += 1 if i >= repeats: msg = ( "\nUnable to receive observations from Skylark!\n\n" "Please check that:\n" " - you have a network connection\n" " - your Piksi has a single-point position\n" " - a Skylark-connected Piksi receiver \n is nearby (within 5km)" ) self._prompt_networking_error(msg) self.http.read_close() return print "Connected as a rover!" with Handler(Framer(self.http.read, self.http.write)) as net_link: self.net_link = net_link self.fwd = Forwarder(net_link, swriter(self.link)) self.fwd.start() while True: time.sleep(1) if not net_link.is_alive(): sys.stderr.write( "Network observation stream disconnected!") break # Unless the user has initiated a reconnection, assume here that the rover # still wants to be connected, so if we break out of the handler loop, # cleanup rover connection and try reconnecting. if self.connected_rover: sys.stderr.write("Going for a networking reconnection!") self._disconnect_rover_fired() self._connect_rover_fired()
def connect(self, link, read_config): """Retry read connections. Intended to be called when thread started Only shared resource here is the self.link Parameters ---------- link : SbpHandler read_config : SkylarkConsoleConnectConfig object Returns ---------- ret : int 0 if exited normally by thread stopping -1 if unable to connect as base station -2 if unable to connect as rover -3 if we lost our net connection to skylark (we restart in this case unless stopped) """ assert isinstance(read_config, SkylarkConsoleConnectConfig) self._connect_time = time.time() if self.verbose: print(("SkylarkWatchdogThread connection attempted at time {0} with parameters {1}".format( self.get_connect_time(), read_config))) i = 0 repeats = 5 http = HTTPDriver(device_uid=read_config.base_uuid, url=read_config.skylark_url) if not http.connect_write(link, read_config.whitelist, pragma=read_config.base_pragma): msg = ("\nUnable to connect to Skylark!\n\n" + "Please check that you have a network connection.") self._prompt_networking_error(msg) http.close() self.stop() return -1 # unable to connect as base time.sleep(1) # If we get here, we were able to connect as a base print("Attempting to read observation from Skylark...") while (not self.stopped() and http and not http.connect_read(device_uid=read_config.rover_uuid, pragma=read_config.rover_pragma)): time.sleep(0.1) i += 1 if i >= repeats: msg = ("\nUnable to receive observations from Skylark!\n\n" "Please check that:\n" " - you have a network connection\n" " - your Piksi has a single-point position\n" " - a Skylark-connected Piksi receiver \n is nearby (within 5km)") self._prompt_networking_error(msg) http.close() self.stop() return -2 # Unable to connect as rover # If we get here, we were able to connect as rover print("Connected as a rover!") with Handler(Framer(http.read, http.write)) as net_link: fwd = Forwarder(net_link, swriter(link)) if self.verbose: print("Starting forwarder") fwd.start() # now we sleep until we stop the thread or our http handler dies while not self.stopped() and net_link.is_alive(): time.sleep(0.1) # when we leave this loop, we are no longer connected to skylark so the fwd should be stopped if self.verbose: print("Stopping forwarder") fwd.stop() if self.verbose: print("Stopping HTTPDriver") http.close() # now manage the return code if self.stopped(): return 0 # If we stop from the event, it it intended and we return 0 else: return -3 # Lost connection