From 78abdd0db2890bd97b5ad7f82d6a7b5767c2fab6 Mon Sep 17 00:00:00 2001 From: codesprint Date: Tue, 4 Nov 2014 09:29:44 +0800 Subject: [PATCH 1/2] addresses issue #18, hash with addr as well as id --- AUTHORS | 1 + include/tube.h | 9 +++++++++ src/tube.c | 35 ++++++++++++++++++++++++++++++++--- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/AUTHORS b/AUTHORS index d30782a..8ec125e 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,2 +1,3 @@ Pål-Erik Martinsen Joe Hildeabrand +Peng Fan \ No newline at end of file diff --git a/include/tube.h b/include/tube.h index 562b184..001ea67 100644 --- a/include/tube.h +++ b/include/tube.h @@ -36,6 +36,12 @@ typedef struct _tube_manager tube_manager; typedef struct _tube tube; +typedef struct _tuple +{ + uint64_t id; + struct sockaddr peer; +} tuple; + typedef ssize_t (*tube_sendmsg_func)(int socket, const struct msghdr *message, int flags); @@ -102,3 +108,6 @@ LS_API void tube_get_id(tube *t, spud_tube_id *id); LS_API void tube_set_socket_functions(tube_sendmsg_func send, tube_recvmsg_func recv); + +unsigned int tube_hash_tuple(const void *t); +int tube_compare_tuple(const void *key1, const void *key2); diff --git a/src/tube.c b/src/tube.c index ffaeacf..4fd4e93 100644 --- a/src/tube.c +++ b/src/tube.c @@ -281,7 +281,7 @@ LS_API void tube_get_id(tube *t, spud_tube_id *id) { spud_copy_id(&t->id, id); } - +/* static unsigned int hash_id(const void *id) { // treat the 8 bytes of tube ID like a long long. uint64_t key = *(uint64_t *)id; @@ -295,8 +295,25 @@ static unsigned int hash_id(const void *id) { key = key + (key << 6); key = key ^ (key >> 22); return (unsigned int) key; -} +}*/ + +unsigned int tube_hash_tuple(const void *t) { + // currently only id and remote sockaddr are taken into consideration + const tuple *tup = t; + const struct sockaddr_in6 * p_in6 = (const struct sockaddr_in6 *) &tup->peer; + uint64_t key = (uint64_t)tup->id + (uint64_t)(*(uint64_t *)p_in6); + // from + // https://gist.github.com/badboy/6267743#64-bit-to-32-bit-hash-functions + key = (~key) + (key << 18); + key = key ^ (key >> 31); + key = key * 21; + key = key ^ (key >> 11); + key = key + (key << 6); + key = key ^ (key >> 22); + return (unsigned int) key; +} +/* static int compare_id(const void *key1, const void *key2) { int ret = 0; uint64_t k1 = *(uint64_t *)key1; @@ -307,6 +324,18 @@ static int compare_id(const void *key1, const void *key2) { ret = (k1==k2) ? 0 : 1; } return ret; +}*/ + +int tube_compare_tuple(const void *key1, const void *key2) { + int ret = 0; + uint64_t k1 = *(uint64_t *)key1; + uint64_t k2 = *(uint64_t *)key2; + if (k1tubes, err)) { + if (!ls_htable_create(buckets, tube_hash_tuple, tube_compare_tuple, &ret->tubes, err)) { goto cleanup; } From 29482cbf75aef4ee162081bc1d281ef56ca63521 Mon Sep 17 00:00:00 2001 From: fanpeng Date: Sun, 22 Mar 2015 10:55:12 +0800 Subject: [PATCH 2/2] rewrote tube_compare_tuple and added test to check distinct tubes --- include/tube.h | 1 + src/tube.c | 18 ++++++++++-------- test/tube_test.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/include/tube.h b/include/tube.h index 001ea67..be87fa3 100644 --- a/include/tube.h +++ b/include/tube.h @@ -36,6 +36,7 @@ typedef struct _tube_manager tube_manager; typedef struct _tube tube; +//id and remote peer addr used to distinguish tubes typedef struct _tuple { uint64_t id; diff --git a/src/tube.c b/src/tube.c index 4fd4e93..3100736 100644 --- a/src/tube.c +++ b/src/tube.c @@ -327,15 +327,17 @@ static int compare_id(const void *key1, const void *key2) { }*/ int tube_compare_tuple(const void *key1, const void *key2) { - int ret = 0; - uint64_t k1 = *(uint64_t *)key1; - uint64_t k2 = *(uint64_t *)key2; - if (k1