diff options
author | irungentoo <irungentoo@gmail.com> | 2013-08-05 20:35:47 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-08-05 20:35:47 -0400 |
commit | 524cf1895413026f528d9c59d16755a066c56f1c (patch) | |
tree | 42b4bdc2e23430df593efecebf1afe50c026eec9 | |
parent | c9a597b794291492a6ac5251119615951b5b2f5d (diff) |
Fixed style and is_pinging().
-rw-r--r-- | core/ping.c | 110 | ||||
-rw-r--r-- | core/util.c | 25 |
2 files changed, 73 insertions, 62 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 | ||
17 | typedef struct { | 17 | typedef 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 | ||
23 | static pinged_t pings[PING_NUM_MAX]; | 23 | static pinged_t pings[PING_NUM_MAX]; |
@@ -25,71 +25,79 @@ static size_t num_pings; | |||
25 | static size_t pos_pings; | 25 | static size_t pos_pings; |
26 | 26 | ||
27 | 27 | ||
28 | void init_ping() { | 28 | void init_ping() |
29 | num_pings = 0; | 29 | { |
30 | pos_pings = 0; | 30 | num_pings = 0; |
31 | pos_pings = 0; | ||
31 | } | 32 | } |
32 | 33 | ||
33 | static bool is_timeout(uint64_t time) { | 34 | static bool is_timeout(uint64_t time) |
34 | return (time + PING_TIMEOUT) < now(); | 35 | { |
36 | return (time + PING_TIMEOUT) < now(); | ||
35 | } | 37 | } |
36 | 38 | ||
37 | static void remove_timeouts() { // O(n) | 39 | static 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 | ||
59 | uint64_t add_ping(IP_Port ipp) { // O(n) | 62 | uint64_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 | ||
81 | bool is_pinging(IP_Port ipp, uint64_t ping_id) { // O(n) | 85 | bool 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 | } |
diff --git a/core/util.c b/core/util.c index 6b40dad7..4ce9271e 100644 --- a/core/util.c +++ b/core/util.c | |||
@@ -11,21 +11,24 @@ | |||
11 | 11 | ||
12 | #include "network.h" | 12 | #include "network.h" |
13 | 13 | ||
14 | uint64_t now() { | 14 | uint64_t now() |
15 | return time(NULL); | 15 | { |
16 | return time(NULL); | ||
16 | } | 17 | } |
17 | 18 | ||
18 | uint64_t random_64b() { | 19 | uint64_t random_64b() |
19 | uint64_t r; | 20 | { |
21 | uint64_t r; | ||
20 | 22 | ||
21 | // This is probably not random enough? | 23 | // This is probably not random enough? |
22 | r = random_int(); | 24 | r = random_int(); |
23 | r <<= 32; | 25 | r <<= 32; |
24 | r |= random_int(); | 26 | r |= random_int(); |
25 | 27 | ||
26 | return r; | 28 | return r; |
27 | } | 29 | } |
28 | 30 | ||
29 | bool ipp_eq(IP_Port a, IP_Port b) { | 31 | bool ipp_eq(IP_Port a, IP_Port b) |
30 | return (a.ip.i == b.ip.i) && (a.port == b.port); | 32 | { |
33 | return (a.ip.i == b.ip.i) && (a.port == b.port); | ||
31 | } | 34 | } |