diff options
author | Christian Grothoff <christian@grothoff.org> | 2016-06-25 20:33:57 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2016-06-25 20:33:57 +0000 |
commit | 93cf4031938ca0f3d13ce491ba9be2c77763ffae (patch) | |
tree | f19ff21d13772eedc42d4f86d1b844891c3e3055 /src/testbed/testbed_api_barriers.c | |
parent | 98764c99d4e56c8520ac5d1f3a056649fa2137fc (diff) | |
download | gnunet-93cf4031938ca0f3d13ce491ba9be2c77763ffae.tar.gz gnunet-93cf4031938ca0f3d13ce491ba9be2c77763ffae.zip |
remove global variable for barriers, move into controller
Diffstat (limited to 'src/testbed/testbed_api_barriers.c')
-rw-r--r-- | src/testbed/testbed_api_barriers.c | 181 |
1 files changed, 15 insertions, 166 deletions
diff --git a/src/testbed/testbed_api_barriers.c b/src/testbed/testbed_api_barriers.c index 824dbcdba..0163c0ce4 100644 --- a/src/testbed/testbed_api_barriers.c +++ b/src/testbed/testbed_api_barriers.c | |||
@@ -41,48 +41,6 @@ | |||
41 | #define LOG_DEBUG(...) \ | 41 | #define LOG_DEBUG(...) \ |
42 | LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__); | 42 | LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__); |
43 | 43 | ||
44 | /** | ||
45 | * Handle for barrier | ||
46 | */ | ||
47 | struct GNUNET_TESTBED_Barrier | ||
48 | { | ||
49 | /** | ||
50 | * hashcode identifying this barrier in the hashmap | ||
51 | */ | ||
52 | struct GNUNET_HashCode key; | ||
53 | |||
54 | /** | ||
55 | * The controller handle given while initiliasing this barrier | ||
56 | */ | ||
57 | struct GNUNET_TESTBED_Controller *c; | ||
58 | |||
59 | /** | ||
60 | * The name of the barrier | ||
61 | */ | ||
62 | char *name; | ||
63 | |||
64 | /** | ||
65 | * The continuation callback to call when we have a status update on this | ||
66 | */ | ||
67 | GNUNET_TESTBED_barrier_status_cb cb; | ||
68 | |||
69 | /** | ||
70 | * the closure for the above callback | ||
71 | */ | ||
72 | void *cls; | ||
73 | |||
74 | /** | ||
75 | * Should the barrier crossed status message be echoed back to the controller? | ||
76 | */ | ||
77 | int echo; | ||
78 | }; | ||
79 | |||
80 | |||
81 | /** | ||
82 | * handle for hashtable of barrier handles | ||
83 | */ | ||
84 | static struct GNUNET_CONTAINER_MultiHashMap *barrier_map; | ||
85 | |||
86 | 44 | ||
87 | /** | 45 | /** |
88 | * Remove a barrier and it was the last one in the barrier hash map, destroy the | 46 | * Remove a barrier and it was the last one in the barrier hash map, destroy the |
@@ -90,133 +48,23 @@ static struct GNUNET_CONTAINER_MultiHashMap *barrier_map; | |||
90 | * | 48 | * |
91 | * @param barrier the barrier to remove | 49 | * @param barrier the barrier to remove |
92 | */ | 50 | */ |
93 | static void | 51 | void |
94 | barrier_remove (struct GNUNET_TESTBED_Barrier *barrier) | 52 | GNUNET_TESTBED_barrier_remove_ (struct GNUNET_TESTBED_Barrier *barrier) |
95 | { | 53 | { |
96 | GNUNET_assert (NULL != barrier_map); /* No barriers present */ | 54 | struct GNUNET_TESTBED_Controller *c = barrier->c; |
55 | |||
56 | GNUNET_assert (NULL != c->barrier_map); /* No barriers present */ | ||
97 | GNUNET_assert (GNUNET_OK == | 57 | GNUNET_assert (GNUNET_OK == |
98 | GNUNET_CONTAINER_multihashmap_remove (barrier_map, | 58 | GNUNET_CONTAINER_multihashmap_remove (c->barrier_map, |
99 | &barrier->key, | 59 | &barrier->key, |
100 | barrier)); | 60 | barrier)); |
101 | GNUNET_free (barrier->name); | 61 | GNUNET_free (barrier->name); |
102 | GNUNET_free (barrier); | 62 | GNUNET_free (barrier); |
103 | if (0 == GNUNET_CONTAINER_multihashmap_size (barrier_map)) | 63 | if (0 == GNUNET_CONTAINER_multihashmap_size (c->barrier_map)) |
104 | { | 64 | { |
105 | GNUNET_CONTAINER_multihashmap_destroy (barrier_map); | 65 | GNUNET_CONTAINER_multihashmap_destroy (c->barrier_map); |
106 | barrier_map = NULL; | 66 | c->barrier_map = NULL; |
107 | } | ||
108 | } | ||
109 | |||
110 | |||
111 | /** | ||
112 | * Validate #GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS message. | ||
113 | * | ||
114 | * @param cls the controller handle to determine the connection this message | ||
115 | * belongs to | ||
116 | * @param msg the barrier status message | ||
117 | * @return #GNUNET_OK if the message is valid; #GNUNET_SYSERR to tear it | ||
118 | * down signalling an error (message malformed) | ||
119 | */ | ||
120 | int | ||
121 | check_barrier_status_ (struct GNUNET_TESTBED_Controller *c, | ||
122 | const struct GNUNET_TESTBED_BarrierStatusMsg *msg) | ||
123 | { | ||
124 | uint16_t msize; | ||
125 | uint16_t name_len; | ||
126 | int status; | ||
127 | const char *name; | ||
128 | size_t emsg_len; | ||
129 | |||
130 | msize = ntohs (msg->header.size); | ||
131 | name = msg->data; | ||
132 | name_len = ntohs (msg->name_len); | ||
133 | |||
134 | if (sizeof (struct GNUNET_TESTBED_BarrierStatusMsg) + name_len + 1 > msize) | ||
135 | { | ||
136 | GNUNET_break_op (0); | ||
137 | return GNUNET_SYSERR; | ||
138 | } | ||
139 | if ('\0' != name[name_len]) | ||
140 | { | ||
141 | GNUNET_break_op (0); | ||
142 | return GNUNET_SYSERR; | ||
143 | } | ||
144 | status = ntohs (msg->status); | ||
145 | if (GNUNET_TESTBED_BARRIERSTATUS_ERROR == status) | ||
146 | { | ||
147 | emsg_len = msize - (sizeof (struct GNUNET_TESTBED_BarrierStatusMsg) + name_len | ||
148 | + 1); /* +1!? */ | ||
149 | if (0 == emsg_len) | ||
150 | { | ||
151 | GNUNET_break_op (0); | ||
152 | return GNUNET_SYSERR; | ||
153 | } | ||
154 | } | ||
155 | return GNUNET_OK; | ||
156 | } | ||
157 | |||
158 | |||
159 | /** | ||
160 | * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS messages | ||
161 | * | ||
162 | * @param c the controller handle to determine the connection this message | ||
163 | * belongs to | ||
164 | * @param msg the barrier status message | ||
165 | */ | ||
166 | void | ||
167 | handle_barrier_status_ (struct GNUNET_TESTBED_Controller *c, | ||
168 | const struct GNUNET_TESTBED_BarrierStatusMsg *msg) | ||
169 | { | ||
170 | struct GNUNET_TESTBED_Barrier *barrier; | ||
171 | char *emsg; | ||
172 | const char *name; | ||
173 | struct GNUNET_HashCode key; | ||
174 | size_t emsg_len; | ||
175 | int status; | ||
176 | uint16_t msize; | ||
177 | uint16_t name_len; | ||
178 | |||
179 | emsg = NULL; | ||
180 | barrier = NULL; | ||
181 | msize = ntohs (msg->header.size); | ||
182 | name = msg->data; | ||
183 | name_len = ntohs (msg->name_len); | ||
184 | LOG_DEBUG ("Received BARRIER_STATUS msg\n"); | ||
185 | status = ntohs (msg->status); | ||
186 | if (GNUNET_TESTBED_BARRIERSTATUS_ERROR == status) | ||
187 | { | ||
188 | status = -1; | ||
189 | emsg_len = msize - (sizeof (struct GNUNET_TESTBED_BarrierStatusMsg) + name_len | ||
190 | + 1); | ||
191 | emsg = GNUNET_malloc (emsg_len + 1); | ||
192 | memcpy (emsg, | ||
193 | msg->data + name_len + 1, | ||
194 | emsg_len); | ||
195 | } | ||
196 | if (NULL == barrier_map) | ||
197 | { | ||
198 | GNUNET_break_op (0); | ||
199 | goto cleanup; | ||
200 | } | ||
201 | GNUNET_CRYPTO_hash (name, name_len, &key); | ||
202 | barrier = GNUNET_CONTAINER_multihashmap_get (barrier_map, &key); | ||
203 | if (NULL == barrier) | ||
204 | { | ||
205 | GNUNET_break_op (0); | ||
206 | goto cleanup; | ||
207 | } | 67 | } |
208 | GNUNET_assert (NULL != barrier->cb); | ||
209 | if ((GNUNET_YES == barrier->echo) && | ||
210 | (GNUNET_TESTBED_BARRIERSTATUS_CROSSED == status)) | ||
211 | GNUNET_TESTBED_queue_message_ (c, GNUNET_copy_message (&msg->header)); | ||
212 | barrier->cb (barrier->cls, name, barrier, status, emsg); | ||
213 | if (GNUNET_TESTBED_BARRIERSTATUS_INITIALISED == status) | ||
214 | return; /* just initialised; skip cleanup */ | ||
215 | |||
216 | cleanup: | ||
217 | GNUNET_free_non_null (emsg); | ||
218 | if (NULL != barrier) | ||
219 | barrier_remove (barrier); | ||
220 | } | 68 | } |
221 | 69 | ||
222 | 70 | ||
@@ -254,10 +102,11 @@ GNUNET_TESTBED_barrier_init_ (struct GNUNET_TESTBED_Controller *controller, | |||
254 | name_len = strlen (name); | 102 | name_len = strlen (name); |
255 | GNUNET_assert (0 < name_len); | 103 | GNUNET_assert (0 < name_len); |
256 | GNUNET_CRYPTO_hash (name, name_len, &key); | 104 | GNUNET_CRYPTO_hash (name, name_len, &key); |
257 | if (NULL == barrier_map) | 105 | if (NULL == controller->barrier_map) |
258 | barrier_map = GNUNET_CONTAINER_multihashmap_create (3, GNUNET_YES); | 106 | controller->barrier_map = GNUNET_CONTAINER_multihashmap_create (3, GNUNET_YES); |
259 | if (GNUNET_YES == | 107 | if (GNUNET_YES == |
260 | GNUNET_CONTAINER_multihashmap_contains (barrier_map, &key)) | 108 | GNUNET_CONTAINER_multihashmap_contains (controller->barrier_map, |
109 | &key)) | ||
261 | { | 110 | { |
262 | GNUNET_break (0); | 111 | GNUNET_break (0); |
263 | return NULL; | 112 | return NULL; |
@@ -271,7 +120,7 @@ GNUNET_TESTBED_barrier_init_ (struct GNUNET_TESTBED_Controller *controller, | |||
271 | barrier->echo = echo; | 120 | barrier->echo = echo; |
272 | (void) memcpy (&barrier->key, &key, sizeof (struct GNUNET_HashCode)); | 121 | (void) memcpy (&barrier->key, &key, sizeof (struct GNUNET_HashCode)); |
273 | GNUNET_assert (GNUNET_OK == | 122 | GNUNET_assert (GNUNET_OK == |
274 | GNUNET_CONTAINER_multihashmap_put (barrier_map, | 123 | GNUNET_CONTAINER_multihashmap_put (controller->barrier_map, |
275 | &barrier->key, | 124 | &barrier->key, |
276 | barrier, | 125 | barrier, |
277 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)); | 126 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)); |
@@ -328,7 +177,7 @@ GNUNET_TESTBED_barrier_cancel (struct GNUNET_TESTBED_Barrier *barrier) | |||
328 | msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_CANCEL); | 177 | msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_CANCEL); |
329 | (void) memcpy (msg->name, barrier->name, strlen (barrier->name)); | 178 | (void) memcpy (msg->name, barrier->name, strlen (barrier->name)); |
330 | GNUNET_TESTBED_queue_message_ (barrier->c, &msg->header); | 179 | GNUNET_TESTBED_queue_message_ (barrier->c, &msg->header); |
331 | barrier_remove (barrier); | 180 | GNUNET_TESTBED_barrier_remove_ (barrier); |
332 | } | 181 | } |
333 | 182 | ||
334 | 183 | ||