diff options
author | Christian Grothoff <christian@grothoff.org> | 2016-07-25 00:30:00 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2016-07-25 00:30:00 +0000 |
commit | d0701eb6315fc3f5aca74415bbf0ff7418409f87 (patch) | |
tree | fcacb97bbcca542ce9493001c825fd265cb8a801 /src/transport/transport_api_manipulation.c | |
parent | 9bfb8bff3fd253407530c76c7590b33bacf033f1 (diff) | |
download | gnunet-d0701eb6315fc3f5aca74415bbf0ff7418409f87.tar.gz gnunet-d0701eb6315fc3f5aca74415bbf0ff7418409f87.zip |
switching transport tests to new MQ-based transport API
Diffstat (limited to 'src/transport/transport_api_manipulation.c')
-rw-r--r-- | src/transport/transport_api_manipulation.c | 244 |
1 files changed, 244 insertions, 0 deletions
diff --git a/src/transport/transport_api_manipulation.c b/src/transport/transport_api_manipulation.c new file mode 100644 index 000000000..6325354cb --- /dev/null +++ b/src/transport/transport_api_manipulation.c | |||
@@ -0,0 +1,244 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2009-2013, 2016 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
18 | Boston, MA 02110-1301, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file transport/transport_api_manipulation.c | ||
23 | * @brief library to access the low-level P2P IO service | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_constants.h" | ||
29 | #include "gnunet_arm_service.h" | ||
30 | #include "gnunet_hello_lib.h" | ||
31 | #include "gnunet_protocols.h" | ||
32 | #include "gnunet_transport_service.h" | ||
33 | #include "transport.h" | ||
34 | |||
35 | #define LOG(kind,...) GNUNET_log_from (kind, "transport-api",__VA_ARGS__) | ||
36 | |||
37 | |||
38 | /** | ||
39 | * Handle for the transport service (includes all of the | ||
40 | * state for the transport service). | ||
41 | */ | ||
42 | struct GNUNET_TRANSPORT_ManipulationHandle | ||
43 | { | ||
44 | |||
45 | /** | ||
46 | * My client connection to the transport service. | ||
47 | */ | ||
48 | struct GNUNET_MQ_Handle *mq; | ||
49 | |||
50 | /** | ||
51 | * My configuration. | ||
52 | */ | ||
53 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
54 | |||
55 | /** | ||
56 | * ID of the task trying to reconnect to the service. | ||
57 | */ | ||
58 | struct GNUNET_SCHEDULER_Task *reconnect_task; | ||
59 | |||
60 | /** | ||
61 | * Delay until we try to reconnect. | ||
62 | */ | ||
63 | struct GNUNET_TIME_Relative reconnect_delay; | ||
64 | |||
65 | /** | ||
66 | * Reconnect in progress | ||
67 | */ | ||
68 | int reconnecting; | ||
69 | }; | ||
70 | |||
71 | |||
72 | /** | ||
73 | * Function that will schedule the job that will try | ||
74 | * to connect us again to the client. | ||
75 | * | ||
76 | * @param h transport service to reconnect | ||
77 | */ | ||
78 | static void | ||
79 | disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_ManipulationHandle *h); | ||
80 | |||
81 | |||
82 | /** | ||
83 | * Generic error handler, called with the appropriate | ||
84 | * error code and the same closure specified at the creation of | ||
85 | * the message queue. | ||
86 | * Not every message queue implementation supports an error handler. | ||
87 | * | ||
88 | * @param cls closure with the `struct GNUNET_TRANSPORT_ManipulationHandle *` | ||
89 | * @param error error code | ||
90 | */ | ||
91 | static void | ||
92 | mq_error_handler (void *cls, | ||
93 | enum GNUNET_MQ_Error error) | ||
94 | { | ||
95 | struct GNUNET_TRANSPORT_ManipulationHandle *h = cls; | ||
96 | |||
97 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
98 | "Error receiving from transport service, disconnecting temporarily.\n"); | ||
99 | h->reconnecting = GNUNET_YES; | ||
100 | disconnect_and_schedule_reconnect (h); | ||
101 | } | ||
102 | |||
103 | |||
104 | /** | ||
105 | * Try again to connect to transport service. | ||
106 | * | ||
107 | * @param cls the handle to the transport service | ||
108 | */ | ||
109 | static void | ||
110 | reconnect (void *cls) | ||
111 | { | ||
112 | struct GNUNET_TRANSPORT_ManipulationHandle *h = cls; | ||
113 | struct GNUNET_MQ_MessageHandler handlers[] = { | ||
114 | GNUNET_MQ_handler_end () | ||
115 | }; | ||
116 | struct GNUNET_MQ_Envelope *env; | ||
117 | struct StartMessage *s; | ||
118 | |||
119 | h->reconnect_task = NULL; | ||
120 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
121 | "Connecting to transport service.\n"); | ||
122 | GNUNET_assert (NULL == h->mq); | ||
123 | h->reconnecting = GNUNET_NO; | ||
124 | h->mq = GNUNET_CLIENT_connecT (h->cfg, | ||
125 | "transport", | ||
126 | handlers, | ||
127 | &mq_error_handler, | ||
128 | h); | ||
129 | if (NULL == h->mq) | ||
130 | return; | ||
131 | env = GNUNET_MQ_msg (s, | ||
132 | GNUNET_MESSAGE_TYPE_TRANSPORT_START); | ||
133 | GNUNET_MQ_send (h->mq, | ||
134 | env); | ||
135 | } | ||
136 | |||
137 | |||
138 | /** | ||
139 | * Function that will schedule the job that will try | ||
140 | * to connect us again to the client. | ||
141 | * | ||
142 | * @param h transport service to reconnect | ||
143 | */ | ||
144 | static void | ||
145 | disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_ManipulationHandle *h) | ||
146 | { | ||
147 | GNUNET_assert (NULL == h->reconnect_task); | ||
148 | if (NULL != h->mq) | ||
149 | { | ||
150 | GNUNET_MQ_destroy (h->mq); | ||
151 | h->mq = NULL; | ||
152 | } | ||
153 | h->reconnect_task = | ||
154 | GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, | ||
155 | &reconnect, | ||
156 | h); | ||
157 | h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay); | ||
158 | } | ||
159 | |||
160 | |||
161 | /** | ||
162 | * Set transport metrics for a peer and a direction. | ||
163 | * | ||
164 | * @param handle transport handle | ||
165 | * @param peer the peer to set the metric for | ||
166 | * @param prop the performance metrics to set | ||
167 | * @param delay_in inbound delay to introduce | ||
168 | * @param delay_out outbound delay to introduce | ||
169 | * | ||
170 | * Note: Delay restrictions in receiving direction will be enforced | ||
171 | * with one message delay. | ||
172 | */ | ||
173 | void | ||
174 | GNUNET_TRANSPORT_manipulation_set (struct GNUNET_TRANSPORT_ManipulationHandle *handle, | ||
175 | const struct GNUNET_PeerIdentity *peer, | ||
176 | const struct GNUNET_ATS_Properties *prop, | ||
177 | struct GNUNET_TIME_Relative delay_in, | ||
178 | struct GNUNET_TIME_Relative delay_out) | ||
179 | { | ||
180 | struct GNUNET_MQ_Envelope *env; | ||
181 | struct TrafficMetricMessage *msg; | ||
182 | |||
183 | if (NULL == handle->mq) | ||
184 | return; | ||
185 | env = GNUNET_MQ_msg (msg, | ||
186 | GNUNET_MESSAGE_TYPE_TRANSPORT_TRAFFIC_METRIC); | ||
187 | msg->reserved = htonl (0); | ||
188 | msg->peer = *peer; | ||
189 | GNUNET_ATS_properties_hton (&msg->properties, | ||
190 | prop); | ||
191 | msg->delay_in = GNUNET_TIME_relative_hton (delay_in); | ||
192 | msg->delay_out = GNUNET_TIME_relative_hton (delay_out); | ||
193 | GNUNET_MQ_send (handle->mq, | ||
194 | env); | ||
195 | } | ||
196 | |||
197 | |||
198 | /** | ||
199 | * Connect to the transport service. Note that the connection may | ||
200 | * complete (or fail) asynchronously. | ||
201 | * | ||
202 | * @param cfg configuration to use | ||
203 | * @return NULL on error | ||
204 | */ | ||
205 | struct GNUNET_TRANSPORT_ManipulationHandle * | ||
206 | GNUNET_TRANSPORT_manipulation_connect (const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
207 | { | ||
208 | struct GNUNET_TRANSPORT_ManipulationHandle *h; | ||
209 | |||
210 | h = GNUNET_new (struct GNUNET_TRANSPORT_ManipulationHandle); | ||
211 | h->cfg = cfg; | ||
212 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
213 | "Connecting to transport service.\n"); | ||
214 | reconnect (h); | ||
215 | if (NULL == h->mq) | ||
216 | { | ||
217 | GNUNET_free (h); | ||
218 | return NULL; | ||
219 | } | ||
220 | return h; | ||
221 | } | ||
222 | |||
223 | |||
224 | /** | ||
225 | * Disconnect from the transport service. | ||
226 | * | ||
227 | * @param handle handle to the service as returned from #GNUNET_TRANSPORT_manipulation_connect() | ||
228 | */ | ||
229 | void | ||
230 | GNUNET_TRANSPORT_manipulation_disconnect (struct GNUNET_TRANSPORT_ManipulationHandle *handle) | ||
231 | { | ||
232 | if (NULL == handle->reconnect_task) | ||
233 | disconnect_and_schedule_reconnect (handle); | ||
234 | /* and now we stop trying to connect again... */ | ||
235 | if (NULL != handle->reconnect_task) | ||
236 | { | ||
237 | GNUNET_SCHEDULER_cancel (handle->reconnect_task); | ||
238 | handle->reconnect_task = NULL; | ||
239 | } | ||
240 | GNUNET_free (handle); | ||
241 | } | ||
242 | |||
243 | |||
244 | /* end of transport_api_manipulation.c */ | ||