From d62b4e1ea2963561a550e19c3b9d72a3a3f73ae7 Mon Sep 17 00:00:00 2001 From: Yann Leboulanger Date: Wed, 22 Jun 2005 14:54:02 +0000 Subject: [PATCH] NAT Timeout packet are now sent from connection.py so they will be configurable and won't be sent while we connect to a server --- src/common/connection.py | 19 +++++++++++++++++++ src/common/xmpp/dispatcher.py | 10 ---------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/common/connection.py b/src/common/connection.py index 61c92f859..54fea40a6 100644 --- a/src/common/connection.py +++ b/src/common/connection.py @@ -127,6 +127,8 @@ class Connection: self.myVCardID = [] self.bookmarks = [] self.on_purpose = False + self._lastIncome = time.time() + self._natSent = False self.password = gajim.config.get_per('accounts', name, 'password') if USE_GPG: self.gpg = GnuPG.GnuPG() @@ -592,6 +594,10 @@ class Connection: errcode = iq_obj.getErrorCode() jid_from = str(iq_obj.getFrom()) self.dispatch('ERROR_ANSWER', (jid_from, errmsg, errcode)) + + def _StanzaArrivedCB(self, con, obj): + self._lastIncome = time.time() + self._natSent = False def _event_dispatcher(self, realm, event, data): if realm == common.xmpp.NS_REGISTER: @@ -670,6 +676,9 @@ class Connection: con.RegisterHandler('iq', self._PrivateCB, 'result', common.xmpp.NS_PRIVATE) con.RegisterHandler('iq', self._ErrorCB, 'error') + con.RegisterHandler('iq', self._StanzaArrivedCB) + con.RegisterHandler('presence', self._StanzaArrivedCB) + con.RegisterHandler('message', self._StanzaArrivedCB) con.RegisterEventHandler(self._event_dispatcher) gajim.log.debug('Connected to server') @@ -1152,6 +1161,16 @@ class Connection: return if self.connected: try: + if time.time() > (self._lastIncome + 60) and not self._natSent: + # we received nothing since 1 minute + hostname = gajim.config.get_per('accounts', self.name, + 'hostname') + iq = common.xmpp.Iq('get', common.xmpp.NS_LAST, to = hostname) + self.connection.send(iq) + self._natSent = True + if time.time() > self._lastIncome + 105: #1 min + 45 sec for answer + self.connection.disconnect() + return self.connection.Process(timeout) except: gajim.log.debug('error appeared while processing xmpp:') diff --git a/src/common/xmpp/dispatcher.py b/src/common/xmpp/dispatcher.py index 74ecf5aae..4a05bde8f 100644 --- a/src/common/xmpp/dispatcher.py +++ b/src/common/xmpp/dispatcher.py @@ -35,8 +35,6 @@ class Dispatcher(PlugIn): PlugIn.__init__(self) DBG_LINE='dispatcher' self.handlers={} - self._lastIncome = time.time() - self._natSent = False self._expected={} self._defaultHandler=None self._eventHandler=None @@ -110,18 +108,10 @@ class Dispatcher(PlugIn): 1) length of processed data if some data were processed; 2) '0' string if no data were processed but link is alive; 3) 0 (zero) if underlying connection is closed.""" - if (time.time() > self._lastIncome + 60) and not self._natSent: #1 min - iq = Iq('get', NS_LAST, to=self._owner.Server) - self.send(iq) - self._natSent = True - if time.time() > self._lastIncome + 105: #1 min + 45 sec for answer - self._owner.disconnected() for handler in self._cycleHandlers: handler(self) if self._owner.Connection.pending_data(timeout): data=self._owner.Connection.receive() self.Stream.Parse(data) - self._lastIncome = time.time() - self._natSent = False return len(data) return '0' # It means that nothing is received but link is alive.