summaryrefslogtreecommitdiff
path: root/jabber-keepalive.el
blob: f4b936a4dbc5ae5d9f53e540ecb4c594afb1787f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
;; jabber-keepalive.el - try to detect lost connection

;; Copyright (C) 2004 - Magnus Henoch - mange@freemail.hu

;; This file is a part of jabber.el.

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

;;; These keepalive functions send a jabber:iq:time request to the
;;; server every X minutes, and considers the connection broken if
;;; they get no answer within Y seconds.

(defgroup jabber-keepalive nil
  "Keepalive functions try to detect lost connection"
  :group 'jabber)

(defcustom jabber-keepalive-interval 600
  "Interval in seconds between connection checks."
  :type 'integer
  :group 'jabber-keepalive)

(defcustom jabber-keepalive-timeout 20
  "Seconds to wait for response from server."
  :type 'integer
  :group 'jabber-keepalive)

(defvar jabber-keepalive-timer nil
  "Timer object for keepalive function")

(defvar jabber-keepalive-timeout-timer nil
  "Timer object for keepalive timeout function")

(defun jabber-keepalive-start ()
  "Activate keepalive"
  (interactive)

  (when jabber-keepalive-timer
    (jabber-keepalive-stop))

  (setq jabber-keepalive-timer
	(run-with-timer 5
			jabber-keepalive-interval
			'jabber-keepalive-do))
  (add-hook 'jabber-post-disconnect-hook 'jabber-keepalive-stop))

(defun jabber-keepalive-stop()
  "Deactivate keepalive"
  (interactive)

  (when jabber-keepalive-timer
    (cancel-timer jabber-keepalive-timer)
    (setq jabber-keepalive-timer nil)))

(defun jabber-keepalive-do ()
  (message "%s: sending keepalive packet" (current-time-string))
  (setq jabber-keepalive-timeout-timer
	(run-with-timer jabber-keepalive-timeout
			nil
			'jabber-keepalive-timeout))

  ;; Whether we get an error or not is not interesting.
  ;; Getting a response at all is.
  (jabber-send-iq jabber-server "get"
		  '(query ((xmlns . "jabber:iq:time")))
		  'jabber-keepalive-got-response nil
		  'jabber-keepalive-got-response nil))

(defun jabber-keepalive-got-response (&rest args)
  (message "%s: got keepalive response" (current-time-string))
  (cancel-timer jabber-keepalive-timeout-timer)
  (setq jabber-keepalive-timeout-timer nil))

(defun jabber-keepalive-timeout ()
  (message "%s: keepalive timeout, connection considered lost" (current-time-string))
  (cancel-timer jabber-keepalive-timer)
  (setq jabber-keepalive-timer nil)

  (run-hooks jabber-lost-connection-hook)
  (jabber-disconnect))

(provide 'jabber-keepalive)

;;; arch-tag: d19ca743-75a1-475f-9217-83bd18012146