summaryrefslogtreecommitdiff
path: root/core/ping.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-08-05 20:35:47 -0400
committerirungentoo <irungentoo@gmail.com>2013-08-05 20:35:47 -0400
commit524cf1895413026f528d9c59d16755a066c56f1c (patch)
tree42b4bdc2e23430df593efecebf1afe50c026eec9 /core/ping.c
parentc9a597b794291492a6ac5251119615951b5b2f5d (diff)
Fixed style and is_pinging().
Diffstat (limited to 'core/ping.c')
-rw-r--r--core/ping.c110
1 files changed, 59 insertions, 51 deletions
diff --git a/core/ping.c b/core/ping.c
index 8a7d534f..ffabe221 100644
--- a/core/ping.c
+++ b/core/ping.c
@@ -15,9 +15,9 @@
15#define PING_TIMEOUT 5 // 5s 15#define PING_TIMEOUT 5 // 5s
16 16
17typedef struct { 17typedef struct {
18 IP_Port ipp; 18 IP_Port ipp;
19 uint64_t id; 19 uint64_t id;
20 uint64_t timestamp; 20 uint64_t timestamp;
21} pinged_t; 21} pinged_t;
22 22
23static pinged_t pings[PING_NUM_MAX]; 23static pinged_t pings[PING_NUM_MAX];
@@ -25,71 +25,79 @@ static size_t num_pings;
25static size_t pos_pings; 25static size_t pos_pings;
26 26
27 27
28void init_ping() { 28void init_ping()
29 num_pings = 0; 29{
30 pos_pings = 0; 30 num_pings = 0;
31 pos_pings = 0;
31} 32}
32 33
33static bool is_timeout(uint64_t time) { 34static bool is_timeout(uint64_t time)
34 return (time + PING_TIMEOUT) < now(); 35{
36 return (time + PING_TIMEOUT) < now();
35} 37}
36 38
37static void remove_timeouts() { // O(n) 39static void remove_timeouts() // O(n)
38 size_t i, id; 40{
39 size_t new_pos = pos_pings; 41 size_t i, id;
40 size_t new_num = num_pings; 42 size_t new_pos = pos_pings;
41 43 size_t new_num = num_pings;
42 // Loop through buffer, oldest first 44
43 for(i=0; i<num_pings; i++) { 45 // Loop through buffer, oldest first
44 id = (pos_pings + i) % PING_NUM_MAX; 46 for (i=0; i<num_pings; i++) {
45 47 id = (pos_pings + i) % PING_NUM_MAX;
46 if(is_timeout(pings[id].timestamp)) { 48
47 new_pos++; 49 if(is_timeout(pings[id].timestamp)) {
48 new_num--; 50 new_pos++;
51 new_num--;
52 }
53 // Break here because list is sorted.
54 else
55 break;
49 } 56 }
50 // Break here because list is sorted.
51 else
52 break;
53 }
54 57
55 num_pings = new_num; 58 num_pings = new_num;
56 pos_pings = new_pos % PING_NUM_MAX; 59 pos_pings = new_pos % PING_NUM_MAX;
57} 60}
58 61
59uint64_t add_ping(IP_Port ipp) { // O(n) 62uint64_t add_ping(IP_Port ipp) // O(n)
60 size_t p; 63{
61 64 size_t p;
62 remove_timeouts();
63 65
64 // Remove oldest ping if full buffer 66 remove_timeouts();
65 if(num_pings == PING_NUM_MAX) { 67
66 num_pings--; 68 // Remove oldest ping if full buffer
67 pos_pings = (pos_pings + 1) % PING_NUM_MAX; 69 if (num_pings == PING_NUM_MAX) {
68 } 70 num_pings--;
71 pos_pings = (pos_pings + 1) % PING_NUM_MAX;
72 }
69 73
70 // Insert new ping at end of list 74 // Insert new ping at end of list
71 p = (pos_pings + num_pings) % PING_NUM_MAX; 75 p = (pos_pings + num_pings) % PING_NUM_MAX;
72 76
73 pings[p].ipp = ipp; 77 pings[p].ipp = ipp;
74 pings[p].timestamp = now(); 78 pings[p].timestamp = now();
75 pings[p].id = random_64b(); 79 pings[p].id = random_64b();
76 80
77 num_pings++; 81 num_pings++;
78 return pings[p].id; 82 return pings[p].id;
79} 83}
80 84
81bool is_pinging(IP_Port ipp, uint64_t ping_id) { // O(n) 85bool is_pinging(IP_Port ipp, uint64_t ping_id) // O(n) TODO: replace this with something else.
82 size_t i, id; 86{
87 if (ipp.ip.i == 0 && ping_id == 0)
88 return false;
89
90 size_t i, id;
83 91
84 remove_timeouts(); 92 remove_timeouts();
85 93
86 for(i=0; i<num_pings; i++) { 94 for (i=0; i<num_pings; i++) {
87 id = (pos_pings + i) % PING_NUM_MAX; 95 id = (pos_pings + i) % PING_NUM_MAX;
88 96
89 if(ipp_eq(pings[id].ipp, ipp) && pings[id].id == ping_id) { 97 if ((ipp_eq(pings[id].ipp, ipp) || ipp.ip.i == 0) && (pings[id].id == ping_id || ping_id == 0)) {
90 return true; 98 return true;
99 }
91 } 100 }
92 }
93 101
94 return false; 102 return false;
95} 103}