Statistics
| Branch: | Revision:

napa-baselibs / rep / getpeers.c @ 5f3adef4

History | View | Annotate | Download (1.74 KB)

1
#define LOG_MODULE "[rep] "
2
#include        "repoclient_impl.h"
3

    
4
HANDLE repGetPeers(HANDLE rep, cb_repGetPeers cb, void *cbarg, int maxResults, Constraint *cons, int clen, 
5
        Ranking *ranks, int rlen, const char *ch) {
6
        if (!check_handle(rep, __FUNCTION__)) return NULL;
7
        debug("About to call getPeers with maxResults %d, constaints %s and rankings %s", 
8
                maxResults, constraints2str(cons, clen), rankings2str(ranks, rlen));
9

    
10
        char uri[10240];
11
        request_data *rd = (request_data *)malloc(sizeof(request_data));
12
        if (!rd) return NULL;
13
        rd->id = (void *)rd;
14
        rd->cb = cb;
15
        rd->cbarg = cbarg;
16
        rd->server = rep;
17
        rd->data = maxResults;
18

    
19
        sprintf(uri, "/GetPeers?maxresults=%d&%s%s%s",  
20
                maxResults, constraints2str(cons, clen), 
21
                (clen ? "&" : ""), rankings2str(ranks, rlen));
22
        if (ch) {
23
                if (clen + rlen > 0) strcat(uri, "&");
24
                strcat(uri, "channel=");
25
                strcat(uri, ch);
26
        }
27
        debug("Making getPeers request with URI %s", uri);
28
        
29
        make_request(uri, _stringlist_callback, (void *)rd);
30
        return (HANDLE)(rd);
31
}
32

    
33
const char *constraints2str(Constraint *cons, int len) {
34
        static char buf[1024];
35
        buf[0] = 0;
36
        if (len) strcat(buf, "constraints=");
37
        int i;
38

    
39
        for (i = 0; i != len; i++) {
40
                char tmp[1024] = "";
41

    
42
                if (i) strcat(buf, ";");
43
                if (cons[i].strValue) sprintf(tmp, "%s,%s", cons[i].published_name, cons[i].strValue);
44
                else sprintf(tmp, "%s,%f,%f", cons[i].published_name, (float)cons[i].minValue, (float)cons[i].maxValue);
45

    
46
                strcat(buf, tmp);
47
        }
48
        return buf;
49
}
50

    
51
const char *rankings2str(Ranking *ranks, int len) {
52
        static char buf[1024];
53
        buf[0] = 0;
54
        if (len) strcat(buf, "rankings=");
55
        int i;
56

    
57
        for (i = 0; i!= len; i++) {
58
        char tmp[128];
59

    
60
        if (i) strcat(tmp, ";");
61
        sprintf(tmp, "%s,%lf", ranks[i].published_name, ranks[i].weight);
62
        strcat(buf, tmp);
63
        }
64
return buf;
65
}
66