comparison mcabber/src/jabglue.c @ 1001:dff25377c11f

Auto-reconnect after a network/server failure
author Mikael Berthe <mikael@lilotux.net>
date Mon, 06 Nov 2006 22:54:01 +0100
parents 9687ecc59303
children c8b1a52b2fd6
comparison
equal deleted inserted replaced
1000:76c03fafabd3 1001:dff25377c11f
34 #include "commands.h" 34 #include "commands.h"
35 35
36 #define JABBERPORT 5222 36 #define JABBERPORT 5222
37 #define JABBERSSLPORT 5223 37 #define JABBERSSLPORT 5223
38 38
39 #define RECONNECTION_TIMEOUT 60L
40
39 jconn jc; 41 jconn jc;
40 enum enum_jstate jstate; 42 enum enum_jstate jstate;
41 43
42 char imstatus2char[imstatus_size+1] = { 44 char imstatus2char[imstatus_size+1] = {
43 '_', 'o', 'i', 'f', 'd', 'n', 'a', '\0' 45 '_', 'o', 'i', 'f', 'd', 'n', 'a', '\0'
44 }; 46 };
45 47
48 static bool AutoConnection;
46 static time_t LastPingTime; 49 static time_t LastPingTime;
47 static unsigned int KeepaliveDelay; 50 static unsigned int KeepaliveDelay;
48 static enum imstatus mystatus = offline; 51 static enum imstatus mystatus = offline;
49 static gchar *mystatusmsg; 52 static gchar *mystatusmsg;
50 static unsigned char online; 53 static unsigned char online;
120 jstate = STATE_CONNECTING; 123 jstate = STATE_CONNECTING;
121 statehandler(0, -1); 124 statehandler(0, -1);
122 jab_start(jc); 125 jab_start(jc);
123 } 126 }
124 127
128 if (jc)
129 AutoConnection = true;
130
125 return jc; 131 return jc;
126 } 132 }
127 133
128 void jb_disconnect(void) 134 void jb_disconnect(void)
129 { 135 {
139 // Announce it to the user 145 // Announce it to the user
140 statehandler(jc, JCONN_STATE_OFF); 146 statehandler(jc, JCONN_STATE_OFF);
141 147
142 jab_delete(jc); 148 jab_delete(jc);
143 jc = NULL; 149 jc = NULL;
150 AutoConnection = false;
144 } 151 }
145 152
146 inline void jb_reset_keepalive() 153 inline void jb_reset_keepalive()
147 { 154 {
148 time(&LastPingTime); 155 time(&LastPingTime);
162 } 169 }
163 170
164 void jb_set_keepalive_delay(unsigned int delay) 171 void jb_set_keepalive_delay(unsigned int delay)
165 { 172 {
166 KeepaliveDelay = delay; 173 KeepaliveDelay = delay;
174 }
175
176 // check_connection()
177 // Check if we've been disconnected for a while (predefined timeout),
178 // and if so try to reconnect.
179 static void check_connection(void)
180 {
181 static time_t disconnection_timestamp = 0L;
182 time_t now;
183
184 // Are we totally disconnected?
185 if (jc && jc->state != JCONN_STATE_OFF) {
186 disconnection_timestamp = 0L;
187 return;
188 }
189
190 // Maybe we're voluntarily offline...
191 if (!AutoConnection)
192 return;
193
194 time(&now);
195 if (!disconnection_timestamp) {
196 disconnection_timestamp = now;
197 return;
198 }
199
200 // If the reconnection_timeout is reached, try to reconnect.
201 if (now > disconnection_timestamp + RECONNECTION_TIMEOUT) {
202 mcabber_connect();
203 disconnection_timestamp = 0L;
204 }
167 } 205 }
168 206
169 void jb_main() 207 void jb_main()
170 { 208 {
171 time_t now; 209 time_t now;
172 fd_set fds; 210 fd_set fds;
173 long timeout; 211 long timeout;
174 struct timeval tv; 212 struct timeval tv;
175 static time_t last_eviqs_check = 0; 213 static time_t last_eviqs_check = 0L;
176 214
177 if (!online) { 215 if (!online) {
178 safe_usleep(10000); 216 safe_usleep(10000);
217 check_connection();
179 return; 218 return;
180 } 219 }
181 220
182 if (jc && jc->state == JCONN_STATE_CONNECTING) { 221 if (jc && jc->state == JCONN_STATE_CONNECTING) {
183 safe_usleep(75000); 222 safe_usleep(75000);
1581 char *p, *r; 1620 char *p, *r;
1582 char *ustmsg; 1621 char *ustmsg;
1583 const char *rname; 1622 const char *rname;
1584 enum imstatus ust; 1623 enum imstatus ust;
1585 char bpprio; 1624 char bpprio;
1586 time_t timestamp = 0; 1625 time_t timestamp = 0L;
1587 xmlnode muc_packet; 1626 xmlnode muc_packet;
1588 1627
1589 rname = strchr(from, JID_RESOURCE_SEPARATOR); 1628 rname = strchr(from, JID_RESOURCE_SEPARATOR);
1590 if (rname) rname++; 1629 if (rname) rname++;
1591 1630
1660 char *p, *r, *s; 1699 char *p, *r, *s;
1661 xmlnode x; 1700 xmlnode x;
1662 char *body = NULL; 1701 char *body = NULL;
1663 char *enc = NULL; 1702 char *enc = NULL;
1664 char *tmp = NULL; 1703 char *tmp = NULL;
1665 time_t timestamp = 0; 1704 time_t timestamp = 0L;
1666 1705
1667 body = xmlnode_get_tag_data(xmldata, "body"); 1706 body = xmlnode_get_tag_data(xmldata, "body");
1668 1707
1669 p = xmlnode_get_tag_data(xmldata, "subject"); 1708 p = xmlnode_get_tag_data(xmldata, "subject");
1670 if (p != NULL) { 1709 if (p != NULL) {