Merged silc_1_0_branch to trunk.
[silc.git] / apps / irssi / src / silc / core / silc-lag.c
1 #include "module.h"
2 #include "signals.h"
3 #include "misc.h"
4 #include "settings.h"
5
6 #include "silc-servers.h"
7
8 #define SILC_CLIENT_LAG_PING_ID 0x1337
9
10 static int timeout_tag;
11 static void lag_event_pong(SILC_SERVER_REC *server, 
12                 SilcClientCommandReplyContext cmd);
13
14 static void lag_get(SILC_SERVER_REC *server)
15 {
16         SilcBuffer idp;
17         g_get_current_time(&server->lag_sent);
18         server->lag_last_check = time(NULL);
19
20         /* register pending callback & send ping */
21         silc_client_command_pending(server->conn, SILC_COMMAND_PING,
22                         SILC_CLIENT_LAG_PING_ID,
23                         (SilcCommandCb)lag_event_pong, (void *)server);
24         idp = silc_id_payload_encode(server->conn->remote_id, SILC_ID_SERVER);
25         silc_client_command_send(silc_client, server->conn,
26                         SILC_COMMAND_PING, SILC_CLIENT_LAG_PING_ID,
27                         1, 1, idp->data, idp->len);
28         silc_buffer_free(idp);
29 }
30
31 static void lag_event_pong(SILC_SERVER_REC *server,
32                            SilcClientCommandReplyContext cmd)
33 {
34         GTimeVal now;
35
36         if (cmd->error != SILC_STATUS_OK) {
37
38                 /* if the ping failed for some reason, try it again */
39                 lag_get(server);
40                 return;
41
42         }
43
44         if (server->lag_sent.tv_sec == 0) {
45                 /* not expecting lag reply.. */
46                 return;
47         }
48
49         g_get_current_time(&now);
50         server->lag = (int) get_timeval_diff(&now, &server->lag_sent);
51         memset(&server->lag_sent, 0, sizeof(server->lag_sent));
52
53         signal_emit("server lag", 1, server);
54 }
55
56 static int sig_check_lag(void)
57 {
58         GSList *tmp, *next;
59         time_t now;
60         int lag_check_time, max_lag;
61
62         lag_check_time = settings_get_int("lag_check_time");
63         max_lag = settings_get_int("lag_max_before_disconnect");
64
65         if (lag_check_time <= 0)
66                 return 1;
67
68         now = time(NULL);
69         for (tmp = servers; tmp != NULL; tmp = next) {
70                 SILC_SERVER_REC *rec = tmp->data;
71
72                 next = tmp->next;
73                 if (!IS_SILC_SERVER(rec))
74                         continue;
75
76                 if (rec->lag_sent.tv_sec != 0) {
77                         /* waiting for lag reply */
78                         if (max_lag > 1 && now-rec->lag_sent.tv_sec > max_lag) {
79                                 /* too much lag, disconnect */
80                                 signal_emit("server lag disconnect", 1, rec);
81                                 rec->connection_lost = TRUE;
82                                 server_disconnect((SERVER_REC *) rec);
83                         }
84                 } else if (rec->lag_last_check+lag_check_time < now &&
85                          rec->cmdcount == 0 && rec->connected) {
86                         /* no commands in buffer - get the lag */
87                         lag_get(rec);
88                 }
89         }
90
91         return 1;
92 }
93
94 void silc_lag_init(void)
95 {
96         /* silc-client will need those... silc-plugin uses irc defaults */
97         settings_add_int("misc", "lag_check_time", 60);
98         settings_add_int("misc", "lag_max_before_disconnect", 300); 
99
100         timeout_tag = g_timeout_add(1000, (GSourceFunc) sig_check_lag, NULL);
101 }
102
103 void silc_lag_deinit(void)
104 {
105         g_source_remove(timeout_tag);
106 }