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,
host="sip-lmw.rws.nl", port=443, meetnet="LMW", ssl = True,
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])
user(optinal): Lmw user name
@@ -55,25 +55,29 @@ Opens the connection and logs in.
self.check_ssl = check_ssl
self.timeout = timeout
self.cleartelnet = cleartelnet
self.reconnecttime = reconnecttime
self.reconnecttime = 0
self.idlereconnect = 0
self._connecttime = time.time()
self._idletime = time.time()
self._socket = None
if (log != None):
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:
try:
self.log = logging.getLogger("lmwsip")
self.log.debug("LmwSip.init: Start log")
except Exception as 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):
self.connect()
if (self.user != None):
self.login()
else:
self.reconnecttime = 0
self.reconnecttime = reconnecttime
self.idlereconnect = idlereconnect
def lasttime(self, parameter):
#
@@ -142,29 +146,12 @@ connects to lmw with tcp using the values of the object creation.
pass
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):
"""send(sipcmd)
send a sip command to the server
"""
self.reconnectcheck()
self._idletime = time.time()
if self._socket != None:
try:
logcmd = sipcmd.strip('\r')
@@ -242,12 +229,38 @@ Raises a LmwLoginFailure exception on failure
#
# TODO: Check connect
#
# Don't use: sendrecv with reconnect check!
#
self.send(li)
d = self.recv()
if (d[0] != '!'):
self.closesocket()
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):
"""sendrecv(cmd)
@@ -256,6 +269,7 @@ retry on socket failure.
"""
c = 0
ret = ""
self.reconnectcheck()
while (ret == "") and (c < 3):
try:
self.send(cmd)

View File

@@ -103,6 +103,11 @@ class lmwsipTest(unittest.TestCase):
sleep(2)
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):
capturedOutput = io.StringIO()
sys.stdout = capturedOutput