~ubuntu-branches/ubuntu/raring/gift-gnutella/raring

« back to all changes in this revision

Viewing changes to src/gt_netorg.c

  • Committer: Bazaar Package Importer
  • Author(s): Göran Weinholt
  • Date: 2005-07-31 13:56:53 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050731135653-3i7bcwnrbe7wfd1i
Tags: 0.0.10.1-1
* New upstream version.
  - Fixes FTBFS with gcc-4.0 (closes: #286732).
* Updated debian/patches/update-gwebcaches.patch.
* Updated debian/patches/remove-too-old-check.patch.
* debian/control:
  - Change the encoding of my name to UTF-8.
  - Updated to Standards-Version: 3.6.2 (no changes).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * $Id: gt_netorg.c,v 1.41 2003/12/26 09:27:25 hipnod Exp $
 
2
 * $Id: gt_netorg.c,v 1.47 2005/01/04 15:00:51 mkern Exp $
3
3
 *
4
4
 * Copyright (C) 2001-2003 giFT project (gift.sourceforge.net)
5
5
 *
24
24
#include "gt_accept.h"
25
25
 
26
26
#include "gt_packet.h"
27
 
#include "gt_protocol.h"
28
27
 
29
28
#include "gt_node_cache.h"
30
29
#include "gt_web_cache.h"
34
33
/* how often we check the network's condition */
35
34
#define MAINTAIN_INTERVAL         (10 * SECONDS)
36
35
 
 
36
/* how often to check to disconnect idle nodes */
 
37
#define IDLE_DISCONNECT_INTERVAL  (2 * MINUTES)
 
38
 
37
39
/* how often to trim the node list */
38
40
#define CLEANUP_INTERVAL          (15 * MINUTES)
39
41
 
40
42
/* how often to clear indications of connecting to nodes */
41
43
#define RETRY_ALL_INTERVAL        (60 * MINUTES)
42
44
 
 
45
/* maximum number of unreplied pings before disconnecting from a node */
 
46
#define MAX_UNREPLIED_PINGS       10
 
47
 
43
48
/* how many connections attempts each maintain loop for nodes previously
44
49
 * registered */
45
50
#define TRY_CONNECT_NODE_LIST     gt_config_get_int("connect/node_list=3")
55
60
/* timer for disconnecting connections */
56
61
static timer_id   disconnect_timer;
57
62
 
 
63
/* timer for disconnecting idle nodes */
 
64
static timer_id   idle_disconnect_timer;
 
65
 
58
66
/* timer for cleaning up the node list */
59
67
static timer_id   cleanup_timer;
60
68
 
73
81
static GtNode *node_ping (TCPC *c, GtNode *node, GtPacket *packet)
74
82
{
75
83
        gt_packet_send (c, packet);
 
84
 
 
85
        /* ->pings_with_noreply gets set to zero when the node sends a pong */
 
86
        if (gt_packet_ttl (packet) == 1)
 
87
                node->pings_with_noreply++;
 
88
 
76
89
        return NULL;
77
90
}
78
91
 
80
93
{
81
94
        GtPacket  *packet;
82
95
 
83
 
        if (!(packet = gt_packet_new (GT_PING_REQUEST, ttl, NULL)))
 
96
        if (!(packet = gt_packet_new (GT_MSG_PING, ttl, NULL)))
84
97
                return;
85
98
 
86
99
        gt_conn_foreach (GT_CONN_FOREACH(node_ping), packet,
91
104
 
92
105
static void ping_hosts (time_t now)
93
106
{
94
 
        static time_t large_ping_time;
 
107
        static time_t last_ping;
 
108
        static time_t last_keep_alive;
 
109
        BOOL          need_connections;
95
110
        uint8_t       ttl;
96
 
        BOOL          need_connections;
97
 
 
98
 
        need_connections = BOOL_EXPR (gt_conn_need_connections (GT_NODE_ULTRA) > 0);
 
111
 
 
112
        need_connections = gt_conn_need_connections (GT_NODE_ULTRA);
 
113
 
 
114
        if (now - last_ping < 30 * SECONDS && !need_connections)
 
115
                return;
 
116
 
 
117
        last_ping = now;
99
118
 
100
119
        /* ping to get more hosts if we need connections */
101
 
        if (now - large_ping_time >= 30 * ESECONDS || need_connections)
102
 
        {
 
120
        if (now - last_keep_alive >= 1 * MINUTES)
 
121
        {
 
122
                /* do a keepalive */
 
123
                ttl = 1;
 
124
                last_keep_alive = now;
 
125
        }
 
126
        else
 
127
        {
 
128
                /* get more hosts */
103
129
                ttl = 7;
104
 
                large_ping_time = now;
105
 
        }
106
 
        else
107
 
        {
108
 
                /* just do a keepalive */
109
 
                ttl = 1;
110
130
        }
111
131
 
112
132
        ping_hosts_ttl (ttl);
135
155
        if (connected != last_connected)
136
156
        {
137
157
                GT->DBGFN (GT, "connected=%d nodes=%d", connected, 
138
 
                           gt_conn_length (GT_NODE_NONE, -1));
 
158
                           gt_conn_length (GT_NODE_NONE, GT_NODE_ANY));
139
159
                last_connected = connected;
140
160
        }
141
161
}
142
162
 
143
 
static int get_need_as_ultra (GtNodeClass klass)
 
163
static int get_need_as_ultra (gt_node_class_t klass)
144
164
{
145
165
        switch (klass)
146
166
        {
150
170
        }
151
171
}
152
172
 
153
 
static int get_need_as_leaf (GtNodeClass klass)
 
173
static int get_need_as_leaf (gt_node_class_t klass)
154
174
{
155
175
        switch (klass)
156
176
        {
160
180
        }
161
181
}
162
182
 
163
 
int gt_conn_need_connections (GtNodeClass klass)
 
183
int gt_conn_need_connections (gt_node_class_t klass)
164
184
{
165
185
        int connected;
166
186
        int desired;
179
199
        return desired - connected;
180
200
}
181
201
 
182
 
static void disconnect_hosts (GtNodeClass klass, int excess)
 
202
static void disconnect_hosts (gt_node_class_t klass, int excess)
183
203
{
184
204
        int connected;
185
205
 
358
378
        return total;
359
379
}
360
380
 
361
 
static void maintain_class (GtNodeClass klass, time_t now)
 
381
static void maintain_class (gt_node_class_t klass, time_t now)
362
382
{
363
383
        int   connected;
364
384
        int   need;
435
455
        }
436
456
}
437
457
 
 
458
static GtNode *disconnect_no_ping_replies (TCPC *c, GtNode *node, void *udata)
 
459
{
 
460
        if (node->pings_with_noreply < MAX_UNREPLIED_PINGS)
 
461
                return NULL;
 
462
 
 
463
        GT->DBGSOCK (GT, node->c, "%d unreplied pings. disconnecting",
 
464
                     node->pings_with_noreply);
 
465
 
 
466
        gt_node_disconnect (c);
 
467
        return NULL;
 
468
}
 
469
 
 
470
/*****************************************************************************/
 
471
 
438
472
/*
439
473
 * This is the main network maintainence function. All connections to the
440
474
 * network are initiated from here.
449
483
        if (!(GT_SELF->klass & GT_NODE_ULTRA))
450
484
                disconnect_no_query_route ();
451
485
 
452
 
        /* TODO: expire old nodes here? */
453
 
 
454
486
#if 0
455
487
        trace_list (connections);
456
488
#endif
468
500
        return TRUE;
469
501
}
470
502
 
 
503
static BOOL idle_disconnect (void *udata)
 
504
{
 
505
        gt_conn_foreach (GT_CONN_FOREACH(disconnect_no_ping_replies), NULL,
 
506
                         GT_NODE_NONE, GT_NODE_CONNECTED, 0);
 
507
        return TRUE;
 
508
}
 
509
 
471
510
static BOOL cleanup (void *udata)
472
511
{
473
512
        /* trim excess nodes */
509
548
 
510
549
        /* setup the links maintain timer */
511
550
        maintain_timer = timer_add (MAINTAIN_INTERVAL,
512
 
                                    (TimerCallback)maintain, NULL);
 
551
                                    maintain, NULL);
 
552
 
 
553
        idle_disconnect_timer = timer_add (IDLE_DISCONNECT_INTERVAL,
 
554
                                           idle_disconnect, NULL);
513
555
 
514
556
        cleanup_timer = timer_add (CLEANUP_INTERVAL,
515
 
                                   (TimerCallback)cleanup, NULL);
 
557
                                   cleanup, NULL);
516
558
 
517
559
        retry_all_timer = timer_add (RETRY_ALL_INTERVAL,
518
 
                                     (TimerCallback)retry_all, NULL);
 
560
                                     retry_all, NULL);
519
561
 
520
562
        /* call it now so we don't have to wait the first time */
521
563
        maintain (NULL);
529
571
        timer_remove_zero (&disconnect_timer);
530
572
 
531
573
        timer_remove_zero (&maintain_timer);
 
574
        timer_remove_zero (&idle_disconnect_timer);
532
575
        timer_remove_zero (&cleanup_timer);
533
576
        timer_remove_zero (&retry_all_timer);
534
577
}