napa-baselibs / rep / repoclient_impl.h @ 5f3adef4
History | View | Annotate | Download (4.46 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 <grapes.h> |
23 |
#include <grapes_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 |
|
31 |
/** Struct maintaining streambuffer data. Used internally */
|
32 |
struct streambuffer {
|
33 |
FILE *stream; |
34 |
char *buffer;
|
35 |
size_t len; |
36 |
}; |
37 |
|
38 |
extern struct streambuffer publish_streambuffer; |
39 |
|
40 |
struct deferred_publish;
|
41 |
|
42 |
/** Internal structure to store a reposerver's connection data */
|
43 |
struct reposerver {
|
44 |
/** IP Address part of the server URI */
|
45 |
char *address;
|
46 |
/** Port part of the server URI */
|
47 |
unsigned short port; |
48 |
/** http connection from libevent */
|
49 |
struct evhttp_connection *evhttp_conn;
|
50 |
/** publish_delay */
|
51 |
int publish_delay;
|
52 |
/** publish buffer */
|
53 |
struct deferred_publish *publish_buffer;
|
54 |
/** publish buffer counter */
|
55 |
int publish_buffer_entries;
|
56 |
/* deferred publishs in transit */
|
57 |
struct deferred_publish *in_transit;
|
58 |
/** in-transit buffer counter */
|
59 |
int in_transit_entries;
|
60 |
/** magic value for paranoid people */
|
61 |
int magic;
|
62 |
}; |
63 |
|
64 |
/** Holds callback address and id for HTTP response callbacks, as well as a data field */
|
65 |
typedef struct { |
66 |
/** callback address */
|
67 |
void *cb;
|
68 |
/** callback user argument */
|
69 |
void *cbarg;
|
70 |
/** id of the request */
|
71 |
HANDLE id; |
72 |
/** the server this request belongs to */
|
73 |
struct reposerver *server;
|
74 |
/** arbitrary data field (e.g. used for maxresults) */
|
75 |
int data;
|
76 |
} request_data; |
77 |
|
78 |
/** Internal structure for holding batch publish records */
|
79 |
struct deferred_publish {
|
80 |
MeasurementRecord r; |
81 |
request_data *requestdata; |
82 |
}; |
83 |
|
84 |
/** Helper callback for operations returning a string-list (i.e. char **) to be called by libevent
|
85 |
|
86 |
@param req the http request struct
|
87 |
@param arg we store the pointer to the corresponding request_data
|
88 |
*/
|
89 |
void _stringlist_callback(struct evhttp_request *req,void *arg); |
90 |
|
91 |
|
92 |
/** Check the validity (i.e. it is open) of a repoclient handle
|
93 |
|
94 |
@param h the handle
|
95 |
@param fn name of the function check_handle is called from (to aid error reporting)
|
96 |
@return 1 if the handle is valid, 0 if not
|
97 |
*/
|
98 |
int check_handle(HANDLE h, const char *fn); |
99 |
|
100 |
/** Helper for HTTP GET queries
|
101 |
|
102 |
@param uri request string
|
103 |
@param callback callback function
|
104 |
@param cb_arg callback arg
|
105 |
*/
|
106 |
void make_request(const char *uri, void (*callback)(struct evhttp_request *, void *), void *cb_arg); |
107 |
|
108 |
/** Helper for HTTP POST queries
|
109 |
|
110 |
@param uri request string
|
111 |
@param data POST DATA (as 0-terminated string)
|
112 |
@param callback callback function
|
113 |
@param cb_arg callback arg
|
114 |
@retun 0 on success, <0 on error
|
115 |
*/
|
116 |
int make_post_request(const char *uri, const char *data, void (*callback)(struct evhttp_request *, void *), void *cb_arg); |
117 |
|
118 |
/** Parse a measurement record from the HTTP encoding
|
119 |
|
120 |
@param line the line as sent by the repo server
|
121 |
@return the record parsed or NULL if invalid. The record is dynamically allocated and needs to be freed by the caller.
|
122 |
*/
|
123 |
MeasurementRecord parse_measurementrecord(const char *line); |
124 |
|
125 |
/** print a Constraint array according to the HTTP encoding used in the reposerver communication
|
126 |
|
127 |
@param ranks array of Constraint s
|
128 |
@param len length of the constraints array
|
129 |
@return textual representation of the constraint struct in a static buffer
|
130 |
*/
|
131 |
const char *constraints2str(Constraint *constraints, int len); |
132 |
|
133 |
/** print a Ranking array according to the HTTP encoding used in the reposerver communication
|
134 |
|
135 |
@param ranks array of Ranking s
|
136 |
@param len length of the ranks array
|
137 |
@return textual representation of the rankings struct in a static buffer
|
138 |
*/
|
139 |
const char *rankings2str(Ranking *ranks, int len); |
140 |
|
141 |
/** print a MeasurementRecord
|
142 |
|
143 |
@param r the record
|
144 |
@return pointer to an internal buffer containing string representation
|
145 |
*/
|
146 |
const char *encode_measurementrecord(const MeasurementRecord *r); |
147 |
|
148 |
/** free the contents of a MeasurementRecord
|
149 |
|
150 |
@param r pointer to the record
|
151 |
*/
|
152 |
void free_measurementrecord(MeasurementRecord *r);
|
153 |
|
154 |
/** copy a measurementrecord for deferred publishing (by strdup-ing string elements)
|
155 |
|
156 |
@param dst destination MR address
|
157 |
@param src source MR
|
158 |
*/
|
159 |
void copy_measurementrecord(MeasurementRecord *dst, const MeasurementRecord *src); |
160 |
|
161 |
#endif
|