Procházet zdrojové kódy

server: use BAVL instead of HashTable for looking up a client by its ID

ambrop7 před 15 roky
rodič
revize
2efe68c9d1
2 změnil soubory, kde provedl 11 přidání a 46 odebrání
  1. 9 43
      server/server.c
  2. 2 3
      server/server.h

+ 9 - 43
server/server.c

@@ -52,7 +52,6 @@
 // BadVPN
 // BadVPN
 #include <misc/version.h>
 #include <misc/version.h>
 #include <misc/debug.h>
 #include <misc/debug.h>
-#include <misc/jenkins_hash.h>
 #include <misc/offset.h>
 #include <misc/offset.h>
 #include <misc/nsskey.h>
 #include <misc/nsskey.h>
 #include <misc/byteorder.h>
 #include <misc/byteorder.h>
@@ -163,9 +162,8 @@ peerid_t clients_nextid;
 // clients list
 // clients list
 LinkedList2 clients;
 LinkedList2 clients;
 
 
-// clients hash table by client ID
-HashTable clients_by_id;
-uint32_t clients_by_id_initval;
+// clients tree (by ID)
+BAVL clients_tree;
 
 
 // cleans everything up that can be cleaned in order to return
 // cleans everything up that can be cleaned in order to return
 // from the event loop and exit
 // from the event loop and exit
@@ -271,12 +269,6 @@ static peerid_t new_client_id (void);
 // finds a client by its ID
 // finds a client by its ID
 static struct client_data * find_client_by_id (peerid_t id);
 static struct client_data * find_client_by_id (peerid_t id);
 
 
-// clients by ID hash table key comparator
-static int clients_by_id_key_comparator (peerid_t *id1, peerid_t *id2);
-
-// clients by ID hash table hash function
-static int clients_by_id_hash_function (peerid_t *id, int modulo);
-
 // checks if two clients are allowed to communicate. May depend on the order
 // checks if two clients are allowed to communicate. May depend on the order
 // of the clients.
 // of the clients.
 static int clients_allowed (struct client_data *client1, struct client_data *client2);
 static int clients_allowed (struct client_data *client1, struct client_data *client2);
@@ -490,18 +482,8 @@ int main (int argc, char *argv[])
     // initialize clients linked list
     // initialize clients linked list
     LinkedList2_Init(&clients);
     LinkedList2_Init(&clients);
     
     
-    // initialize clients-by-id hash table
-    BRandom_randomize((uint8_t *)&clients_by_id_initval, sizeof(clients_by_id_initval));
-    if (!HashTable_Init(
-        &clients_by_id,
-        OFFSET_DIFF(struct client_data, id, table_node_id),
-        (HashTable_comparator)clients_by_id_key_comparator,
-        (HashTable_hash_function)clients_by_id_hash_function,
-        MAX_CLIENTS
-    )) {
-        BLog(BLOG_ERROR, "HashTable_Init failed");
-        goto fail6;
-    }
+    // initialize clients tree
+    BAVL_Init(&clients_tree, OFFSET_DIFF(struct client_data, id, tree_node), (BAVL_comparator)peerid_comparator, NULL);
     
     
     // initialize listeners
     // initialize listeners
     num_listeners = 0;
     num_listeners = 0;
@@ -521,7 +503,6 @@ fail7:
         num_listeners--;
         num_listeners--;
         Listener_Free(&listeners[num_listeners]);
         Listener_Free(&listeners[num_listeners]);
     }
     }
-    HashTable_Free(&clients_by_id);
 fail6:
 fail6:
     if (options.ssl) {
     if (options.ssl) {
         ASSERT_FORCE(PR_Close(model_prfd) == PR_SUCCESS)
         ASSERT_FORCE(PR_Close(model_prfd) == PR_SUCCESS)
@@ -626,9 +607,6 @@ void terminate (void)
         Listener_Free(&listeners[num_listeners]);
         Listener_Free(&listeners[num_listeners]);
     }
     }
     
     
-    // free clients hash table
-    HashTable_Free(&clients_by_id);
-    
     if (options.ssl) {
     if (options.ssl) {
         // free model
         // free model
         ASSERT_FORCE(PR_Close(model_prfd) == PR_SUCCESS)
         ASSERT_FORCE(PR_Close(model_prfd) == PR_SUCCESS)
@@ -973,7 +951,7 @@ void client_add (struct client_data *client)
     // link in
     // link in
     clients_num++;
     clients_num++;
     LinkedList2_Append(&clients, &client->list_node);
     LinkedList2_Append(&clients, &client->list_node);
-    ASSERT_EXECUTE(HashTable_Insert(&clients_by_id, &client->table_node_id))
+    ASSERT_EXECUTE(BAVL_Insert(&clients_tree, &client->tree_node, NULL))
     
     
     // init knowledge lists
     // init knowledge lists
     LinkedList2_Init(&client->know_out_list);
     LinkedList2_Init(&client->know_out_list);
@@ -1111,7 +1089,7 @@ void client_dealloc (struct client_data *client)
     BPending_Free(&client->dying_job);
     BPending_Free(&client->dying_job);
     
     
     // link out
     // link out
-    ASSERT_EXECUTE(HashTable_Remove(&clients_by_id, &client->id))
+    BAVL_Remove(&clients_tree, &client->tree_node);
     LinkedList2_Remove(&clients, &client->list_node);
     LinkedList2_Remove(&clients, &client->list_node);
     clients_num--;
     clients_num--;
     
     
@@ -1869,24 +1847,12 @@ peerid_t new_client_id (void)
 
 
 struct client_data * find_client_by_id (peerid_t id)
 struct client_data * find_client_by_id (peerid_t id)
 {
 {
-    HashTableNode *node;
-    if (!HashTable_Lookup(&clients_by_id, &id, &node)) {
+    BAVLNode *node;
+    if (!(node = BAVL_LookupExact(&clients_tree, &id))) {
         return NULL;
         return NULL;
     }
     }
-    struct client_data *client = UPPER_OBJECT(node, struct client_data, table_node_id);
-    ASSERT(client->id == id)
     
     
-    return client;
-}
-
-int clients_by_id_key_comparator (peerid_t *id1, peerid_t *id2)
-{
-    return (*id1 == *id2);
-}
-
-int clients_by_id_hash_function (peerid_t *id, int modulo)
-{
-    return (jenkins_lookup2_hash((uint8_t *)id, sizeof(*id), clients_by_id_initval) % modulo);
+    return UPPER_OBJECT(node, struct client_data, tree_node);
 }
 }
 
 
 int clients_allowed (struct client_data *client1, struct client_data *client2)
 int clients_allowed (struct client_data *client1, struct client_data *client2)

+ 2 - 3
server/server.h

@@ -24,7 +24,6 @@
 
 
 #include <protocol/scproto.h>
 #include <protocol/scproto.h>
 #include <structure/LinkedList2.h>
 #include <structure/LinkedList2.h>
-#include <structure/HashTable.h>
 #include <structure/BAVL.h>
 #include <structure/BAVL.h>
 #include <system/BSocket.h>
 #include <system/BSocket.h>
 #include <flow/StreamSocketSource.h>
 #include <flow/StreamSocketSource.h>
@@ -120,8 +119,8 @@ struct client_data {
     
     
     // node in clients linked list
     // node in clients linked list
     LinkedList2Node list_node;
     LinkedList2Node list_node;
-    // node in clients-by-id hash table
-    HashTableNode table_node_id;
+    // node in clients tree (by ID)
+    BAVLNode tree_node;
     
     
     // knowledge lists
     // knowledge lists
     LinkedList2 know_out_list;
     LinkedList2 know_out_list;