summaryrefslogtreecommitdiff
path: root/trackerlogic.c
diff options
context:
space:
mode:
authorerdgeist <>2007-01-04 16:14:21 +0000
committererdgeist <>2007-01-04 16:14:21 +0000
commit33eaf54da7409258bd0d78459eaca939cfdfe458 (patch)
treebb0a2ca75c0b1af0f069771fabb3fde6117af95a /trackerlogic.c
parente0a9c2a4aa4fc6b648ae20071c35797c4a103e42 (diff)
simplified binary_search, it always does a memcmp. began to implement dump knowledge
Diffstat (limited to 'trackerlogic.c')
-rw-r--r--trackerlogic.c50
1 files changed, 29 insertions, 21 deletions
diff --git a/trackerlogic.c b/trackerlogic.c
index 762ad69..083161d 100644
--- a/trackerlogic.c
+++ b/trackerlogic.c
@@ -19,25 +19,17 @@
19// 19//
20static ot_vector all_torrents[256]; 20static ot_vector all_torrents[256];
21 21
22// Helper functions for binary_find
23//
24int compare_hash( const void *hash1, const void *hash2 ) { return memcmp( hash1, hash2, sizeof( ot_hash )); }
25int compare_ip_port( const void *peer1, const void *peer2 ) { return memcmp( peer1, peer2, 6 ); }
26
27// This function gives us a binary search that returns a pointer, even if 22// This function gives us a binary search that returns a pointer, even if
28// no exact match is found. In that case it sets exactmatch 0 and gives 23// no exact match is found. In that case it sets exactmatch 0 and gives
29// calling functions the chance to insert data 24// calling functions the chance to insert data
30// 25//
31static void *binary_search( const void *key, const void *base, 26static void *binary_search( const void *key, const void *base, unsigned long member_count, const unsigned long member_size,
32 unsigned long member_count, const unsigned long member_size, 27 int compare_size, int *exactmatch ) {
33 int (*compar) (const void *, const void *),
34 int *exactmatch )
35{
36 ot_byte *lookat = ((ot_byte*)base) + member_size * (member_count >> 1); 28 ot_byte *lookat = ((ot_byte*)base) + member_size * (member_count >> 1);
37 *exactmatch = 1; 29 *exactmatch = 1;
38 30
39 while( member_count ) { 31 while( member_count ) {
40 int cmp = compar((void*)lookat, key); 32 int cmp = memcmp( lookat, key, compare_size);
41 if (cmp == 0) return (void *)lookat; 33 if (cmp == 0) return (void *)lookat;
42 if (cmp < 0) { 34 if (cmp < 0) {
43 base = (void*)(lookat + member_size); 35 base = (void*)(lookat + member_size);
@@ -48,7 +40,6 @@ static void *binary_search( const void *key, const void *base,
48 } 40 }
49 *exactmatch = 0; 41 *exactmatch = 0;
50 return (void*)lookat; 42 return (void*)lookat;
51
52} 43}
53 44
54// Converter function from memory to human readable hex strings 45// Converter function from memory to human readable hex strings
@@ -56,9 +47,8 @@ static void *binary_search( const void *key, const void *base,
56// 47//
57char ths[1+2*20];char*to_hex(ot_byte*s){char*m="0123456789ABCDEF";char*e=ths+40;char*t=ths;while(t<e){*t++=m[*s>>4];*t++=m[*s++&15];}*t=0;return ths;} 48char ths[1+2*20];char*to_hex(ot_byte*s){char*m="0123456789ABCDEF";char*e=ths+40;char*t=ths;while(t<e){*t++=m[*s>>4];*t++=m[*s++&15];}*t=0;return ths;}
58 49
59 50static void *vector_find_or_insert( ot_vector *vector, void *key, size_t member_size, int compare_size, int *exactmatch ) {
60static void *vector_find_or_insert( ot_vector *vector, void *key, size_t member_size, int(*compare_func)(const void*, const void*), int *exactmatch ) { 51 ot_byte *match = BINARY_FIND( key, vector->data, vector->size, member_size, compare_size, exactmatch );
61 ot_byte *match = BINARY_FIND( key, vector->data, vector->size, member_size, compare_func, exactmatch );
62 52
63 if( *exactmatch ) return match; 53 if( *exactmatch ) return match;
64 54
@@ -84,7 +74,7 @@ static int vector_remove_peer( ot_vector *vector, ot_peer *peer ) {
84 ot_peer *match; 74 ot_peer *match;
85 75
86 if( !vector->size ) return 0; 76 if( !vector->size ) return 0;
87 match = BINARY_FIND( peer, vector->data, vector->size, sizeof( ot_peer ), compare_ip_port, &exactmatch ); 77 match = BINARY_FIND( peer, vector->data, vector->size, sizeof( ot_peer ), OT_PEER_COMPARE_SIZE, &exactmatch );
88 78
89 if( !exactmatch ) return 0; 79 if( !exactmatch ) return 0;
90 exactmatch = OT_FLAG( match ) & PEER_FLAG_SEEDING ? 2 : 1; 80 exactmatch = OT_FLAG( match ) & PEER_FLAG_SEEDING ? 2 : 1;
@@ -110,7 +100,7 @@ static int vector_remove_torrent( ot_vector *vector, ot_hash *hash ) {
110 ot_torrent *match; 100 ot_torrent *match;
111 101
112 if( !vector->size ) return 0; 102 if( !vector->size ) return 0;
113 match = BINARY_FIND( hash, vector->data, vector->size, sizeof( ot_torrent ), compare_hash, &exactmatch ); 103 match = BINARY_FIND( hash, vector->data, vector->size, sizeof( ot_torrent ), OT_HASH_COMPARE_SIZE, &exactmatch );
114 104
115 if( !exactmatch ) return 0; 105 if( !exactmatch ) return 0;
116 free_peerlist( match->peer_list ); 106 free_peerlist( match->peer_list );
@@ -149,7 +139,7 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer ) {
149 ot_peer *peer_dest; 139 ot_peer *peer_dest;
150 ot_vector *torrents_list = &all_torrents[*hash[0]], *peer_pool; 140 ot_vector *torrents_list = &all_torrents[*hash[0]], *peer_pool;
151 141
152 torrent = vector_find_or_insert( torrents_list, (void*)hash, sizeof( ot_torrent ), compare_hash, &exactmatch ); 142 torrent = vector_find_or_insert( torrents_list, (void*)hash, sizeof( ot_torrent ), OT_HASH_COMPARE_SIZE, &exactmatch );
153 if( !torrent ) return NULL; 143 if( !torrent ) return NULL;
154 144
155 if( !exactmatch ) { 145 if( !exactmatch ) {
@@ -167,7 +157,7 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer ) {
167 clean_peerlist( torrent->peer_list ); 157 clean_peerlist( torrent->peer_list );
168 158
169 peer_pool = &torrent->peer_list->peers[0]; 159 peer_pool = &torrent->peer_list->peers[0];
170 peer_dest = vector_find_or_insert( peer_pool, (void*)peer, sizeof( ot_peer ), compare_ip_port, &exactmatch ); 160 peer_dest = vector_find_or_insert( peer_pool, (void*)peer, sizeof( ot_peer ), OT_PEER_COMPARE_SIZE, &exactmatch );
171 161
172 // If we hadn't had a match in current pool, create peer there and 162 // If we hadn't had a match in current pool, create peer there and
173 // remove it from all older pools 163 // remove it from all older pools
@@ -242,7 +232,7 @@ size_t return_scrape_for_torrent( ot_hash *hash, char *reply ) {
242 char *r = reply; 232 char *r = reply;
243 int exactmatch, peers = 0, seeds = 0, i; 233 int exactmatch, peers = 0, seeds = 0, i;
244 ot_vector *torrents_list = &all_torrents[*hash[0]]; 234 ot_vector *torrents_list = &all_torrents[*hash[0]];
245 ot_torrent *torrent = BINARY_FIND( hash, torrents_list->data, torrents_list->size, sizeof( ot_torrent ), compare_hash, &exactmatch ); 235 ot_torrent *torrent = BINARY_FIND( hash, torrents_list->data, torrents_list->size, sizeof( ot_torrent ), OT_HASH_COMPARE_SIZE, &exactmatch );
246 236
247 if( !exactmatch ) return 0; 237 if( !exactmatch ) return 0;
248 clean_peerlist( torrent->peer_list ); 238 clean_peerlist( torrent->peer_list );
@@ -261,7 +251,7 @@ size_t return_scrape_for_torrent( ot_hash *hash, char *reply ) {
261void remove_peer_from_torrent( ot_hash *hash, ot_peer *peer ) { 251void remove_peer_from_torrent( ot_hash *hash, ot_peer *peer ) {
262 int exactmatch, i; 252 int exactmatch, i;
263 ot_vector *torrents_list = &all_torrents[*hash[0]]; 253 ot_vector *torrents_list = &all_torrents[*hash[0]];
264 ot_torrent *torrent = BINARY_FIND( hash, torrents_list->data, torrents_list->size, sizeof( ot_torrent ), compare_hash, &exactmatch ); 254 ot_torrent *torrent = BINARY_FIND( hash, torrents_list->data, torrents_list->size, sizeof( ot_torrent ), OT_HASH_COMPARE_SIZE, &exactmatch );
265 255
266 if( !exactmatch ) return; 256 if( !exactmatch ) return;
267 257
@@ -279,6 +269,24 @@ void remove_peer_from_torrent( ot_hash *hash, ot_peer *peer ) {
279 } 269 }
280} 270}
281 271
272#if 0
273void dump_knowledge( void ) {
274 int ati, tli, pli;
275 for( ati = 0; ati<256; ++ati ) {
276 ot_vector *torrent_list = &all_torrents[ati];
277 for( tli = 0; tli<torrent_list->size; ++tli ) {
278 ot_torrent *torrent = &torrent_list->data[tli];
279 for( pool = 0; pool<OT_POOLS_COUNT; ++pool ) {
280 for( pli=0; pli<torrent->peer_list->peers[pool].size; ++pli ) {
281
282
283 }
284 }
285 }
286 }
287}
288#endif
289
282void cleanup_torrents( void ) { 290void cleanup_torrents( void ) {
283 291
284} 292}