-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpps-client-cat.c
68 lines (54 loc) · 1.59 KB
/
pps-client-cat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "network.h"
#include "client.h"
#include "node.h"
#include "system.h"
int main(int argc, char* argv[])
{
//initialize client
client_t client;
client_init_args_t cl_init;
cl_init.client = &client;
cl_init.argv = &argv;
cl_init.required_args = 2;
cl_init.supported_args = (TOTAL_SERVERS | GET_NEEDED | PUT_NEEDED);
cl_init.argc = argc;
error_code err = client_init(cl_init);
if(err != ERR_NONE) {
printf("FAIL\n");
return -1; // error in main
}
char* concatBuffer = malloc(MAX_MSG_ELEM_SIZE); //we will concat in this (buffer) array
if(concatBuffer == NULL) {
printf("FAIL\n");
return -1;
}
memset(concatBuffer, '\0', MAX_MSG_ELEM_SIZE);
pps_value_t valueBuffer = NULL;
size_t i = 0;
while(argv[i+1] != NULL) {//get all the values needed for concat
error_code res = network_get(client, argv[i], &valueBuffer);
if(res != ERR_NONE) {
free(concatBuffer);
printf("FAIL\n");
return -1;
}
if(strlen(concatBuffer) + strlen(valueBuffer) < MAX_MSG_ELEM_SIZE) {
strcat(concatBuffer, valueBuffer);//concat in the buffer
} else {
free(concatBuffer);
printf("FAIL\n");
return -1;
}
i++;
}
error_code res = network_put(client, argv[i], concatBuffer);//put the result in the DHT
if(res != ERR_NONE) {
free(concatBuffer);
printf("FAIL\n");
return -1;
}
printf("OK\n");
free(concatBuffer);
client_end(&client);
return 0;
}