From 712d66c389c068cb84738bae0e26a7904c5e1c73 Mon Sep 17 00:00:00 2001 From: craigerl Date: Fri, 4 Dec 2020 07:21:37 -0800 Subject: [PATCH 1/3] socket reconnect on timeout testing --- aprsd/main.py | 40 +++++++++------------------------------- 1 file changed, 9 insertions(+), 31 deletions(-) diff --git a/aprsd/main.py b/aprsd/main.py index 5248e70..929dc95 100644 --- a/aprsd/main.py +++ b/aprsd/main.py @@ -113,6 +113,7 @@ def setup_connection(): try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((CONFIG['aprs']['host'], 14580)) + sock.settimeout(60) except Exception, e: print "Unable to connect to APRS-IS server.\n" print str(e) @@ -493,7 +494,7 @@ def main(args=args): password = CONFIG['aprs']['password'] LOG.info("LOGIN to APRSD with user '%s'" % user) #tn.write("user %s pass %s vers aprsd 0.99\n" % (user, password)) - sock.send("user %s pass %s vers aprsd 0.99\n" % (user, password)) + sock.send("user %s pass %s vers https://github.com/craigerl/aprsd 2.00\n" % (user, password)) time.sleep(2) @@ -505,9 +506,6 @@ def main(args=args): while True: line = "" try: - #for char in tn.read_until("\n", 100): - # line = line + char - #line = line.replace('\n', '') line = sock_file.readline().strip() LOG.info(line) searchstring = '::%s' % user @@ -586,32 +584,6 @@ def main(args=args): send_message(fromcall, reply.rstrip()) # LOCATION (l) "8 Miles E Auburn CA 1771' 38.91547,-120.99500 0.1h ago" -# elif re.search('^l', message): -# # get my last location, get descriptive name from weather service -# try: -# url = "http://api.aprs.fi/api/get?name=" + fromcall + "&what=loc&apikey=104070.f9lE8qg34L8MZF&format=json" -# response = urllib.urlopen(url) -# aprs_data = json.loads(response.read()) -# lat = aprs_data['entries'][0]['lat'] -# lon = aprs_data['entries'][0]['lng'] -# try: # altitude not always provided -# alt = aprs_data['entries'][0]['altitude'] -# except: -# alt = 0 -# altfeet = int(alt * 3.28084) -# aprs_lasttime_seconds = aprs_data['entries'][0]['lasttime'] -# aprs_lasttime_seconds = aprs_lasttime_seconds.encode('ascii',errors='ignore') #unicode to ascii -# delta_seconds = time.time() - int(aprs_lasttime_seconds) -# delta_hours = delta_seconds / 60 / 60 -# url2 = "https://forecast.weather.gov/MapClick.php?lat=" + str(lat) + "&lon=" + str(lon) + "&FcstType=json" -# response2 = urllib.urlopen(url2) -# wx_data = json.loads(response2.read()) -# reply = wx_data['location']['areaDescription'] + " " + str(altfeet) + "' " + str(lat) + "," + str(lon) + " " + str("%.1f" % round(delta_hours,1)) + "h ago" -# reply = reply.encode('ascii', errors='ignore') # unicode to ascii -# send_message(fromcall, reply.rstrip()) -# except: -# reply = "Unable to find you (send beacon?)" -# send_message(fromcall, reply.rstrip()) elif re.search('^[lL]', message): # get last location of a callsign, get descriptive name from weather service try: @@ -690,7 +662,13 @@ def main(args=args): LOG.error("Error in mainline loop:") LOG.error("%s" % str(e)) LOG.error("Exiting.") - # sys.exit(1) # merely a suggestion + if str(e) == "timed out": + LOG.error("Attempting to reconnect.") + sock.shutdown(0) + sock.close() + setup_connection() + sock.send("user %s pass %s vers https://github.com/craigerl/aprsd 2.00\n" % (user, password)) + continue os._exit(1) # end while True From 766acbb777a59150da5e77d2d5c7725621dfc01e Mon Sep 17 00:00:00 2001 From: craigerl Date: Fri, 4 Dec 2020 09:43:11 -0800 Subject: [PATCH 2/3] Reconnect on socket timeout --- aprsd/main.py | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/aprsd/main.py b/aprsd/main.py index 929dc95..1e93de0 100644 --- a/aprsd/main.py +++ b/aprsd/main.py @@ -110,17 +110,22 @@ args = parser.parse_args() def setup_connection(): global sock global sock_file - try: - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.connect((CONFIG['aprs']['host'], 14580)) - sock.settimeout(60) - except Exception, e: - print "Unable to connect to APRS-IS server.\n" - print str(e) - os._exit(1) - sock_file = sock.makefile(mode='r', bufsize=0 ) - sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) # disable nagle algorithm - sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 512) # buffer size + connected = False + while not connected: + try: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.connect((CONFIG['aprs']['host'], 14580)) + sock.settimeout(60) + connected = True + except Exception, e: + print "Unable to connect to APRS-IS server.\n" + print str(e) + time.sleep(5) + continue + #os._exit(1) + sock_file = sock.makefile(mode='r', bufsize=0 ) + sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) # disable nagle algorithm + sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 512) # buffer size @@ -661,15 +666,17 @@ def main(args=args): except Exception as e: LOG.error("Error in mainline loop:") LOG.error("%s" % str(e)) - LOG.error("Exiting.") - if str(e) == "timed out": + if str(e) == "timed out" or str(e) == "Temporary failure in name resolution" or str(e) == "Network is unreachable": LOG.error("Attempting to reconnect.") sock.shutdown(0) sock.close() setup_connection() sock.send("user %s pass %s vers https://github.com/craigerl/aprsd 2.00\n" % (user, password)) continue - os._exit(1) + #LOG.error("Exiting.") + #os._exit(1) + time.sleep(5) + continue # don't know what failed, so wait and then continue main loop again # end while True #tn.close() From 32e146d105dc47ab591f200b4d0df89209d45b77 Mon Sep 17 00:00:00 2001 From: craigerl Date: Fri, 4 Dec 2020 13:37:29 -0800 Subject: [PATCH 3/3] socket timeout of 300 instead of 60 --- aprsd/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aprsd/main.py b/aprsd/main.py index 1e93de0..3373109 100644 --- a/aprsd/main.py +++ b/aprsd/main.py @@ -115,7 +115,7 @@ def setup_connection(): try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((CONFIG['aprs']['host'], 14580)) - sock.settimeout(60) + sock.settimeout(300) connected = True except Exception, e: print "Unable to connect to APRS-IS server.\n"