mirror of
https://github.com/craigerl/aprsd.git
synced 2025-06-24 21:15:18 -04:00
clean up code around closed_socket and reconnect
This commit is contained in:
parent
e6995c3605
commit
371a7a08ac
@ -101,17 +101,6 @@ parser.add_argument("--quiet",
|
|||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
# def setup_connection():
|
|
||||||
# global tn
|
|
||||||
# host = CONFIG['aprs']['host']
|
|
||||||
# port = CONFIG['aprs']['port']
|
|
||||||
# LOG.debug("Setting up telnet connection to '%s:%s'" % (host, port))
|
|
||||||
# try:
|
|
||||||
# tn = telnetlib.Telnet(host, port)
|
|
||||||
# except Exception:
|
|
||||||
# LOG.exception("Telnet session failed.")
|
|
||||||
# sys.exit(-1)
|
|
||||||
|
|
||||||
def setup_connection():
|
def setup_connection():
|
||||||
global sock
|
global sock
|
||||||
global sock_file
|
global sock_file
|
||||||
@ -132,15 +121,20 @@ def setup_connection():
|
|||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
continue
|
continue
|
||||||
# os._exit(1)
|
# os._exit(1)
|
||||||
|
user = CONFIG['aprs']['login']
|
||||||
|
password = CONFIG['aprs']['password']
|
||||||
|
LOG.debug("Logging in to APRS-IS with user '%s'" % user)
|
||||||
|
msg = ("user {} pass {} vers aprsd {}\n".format(user, password, aprsd.__version__))
|
||||||
|
sock.send(msg.encode())
|
||||||
|
|
||||||
|
|
||||||
def signal_handler(signal, frame):
|
def signal_handler(signal, frame):
|
||||||
LOG.info("Ctrl+C, exiting.")
|
LOG.info("Ctrl+C, exiting.")
|
||||||
# sys.exit(0) # thread ignores this
|
# sys.exit(0) # thread ignores this
|
||||||
os._exit(0)
|
os._exit(0)
|
||||||
|
|
||||||
|
|
||||||
# end signal_handler
|
# end signal_handler
|
||||||
|
|
||||||
|
|
||||||
def parse_email(msgid, data, server):
|
def parse_email(msgid, data, server):
|
||||||
envelope = data[b'ENVELOPE']
|
envelope = data[b'ENVELOPE']
|
||||||
# email address match
|
# email address match
|
||||||
@ -630,10 +624,9 @@ def main(args=args):
|
|||||||
user = CONFIG['aprs']['login']
|
user = CONFIG['aprs']['login']
|
||||||
password = CONFIG['aprs']['password']
|
password = CONFIG['aprs']['password']
|
||||||
|
|
||||||
LOG.debug("LOGIN to APRSD with user '%s'" % user)
|
#LOG.debug("LOGIN to APRSD with user '%s'" % user)
|
||||||
msg = ("user {} pass {} vers aprsd {}\n".format(user, password,
|
#msg = ("user {} pass {} vers aprsd {}\n".format(user, password, aprsd.__version__))
|
||||||
aprsd.__version__))
|
#sock.send(msg.encode())
|
||||||
sock.send(msg.encode())
|
|
||||||
|
|
||||||
|
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
@ -652,8 +645,7 @@ def main(args=args):
|
|||||||
line = ""
|
line = ""
|
||||||
try:
|
try:
|
||||||
line = sock_file.readline().strip()
|
line = sock_file.readline().strip()
|
||||||
if line:
|
LOG.info("APRS-IS: " + line)
|
||||||
LOG.info(line)
|
|
||||||
# is aprs message to us? not beacon, status, empty line, etc
|
# is aprs message to us? not beacon, status, empty line, etc
|
||||||
searchstring = '::%s' % user
|
searchstring = '::%s' % user
|
||||||
if re.search(searchstring, line):
|
if re.search(searchstring, line):
|
||||||
@ -662,22 +654,16 @@ def main(args=args):
|
|||||||
LOG.debug("reset empty line counter")
|
LOG.debug("reset empty line counter")
|
||||||
empty_line_rx = 0
|
empty_line_rx = 0
|
||||||
else:
|
else:
|
||||||
LOG.debug("Noise: " + line)
|
# LOG.debug("Noise: " + line)
|
||||||
# detect closed socket, getting lots of empty lines
|
# detect closed socket, getting lots of empty lines
|
||||||
if len(line.strip()) == 0:
|
if len(line.strip()) == 0:
|
||||||
LOG.debug("Zero line length received. Consecutive empty line count: " + str(empty_line_rx))
|
LOG.debug("Zero line length received. Consecutive empty line count: " + str(empty_line_rx))
|
||||||
empty_line_rx += 1
|
empty_line_rx += 1
|
||||||
if empty_line_rx >= 30:
|
if empty_line_rx >= 30:
|
||||||
LOG.debug("Excessive empty lines received, socket likely CLOSED_WAIT. Reconnecting.")
|
LOG.debug("Excessive empty lines received, socket likely CLOSED_WAIT. Reconnecting.")
|
||||||
sock_file.close()
|
|
||||||
sock.shutdown(0)
|
|
||||||
sock.close()
|
|
||||||
time.sleep(30)
|
|
||||||
setup_connection()
|
|
||||||
sock.send("user %s pass %s vers https://github.com/craigerl/aprsd 2.00\n" % (user, password))
|
|
||||||
#LOG.debug("reset empty line counter")
|
|
||||||
empty_line_rx = 0
|
empty_line_rx = 0
|
||||||
continue
|
raise Exception("closed_socket")
|
||||||
|
continue # line is something we don't care about
|
||||||
|
|
||||||
# ACK (ack##)
|
# ACK (ack##)
|
||||||
if re.search('^ack[0-9]+', message):
|
if re.search('^ack[0-9]+', message):
|
||||||
@ -767,7 +753,6 @@ def main(args=args):
|
|||||||
LOG.debug("LOCATION")
|
LOG.debug("LOCATION")
|
||||||
# get last location of a callsign, get descriptive name from weather service
|
# get last location of a callsign, get descriptive name from weather service
|
||||||
try:
|
try:
|
||||||
print("XXX message is:" + message)
|
|
||||||
a = re.search(r"^.*\s+(.*)", message) # optional second argument is a callsign to search
|
a = re.search(r"^.*\s+(.*)", message) # optional second argument is a callsign to search
|
||||||
if a is not None:
|
if a is not None:
|
||||||
searchcall = a.group(1)
|
searchcall = a.group(1)
|
||||||
@ -833,38 +818,33 @@ def main(args=args):
|
|||||||
# USAGE
|
# USAGE
|
||||||
else:
|
else:
|
||||||
LOG.debug("USAGE")
|
LOG.debug("USAGE")
|
||||||
reply = "Usage: weath, locate <callsign>, ping, time, fortune"
|
reply = "Usage: weather, locate <callsign>, ping, time, fortune"
|
||||||
send_message(fromcall, reply)
|
send_message(fromcall, reply)
|
||||||
|
|
||||||
# let any threads do their thing, then ack
|
# let any threads do their thing, then ack
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
# send an ack last
|
# send an ack last
|
||||||
LOG.debug("SEND ACK")
|
LOG.debug("Send ACK to radio.")
|
||||||
send_ack(fromcall, ack)
|
send_ack(fromcall, ack)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
LOG.error("Error in mainline loop:")
|
LOG.error("Error in mainline loop:")
|
||||||
LOG.error("%s" % str(e))
|
LOG.error("%s" % str(e))
|
||||||
|
if ( str(e) == "closed_socket" or str(e) == "timed out" or str(e) == "Temporary failure in name resolution" or str(e) == "Network is unreachable"):
|
||||||
if (str(e) == "timed out" or str(e) == "Temporary failure in name resolution" or str(e) == "Network is unreachable"):
|
|
||||||
LOG.error("Attempting to reconnect.")
|
LOG.error("Attempting to reconnect.")
|
||||||
|
sock_file.close()
|
||||||
sock.shutdown(0)
|
sock.shutdown(0)
|
||||||
sock.close()
|
sock.close()
|
||||||
setup_connection()
|
setup_connection()
|
||||||
sock.send("user %s pass %s vers https://github.com/craigerl/aprsd 2.00\n" % (user, password))
|
|
||||||
LOG.debug("continue: connection failed, just reconnected")
|
|
||||||
continue
|
continue
|
||||||
# LOG.error("Exiting.")
|
LOG.error("Unexpected error: " + str(e))
|
||||||
# os._exit(1)
|
LOG.error("Continuing anyway.")
|
||||||
|
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
LOG.debug("contnue: Unexpected error: " + str(e))
|
|
||||||
continue # don't know what failed, so wait and then continue main loop again
|
continue # don't know what failed, so wait and then continue main loop again
|
||||||
|
|
||||||
LOG.debug("Main loop end")
|
LOG.debug("Main loop end")
|
||||||
# end while True
|
# end while True
|
||||||
|
|
||||||
exit()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
x
Reference in New Issue
Block a user