napa-baselibs / rep / utils.c @ 5f3adef4
History | View | Annotate | Download (3.19 KB)
1 |
#define LOG_MODULE "[rep] " |
---|---|
2 |
#include "repoclient_impl.h" |
3 |
|
4 |
/** Check the validity (i.e. it is open) of the handle from function fn */
|
5 |
int check_handle(HANDLE h, const char *fn) { |
6 |
struct reposerver *r = (struct reposerver *)h; |
7 |
|
8 |
if (!r) {
|
9 |
error("Trying to use a NULL reposerver handle in fn %s", fn);
|
10 |
return 0; |
11 |
} |
12 |
if (r->magic != REPOSERVER_MAGIC) {
|
13 |
error("Trying to use a corrupt reposerver handle in fn %s", fn);
|
14 |
return 0; |
15 |
} |
16 |
return 1; |
17 |
} |
18 |
|
19 |
const char *measurementrecord2str(const MeasurementRecord r) { |
20 |
return encode_measurementrecord(&r);
|
21 |
} |
22 |
|
23 |
|
24 |
/** Helper callback for operations returning a string-list (i.e. char **).
|
25 |
Such ops are ListMeasurementNames and GetPeers
|
26 |
*/
|
27 |
void _stringlist_callback(struct evhttp_request *req,void *arg) { |
28 |
if (req == NULL || arg == NULL) return; |
29 |
request_data *cbdata = (request_data *)arg; |
30 |
|
31 |
void (*user_cb)(HANDLE rep, HANDLE id, void *cbarg, char **result, int nResults) = cbdata->cb; |
32 |
struct reposerver *server = cbdata->server;
|
33 |
HANDLE id = cbdata->id; |
34 |
/* maxresults is stored in data */
|
35 |
int max = cbdata->data;
|
36 |
if (max <= 0) max = 10000; |
37 |
|
38 |
if (req->response_code != HTTP_OK) {
|
39 |
warn("Failed repository operation (id %p, error is %d %s): %s",
|
40 |
id, req->response_code, req->response_code_line, req->uri); |
41 |
size_t response_len = evbuffer_get_length(req->input_buffer); |
42 |
if (response_len) {
|
43 |
char *response = malloc(response_len + 1); |
44 |
evbuffer_remove(req->input_buffer, response, response_len); |
45 |
response[response_len] = 0;
|
46 |
debug("Response string (len %d): %s", response_len, response);
|
47 |
free(response); |
48 |
} |
49 |
if (user_cb) user_cb((HANDLE)server, id, cbdata->cbarg, NULL, 0); |
50 |
free(cbdata); |
51 |
return;
|
52 |
} |
53 |
|
54 |
char **result = calloc(max, sizeof(char *)); |
55 |
int i = 0; |
56 |
|
57 |
while (1) { |
58 |
char *line = evbuffer_readln(req->input_buffer, NULL, EVBUFFER_EOL_ANY); |
59 |
if (!line || i == max) break; |
60 |
result[i++] = strdup(line); |
61 |
if (i % 10000 == 0) { |
62 |
max += 10000;
|
63 |
result = realloc(result, max * sizeof(char *)); |
64 |
} |
65 |
} |
66 |
//info("cbarg in getpeers.c: %p", cbdata->cbarg);
|
67 |
if (user_cb) user_cb((HANDLE)server, id, cbdata->cbarg, result, i);
|
68 |
free(cbdata); |
69 |
} |
70 |
|
71 |
/** Free the contents of a MR */
|
72 |
void free_measurementrecord(MeasurementRecord *r) {
|
73 |
if (r->originator) free(r->originator);
|
74 |
if (r->targetA) free(r->targetA);
|
75 |
if (r->targetB) free(r->targetB);
|
76 |
if (r->published_name) free((void *)r->published_name); |
77 |
if (r->string_value) free(r->string_value);
|
78 |
if (r->channel) free((void *)r->channel); |
79 |
} |
80 |
|
81 |
/** Copy a MeasurementRecord by strdup-ing members */
|
82 |
void copy_measurementrecord(MeasurementRecord *dst, const MeasurementRecord *src) { |
83 |
if (src->originator) dst->originator = strdup(src->originator);
|
84 |
else dst->originator = NULL; |
85 |
|
86 |
if (src->targetA) dst->targetA = strdup(src->targetA);
|
87 |
else dst->targetA = NULL; |
88 |
|
89 |
if (src->targetB) dst->targetB = strdup(src->targetB);
|
90 |
else dst->targetB = NULL; |
91 |
|
92 |
if (src->published_name) dst->published_name = strdup(src->published_name);
|
93 |
else dst->published_name = NULL; |
94 |
|
95 |
dst->value = src->value; |
96 |
|
97 |
if (src->string_value) dst->string_value = strdup(src->string_value);
|
98 |
else dst->string_value = NULL; |
99 |
|
100 |
dst->timestamp = src->timestamp;; |
101 |
|
102 |
if (src->channel) dst->channel = strdup(src->channel);
|
103 |
else dst->channel = NULL;; |
104 |
} |
105 |
|
106 |
|
107 |
|