Statistics
| Branch: | Revision:

napa-baselibs / tests / neighborlist / neighborlist.c @ 507372bb

History | View | Annotate | Download (2.81 KB)

1
#include        <stdio.h>
2
#include        <stdlib.h>
3
#include        <string.h>
4

    
5
#include        <confuse.h>
6
#include        <event2/event.h>
7
#include        <event2/buffer.h>
8
#include        <event2/http.h>
9
#include        <event2/http_struct.h>
10

    
11
#include        <napa.h>
12
#include        <napa_log.h>
13
#include        <repoclient.h>
14
#include        <neighborlist.h>
15

    
16
struct event_base *eventbase;
17

    
18
/* Configuration file parsing instructions. see neighborlist.cfg for an example */
19

    
20
cfg_opt_t opts_neighspec[] = {
21
        CFG_INT("desired_size", 5, CFGF_NONE),
22
        CFG_INT("update_period", 30, CFGF_NONE),
23
        CFG_END()
24
};
25
cfg_opt_t opts_repospec[] = {
26
        CFG_STR("server", "192.168.35.203:9832", CFGF_NONE),
27
        CFG_END()
28
};
29
cfg_opt_t neighborlist_opts[] = {
30
        CFG_SEC("repository", opts_repospec, CFGF_NONE),
31
        CFG_SEC("neighborlist", opts_neighspec, CFGF_NONE),
32
        CFG_END()
33
};
34

    
35
cfg_t *neighborlist_cfg;
36
const char *print_list(NeighborListEntry *list, int n);
37

    
38
void periodic_query(HANDLE h, void *arg) {
39
        HANDLE neighborlist = (HANDLE)arg;
40

    
41
        int size = cfg_getint(neighborlist_cfg, "desired_size");
42
        NeighborListEntry *list = calloc(sizeof(NeighborListEntry), size);
43
        
44
        int success = neighborlist_query(neighborlist, list, &size);
45
        if (!success) info("%d peers: %s", size, print_list(list, size));
46
}
47

    
48

    
49
int main(int argc, char *argv[]) {
50
        /* Check for usage (arguments) */
51
        if (argc < 2) {
52
                fprintf(stderr, "Usage: %s neighborlist.cfg\n", argv[0]);
53
                exit(1);
54
        }
55
        /* Initialize libevent and logging */
56
        eventbase = event_base_new();
57
        napaInitLog(-1, NULL, NULL);
58
        repInit("");
59

    
60
        /* Parse config file and init repoclient with the "repository" cfg section */
61
        cfg_t *cfg = cfg_init(neighborlist_opts, CFGF_NONE);
62
        if(cfg_parse(cfg, argv[1]))
63
                fatal("Unable to parse config file %s", argv[1]);
64

    
65
        neighborlist_cfg = cfg_getsec(cfg, "neighborlist");
66

    
67
        HANDLE repoclient = repOpen(cfg_getstr(cfg_getsec(cfg, "repository"), "server"),0);
68
        if (repoclient == NULL) fatal("Unable to initialize repoclient");
69

    
70
        HANDLE neighborlist = neighborlist_init(repoclient, 
71
                cfg_getint(neighborlist_cfg, "desired_size"),
72
                cfg_getint(neighborlist_cfg, "update_period"), NULL, NULL, NULL);
73

    
74
        struct timeval begin = { 5, 0 };
75
        napaSchedulePeriodic(&begin, 1.0/10.0, periodic_query, neighborlist);
76

    
77

    
78
        event_base_loop(eventbase, 0);
79

    
80
        neighborlist_close(neighborlist);
81
        repClose(repoclient);
82

    
83
        cfg_free(cfg);
84
        event_base_free(eventbase);
85
}
86

    
87
/** Helper to print a string list */
88
const char *print_list(NeighborListEntry *list, int n) {
89
        static char buffer[4096];
90

    
91
        struct timeval now;
92
        gettimeofday(&now, NULL);
93

    
94
        strcpy(buffer, "[ ");
95
        int i;
96
        for (i = 0; i != n; i++) {
97
                if (i) strcat(buffer, ", ");
98
                strcat(buffer, list[i].peer);
99
                char buf[32];
100
                sprintf(buf, "(%d)", (int)(now.tv_sec - list[i].update_timestamp.tv_sec));
101
                strcat(buffer, buf);
102
        }
103
        strcat(buffer, " ]");
104
        return buffer;
105
}
106

    
107