Aanpassingen reconnectcheck

- reconnectcheck naar sendrecv
 - idleconnect check
 - test voor idleconnect
This commit is contained in:
Marcel Nijenhof
2020-11-08 13:49:12 +01:00
parent 17301b70b3
commit 3001b434a6
2 changed files with 46 additions and 27 deletions

View File

@@ -29,7 +29,7 @@ Support for:
def __init__(self, user=None, password=None, def __init__(self, user=None, password=None,
host="sip-lmw.rws.nl", port=443, meetnet="LMW", ssl = True, host="sip-lmw.rws.nl", port=443, meetnet="LMW", ssl = True,
check_ssl = True, timeout = 10, log = None, cleartelnet = False, check_ssl = True, timeout = 10, log = None, cleartelnet = False,
reconnecttime=540): reconnecttime=540, idlereconnect=45):
"""LmwSip(user, password, [host], [port], [meetnet], [ssl], [check_ssl], [timeout], [log]) """LmwSip(user, password, [host], [port], [meetnet], [ssl], [check_ssl], [timeout], [log])
user(optinal): Lmw user name user(optinal): Lmw user name
@@ -55,25 +55,29 @@ Opens the connection and logs in.
self.check_ssl = check_ssl self.check_ssl = check_ssl
self.timeout = timeout self.timeout = timeout
self.cleartelnet = cleartelnet self.cleartelnet = cleartelnet
self.reconnecttime = reconnecttime self.reconnecttime = 0
self._connecttime = time.time() self.idlereconnect = 0
self._connecttime = time.time()
self._idletime = time.time()
self._socket = None self._socket = None
if (log != None): if (log != None):
self.log = log self.log = log
self.log.debug("LmwSip.init(%s, **********, %s, %s, %s, %s, %s, %s, %s, %s)" %
(user, host, port, meetnet, ssl, check_ssl, timeout, cleartelnet, reconnecttime))
else: else:
try: try:
self.log = logging.getLogger("lmwsip") self.log = logging.getLogger("lmwsip")
self.log.debug("LmwSip.init: Start log") self.log.debug("LmwSip.init: Start log")
except Exception as e: except Exception as e:
print("Logger failed: %s" % e) print("Logger failed: %s" % e)
self.log.debug("LmwSip.init(%s, **********, %s, %s, %s, %s, %s, %s, %s, %s, %s)" %
(user, host, port, meetnet, ssl, check_ssl, timeout,
cleartelnet, reconnecttime, idlereconnect))
if (self.host != None): if (self.host != None):
self.connect() self.connect()
if (self.user != None): if (self.user != None):
self.login() self.login()
else: self.reconnecttime = reconnecttime
self.reconnecttime = 0 self.idlereconnect = idlereconnect
def lasttime(self, parameter): def lasttime(self, parameter):
# #
@@ -112,8 +116,8 @@ Opens the connection and logs in.
connects to lmw with tcp using the values of the object creation. connects to lmw with tcp using the values of the object creation.
""" """
try: try:
self._tcp = socket.create_connection((self.host, self.port)) self._tcp = socket.create_connection((self.host, self.port))
self._connecttime = time.time() self._connecttime = time.time()
except Exception as e: except Exception as e:
self.log.error("LmwSip.connect(%s, %s) failed: %s", self.log.error("LmwSip.connect(%s, %s) failed: %s",
self.host, self.port, e) self.host, self.port, e)
@@ -142,29 +146,12 @@ connects to lmw with tcp using the values of the object creation.
pass pass
self._socket = None self._socket = None
def reconnectcheck(self):
"""Checks if the connection is longer open than the reconnect time.
After this time a logout is sent and a new connection is created.
This prevents the 10 minute server timeout"""
ct = time.time() - self._connecttime
if (self.reconnecttime > 0) and (ct > self.reconnecttime):
self.log.debug("LmwSip.reconnectcheck: reconnect after %i seconds" % ct)
#
# Reset connect time to prevent reconnects
#
self._connecttime = time.time()
self.logout()
time.sleep(1)
self.connect()
self.login()
def send(self, sipcmd): def send(self, sipcmd):
"""send(sipcmd) """send(sipcmd)
send a sip command to the server send a sip command to the server
""" """
self.reconnectcheck() self._idletime = time.time()
if self._socket != None: if self._socket != None:
try: try:
logcmd = sipcmd.strip('\r') logcmd = sipcmd.strip('\r')
@@ -242,12 +229,38 @@ Raises a LmwLoginFailure exception on failure
# #
# TODO: Check connect # TODO: Check connect
# #
# Don't use: sendrecv with reconnect check!
#
self.send(li) self.send(li)
d = self.recv() d = self.recv()
if (d[0] != '!'): if (d[0] != '!'):
self.closesocket() self.closesocket()
raise LmwLoginFailure(self.user, d) raise LmwLoginFailure(self.user, d)
def reconnect(self):
self.logout()
time.sleep(1)
self.connect()
self.login()
def reconnectcheck(self):
"""reconnectcheck()
Checks if a reconnect is nessecery.
There are two timeouts:
The maxium connect time (reconnecttime)
The maxium idle time (idlereconnect)
"""
ct = time.time() - self._connecttime
if (self.reconnecttime > 0) and (ct > self.reconnecttime):
self.log.debug("LmwSip.reconnectcheck: reconnect after %i seconds" % ct)
self.reconnect()
it = time.time() - self._idletime
if (self.idlereconnect > 0) and (it > self.idlereconnect):
self.log.debug("LmwSip.reconnectcheck: idle reconnect after %i seconds" % it)
self.reconnect()
def sendrecv(self, cmd): def sendrecv(self, cmd):
"""sendrecv(cmd) """sendrecv(cmd)
@@ -256,6 +269,7 @@ retry on socket failure.
""" """
c = 0 c = 0
ret = "" ret = ""
self.reconnectcheck()
while (ret == "") and (c < 3): while (ret == "") and (c < 3):
try: try:
self.send(cmd) self.send(cmd)

View File

@@ -103,6 +103,11 @@ class lmwsipTest(unittest.TestCase):
sleep(2) sleep(2)
self.assertEqual(self.sip.sendrecv("LOGOUTCOUNT"), "1\r") self.assertEqual(self.sip.sendrecv("LOGOUTCOUNT"), "1\r")
def test_idlereconnect(self):
self.login(idlereconnect=1)
sleep(2)
self.assertEqual(self.sip.sendrecv("LOGOUTCOUNT"), "1\r")
def test_run(self): def test_run(self):
capturedOutput = io.StringIO() capturedOutput = io.StringIO()
sys.stdout = capturedOutput sys.stdout = capturedOutput