[nicfit] do keepalive without iq:last but just send " "

good because google talk (and other servers) do not support iq:last
good because we do not disconnect ourselves if time changes
good because we do not disconnect ourselves on modal dialogs
good because even stpeter thinks that is the best way to do keepalives
good because we do not flood the server with iq:last
bad because I say it is good :D
This commit is contained in:
Nikos Kouremenos 2005-08-27 00:07:43 +00:00
parent 376146e229
commit 408aae5084
2 changed files with 18 additions and 32 deletions

View File

@ -127,8 +127,7 @@ class Connection:
self.new_account_info = None self.new_account_info = None
self.bookmarks = [] self.bookmarks = []
self.on_purpose = False self.on_purpose = False
self.last_incoming = time.time() self.last_io = time.time()
self.keep_alive_sent = False
self.to_be_sent = [] self.to_be_sent = []
self.last_sent = [] self.last_sent = []
self.files_props = {} self.files_props = {}
@ -1163,8 +1162,7 @@ class Connection:
self.dispatch('ERROR_ANSWER', (id, jid_from, errmsg, errcode)) self.dispatch('ERROR_ANSWER', (id, jid_from, errmsg, errcode))
def _StanzaArrivedCB(self, con, obj): def _StanzaArrivedCB(self, con, obj):
self.last_incoming = time.time() self.last_io = time.time()
self.keep_alive_sent = False
def _event_dispatcher(self, realm, event, data): def _event_dispatcher(self, realm, event, data):
if realm == common.xmpp.NS_REGISTER: if realm == common.xmpp.NS_REGISTER:
@ -1312,7 +1310,7 @@ class Connection:
con.RegisterEventHandler(self._event_dispatcher) con.RegisterEventHandler(self._event_dispatcher)
if auth: if auth:
con.initRoster() con.initRoster()
self.last_incoming = time.time() self.last_io = time.time()
self.connected = 2 self.connected = 2
return con # return connection return con # return connection
else: else:
@ -1884,12 +1882,8 @@ class Connection:
self.to_be_sent.append(iq) self.to_be_sent.append(iq)
def send_keepalive(self): def send_keepalive(self):
# we received nothing for the last foo seconds (60 secs by default) # nothing received for the last foo seconds (60 secs by default)
hostname = gajim.config.get_per('accounts', self.name, self.to_be_sent.append(' ')
'hostname')
iq = common.xmpp.Iq('get', common.xmpp.NS_LAST, to = hostname)
self.to_be_sent.append(iq)
self.keep_alive_sent = True
def process(self, timeout): def process(self, timeout):
if not self.connection: if not self.connection:
@ -1908,28 +1902,18 @@ class Connection:
tosend = self.to_be_sent.pop(0) tosend = self.to_be_sent.pop(0)
self.connection.send(tosend) self.connection.send(tosend)
self.last_sent.append(time.time()) t = time.time()
self.last_io = t
self.last_sent.append(t)
try: try:
if gajim.config.get_per('accounts', self.name, # do we want keepalives?
'keep_alives_enabled'): # do we want keepalives? if gajim.config.get_per('accounts', self.name, 'keep_alives_enabled'):
keep_alive_every_foo_secs = gajim.config.get_per('accounts', t = gajim.config.get_per('accounts', self.name,
self.name,'keep_alive_every_foo_secs') 'keep_alive_every_foo_secs')
#should we send keepalive? # should we send keepalive?
if time.time() > (self.last_incoming + \ if time.time() > (self.last_io + t):
keep_alive_every_foo_secs) and not self.keep_alive_sent:
self.send_keepalive() self.send_keepalive()
# did the server reply to the keepalive? if no disconnect
keep_alive_disconnect_after_foo_secs = gajim.config.get_per(
'accounts', self.name,
'keep_alive_disconnect_after_foo_secs') # 2 mins by default
if time.time() > (self.last_incoming + \
keep_alive_disconnect_after_foo_secs):
self.connection.disconnect() # disconnect if no answer
pritext = _('Gajim disconnected you from %s') % self.name
sectext = _('%s seconds have passed and server did not reply to our keep-alive. If you believe such disconnection should not have happened, you can disable sending keep-alive packets by modifying this account.') % unicode(keep_alive_disconnect_after_foo_secs)
self.dispatch('ERROR', (pritext, sectext))
return
if self.connection: if self.connection:
self.connection.Process(timeout) self.connection.Process(timeout)
except: except:

View File

@ -142,8 +142,10 @@ class TCPsocket(PlugIn):
elif type(raw_data)<>type(''): raw_data = ustr(raw_data).encode('utf-8') elif type(raw_data)<>type(''): raw_data = ustr(raw_data).encode('utf-8')
try: try:
self._send(raw_data) self._send(raw_data)
self.DEBUG(raw_data,'sent') # Avoid printing messages that are empty keepalive packets.
self._owner.Dispatcher.Event('', DATA_SENT, raw_data) if raw_data.strip():
self.DEBUG(raw_data,'sent')
self._owner.Dispatcher.Event('', DATA_SENT, raw_data)
except: except:
self.DEBUG("Socket error while sending data",'error') self.DEBUG("Socket error while sending data",'error')
self._owner.disconnected() self._owner.disconnected()