mirror of
https://github.com/servalproject/serval-dna.git
synced 2024-12-18 20:57:56 +00:00
Publish and retrieve multiple entries for the same phone number
This commit is contained in:
parent
b7c088f2fd
commit
2fa5fbfee7
@ -23,13 +23,7 @@ static struct item *create_item(const char *key){
|
||||
return ret;
|
||||
}
|
||||
|
||||
static struct item *find_item(const char *key, int create){
|
||||
if (!root){
|
||||
if (create)
|
||||
root=create_item(key);
|
||||
return root;
|
||||
}
|
||||
|
||||
static struct item *find_item(const char *key){
|
||||
struct item *item = root;
|
||||
|
||||
while(item){
|
||||
@ -37,26 +31,33 @@ static struct item *find_item(const char *key, int create){
|
||||
if (c==0)
|
||||
return item;
|
||||
if (c<0){
|
||||
if (!item->_left){
|
||||
if (create)
|
||||
item->_left=create_item(key);
|
||||
return item->_left;
|
||||
}
|
||||
item = item->_left;
|
||||
}else{
|
||||
if (!item->_right){
|
||||
if (create)
|
||||
item->_right=create_item(key);
|
||||
return item->_right;
|
||||
}
|
||||
item = item->_right;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void store(char *key, char *value){
|
||||
struct item *item = find_item(key, 1);
|
||||
static void add_item(char *key, char *value){
|
||||
struct item *item = root, **last_ptr=&root;
|
||||
while(item){
|
||||
int c=strcmp(item->key, key);
|
||||
if (c==0){
|
||||
c=strcmp(item->value, value);
|
||||
if (c==0)
|
||||
return;
|
||||
}
|
||||
if (c<0){
|
||||
last_ptr = &item->_left;
|
||||
}else{
|
||||
last_ptr = &item->_right;
|
||||
}
|
||||
item = *last_ptr;
|
||||
}
|
||||
|
||||
*last_ptr = item = create_item(key);
|
||||
|
||||
strncpy(item->value,value,sizeof(item->value));
|
||||
item->value[sizeof(item->value) -1]=0;
|
||||
// expire after 20 minutes
|
||||
@ -65,14 +66,6 @@ static void store(char *key, char *value){
|
||||
fprintf(stderr, "PUBLISHED \"%s\" = \"%s\"\n", key, value);
|
||||
}
|
||||
|
||||
static const char *retrieve(char *key){
|
||||
struct item *item = find_item(key, 0);
|
||||
if (item && item->expires > gettime_ms()){
|
||||
return item->value;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void add_record(){
|
||||
int ttl;
|
||||
overlay_mdp_frame mdp;
|
||||
@ -100,7 +93,16 @@ static void add_record(){
|
||||
|
||||
char url[256];
|
||||
snprintf(url, sizeof(url), "sid://%s/%s|%s|%s", sid, did, did, name);
|
||||
store(did, url);
|
||||
add_item(did, url);
|
||||
}
|
||||
|
||||
static void respond(char *token, struct item *item){
|
||||
if (!item)
|
||||
return;
|
||||
respond(token, item->_left);
|
||||
if (item->expires > gettime_ms())
|
||||
printf("%s|%s|\n",token,item->value);
|
||||
respond(token, item->_right);
|
||||
}
|
||||
|
||||
static void process_line(char *line){
|
||||
@ -112,9 +114,7 @@ static void process_line(char *line){
|
||||
while(*p && *p!='|') p++;
|
||||
*p++=0;
|
||||
|
||||
const char *response = retrieve(did);
|
||||
if (response)
|
||||
printf("%s|%s|\n",token,response);
|
||||
respond(token, find_item(did));
|
||||
printf("DONE\n");
|
||||
fflush(stdout);
|
||||
}
|
||||
|
@ -30,11 +30,18 @@ setup() {
|
||||
setup_servald
|
||||
assert_no_servald_processes
|
||||
foreach_instance +A +B +C create_single_identity
|
||||
set_instance +D
|
||||
DIDD1=$DIDC
|
||||
NAMED1="Agent D Smith"
|
||||
create_identities 1
|
||||
SIDD=$SIDD1
|
||||
DIDD=$DIDD1
|
||||
NAMED=$NAMED1
|
||||
set_instance +A
|
||||
executeOk_servald config set dna.helper.executable "$servald_build_root/directory_service"
|
||||
executeOk_servald config set debug.dnahelper on
|
||||
foreach_instance +B +C executeOk_servald config set directory.service $SIDA
|
||||
start_servald_instances +A +B +C
|
||||
foreach_instance +B +C +D executeOk_servald config set directory.service $SIDA
|
||||
start_servald_instances +A +B +C +D
|
||||
}
|
||||
|
||||
teardown() {
|
||||
@ -49,7 +56,7 @@ is_published() {
|
||||
}
|
||||
|
||||
sent_directory_request() {
|
||||
grep "Sending directory registration" $LOGB || return 1
|
||||
grep "Sending directory registration" $LOGD || return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
@ -59,13 +66,15 @@ test_publish() {
|
||||
wait_until is_published
|
||||
stop_servald_server +B
|
||||
stop_servald_server +C
|
||||
stop_servald_server +D
|
||||
set_instance +A
|
||||
executeOk_servald dna lookup "$DIDB"
|
||||
assertStdoutLineCount '==' 1
|
||||
assertStdoutGrep --matches=1 "^sid://$SIDB/$DIDB:$DIDB:$NAMEB\$"
|
||||
executeOk_servald dna lookup "$DIDC"
|
||||
assertStdoutLineCount '==' 1
|
||||
assertStdoutLineCount '==' 2
|
||||
assertStdoutGrep --matches=1 "^sid://$SIDC/$DIDC:$DIDC:$NAMEC\$"
|
||||
assertStdoutGrep --matches=1 "^sid://$SIDD/$DIDD:$DIDD:$NAMED\$"
|
||||
return
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user