Statistics
| Branch: | Revision:

napa-baselibs / rep / repoclient_impl.h @ 507372bb

History | View | Annotate | Download (4.54 KB)

1
#ifndef _REPOCLIENT_IMPL_H
2
#define _REPOCLIENT_IMPL_H
3

    
4
/** @file repoclient_impl.h
5
 *
6
 * Local header file defining nuts 'n bolts for the repoclient implementation.
7
 *
8
 */
9

    
10
#define _GNU_SOURCE
11

    
12
#include        <stdio.h>
13
#include        <stdlib.h>
14
#include        <string.h>
15
#include        <math.h>
16

    
17
#include        <event2/event.h>
18
#include        <event2/buffer.h>
19
#include        <event2/http.h>
20
#include        <event2/http_struct.h>
21
 
22
#include        <napa.h>
23
#include        <napa_log.h>
24
#include        <mon.h>                // for MeasurementRecord
25

    
26
#include        <repoclient.h>
27

    
28
#define REPOSERVER_MAGIC        0xAABB
29
#define PUBLISH_BUFFER_SIZE        1024
30
#define SB_INCREMENT                 512
31

    
32
/** Struct maintaining streambuffer data. Used internally */
33
struct streambuffer {
34
#if !WIN32 && !MAC_OS
35
        FILE *stream;
36
#else
37
        size_t buffsize;
38
#endif
39
        char *buffer;
40
        size_t len;
41
};
42

    
43
extern struct streambuffer publish_streambuffer;
44

    
45
struct deferred_publish;
46

    
47
/** Internal structure to store a reposerver's connection data */
48
struct reposerver {
49
        /** IP Address part of the server URI */
50
        char *address;
51
        /** Port part of the server URI */
52
        unsigned short port;
53
        /** http connection from libevent */
54
        struct evhttp_connection *evhttp_conn;
55
        /** publish_delay */
56
        int publish_delay;
57
        /** publish buffer */
58
        struct deferred_publish *publish_buffer;
59
        /** publish buffer counter */
60
        int publish_buffer_entries;
61
        /* deferred publishs in transit */
62
        struct deferred_publish *in_transit;
63
        /** in-transit buffer counter */
64
        int in_transit_entries;
65
        /** magic value for paranoid people */
66
        int magic;
67
};
68

    
69
/** Holds callback address and id for HTTP response callbacks, as well as a data field */
70
typedef struct {
71
        /** callback address */
72
        void *cb;
73
        /** callback user argument */
74
        void *cbarg;
75
        /** id of the request */
76
        HANDLE id;
77
        /** the server this request belongs to */
78
        struct reposerver *server;
79
        /** arbitrary data field (e.g. used for maxresults) */
80
        int data;
81
} request_data;
82

    
83
/** Internal structure for holding batch publish records */
84
struct deferred_publish {
85
        MeasurementRecord r;
86
        request_data *requestdata;
87
};
88

    
89
/** Helper callback for operations returning a string-list (i.e. char **) to be called by libevent
90

91
  @param req the http request struct
92
  @param arg we store the pointer to the corresponding request_data
93
*/
94
void _stringlist_callback(struct evhttp_request *req,void *arg);
95

    
96

    
97
/** Check the validity (i.e. it is open) of a repoclient handle 
98
 
99
  @param h the handle
100
  @param fn name of the function check_handle is called from (to aid error reporting)
101
  @return 1 if the handle is valid, 0 if not
102
*/
103
int check_handle(HANDLE h, const char *fn);
104

    
105
/** Helper for HTTP GET queries 
106

107
  @param uri request string
108
  @param callback callback function
109
  @param cb_arg callback arg
110
*/
111
void make_request(const char *uri, void (*callback)(struct evhttp_request *, void *), void *cb_arg);
112

    
113
/** Helper for HTTP POST queries 
114

115
  @param uri request string
116
  @param data POST DATA (as 0-terminated string)
117
  @param callback callback function
118
  @param cb_arg callback arg
119
  @retun 0 on success, <0 on error
120
*/
121
int make_post_request(const char *uri, const char *data, void (*callback)(struct evhttp_request *, void *), void *cb_arg);
122

    
123
/** Parse a measurement record from the HTTP encoding 
124

125
  @param line the line as sent by the repo server
126
  @return the record parsed or NULL if invalid. The record is dynamically allocated and needs to be freed by the caller.
127
*/
128
MeasurementRecord parse_measurementrecord(const char *line);
129

    
130
/** print a Constraint array according to the HTTP encoding used in the reposerver communication 
131

132
  @param ranks array of Constraint s
133
  @param len length of the constraints array
134
  @return textual representation of the constraint struct in a static buffer
135
*/
136
const char *constraints2str(Constraint *constraints, int len);
137

    
138
/** print a Ranking array according to the HTTP encoding used in the reposerver communication 
139

140
  @param ranks array of Ranking s
141
  @param len length of the ranks array
142
  @return textual representation of the rankings struct in a static buffer
143
*/
144
const char *rankings2str(Ranking *ranks, int len);
145

    
146
/** print a MeasurementRecord 
147

148
  @param r the record
149
  @return pointer to an internal buffer containing string representation
150
*/
151
const char *encode_measurementrecord(const MeasurementRecord *r);
152

    
153
/** free the contents of a MeasurementRecord 
154

155
  @param r pointer to the record
156
*/
157
void free_measurementrecord(MeasurementRecord *r);
158

    
159
/** copy a measurementrecord for deferred publishing (by strdup-ing string elements)
160

161
  @param dst destination MR address
162
  @param src source MR
163
*/
164
void copy_measurementrecord(MeasurementRecord *dst, const MeasurementRecord *src);
165

    
166
#endif