aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_client.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-07-05 11:41:23 +0000
committerChristian Grothoff <christian@grothoff.org>2016-07-05 11:41:23 +0000
commit904c393e01da94dc9e842b1c3050eea7ae96b4f0 (patch)
tree0ea34440397241e464e2ffbd7ece78ee3026f9bf /src/util/test_client.c
parentbed74ba115e1ff8501482dcb7f94fc0214e647b8 (diff)
downloadgnunet-904c393e01da94dc9e842b1c3050eea7ae96b4f0.tar.gz
gnunet-904c393e01da94dc9e842b1c3050eea7ae96b4f0.zip
update tests to use new MQ API
Diffstat (limited to 'src/util/test_client.c')
-rw-r--r--src/util/test_client.c87
1 files changed, 51 insertions, 36 deletions
diff --git a/src/util/test_client.c b/src/util/test_client.c
index dab985e5f..97fe10110 100644
--- a/src/util/test_client.c
+++ b/src/util/test_client.c
@@ -29,7 +29,7 @@
29 29
30#define MYNAME "test_client" 30#define MYNAME "test_client"
31 31
32static struct GNUNET_CLIENT_Connection *client; 32static struct GNUNET_MQ_Handle *mq;
33 33
34static struct GNUNET_SERVER_Handle *server; 34static struct GNUNET_SERVER_Handle *server;
35 35
@@ -43,6 +43,7 @@ struct CopyContext
43 struct GNUNET_MessageHeader *cpy; 43 struct GNUNET_MessageHeader *cpy;
44}; 44};
45 45
46
46static size_t 47static size_t
47copy_msg (void *cls, size_t size, void *buf) 48copy_msg (void *cls, size_t size, void *buf)
48{ 49{
@@ -55,7 +56,8 @@ copy_msg (void *cls, size_t size, void *buf)
55 GNUNET_SERVER_receive_done (ctx->client, GNUNET_OK); 56 GNUNET_SERVER_receive_done (ctx->client, GNUNET_OK);
56 GNUNET_free (cpy); 57 GNUNET_free (cpy);
57 GNUNET_free (ctx); 58 GNUNET_free (ctx);
58 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message bounced back to client\n"); 59 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
60 "Message bounced back to client\n");
59 return sizeof (struct GNUNET_MessageHeader); 61 return sizeof (struct GNUNET_MessageHeader);
60} 62}
61 63
@@ -64,7 +66,8 @@ copy_msg (void *cls, size_t size, void *buf)
64 * Callback that just bounces the message back to the sender. 66 * Callback that just bounces the message back to the sender.
65 */ 67 */
66static void 68static void
67echo_cb (void *cls, struct GNUNET_SERVER_Client *client, 69echo_cb (void *cls,
70 struct GNUNET_SERVER_Client *client,
68 const struct GNUNET_MessageHeader *message) 71 const struct GNUNET_MessageHeader *message)
69{ 72{
70 struct CopyContext *cc; 73 struct CopyContext *cc;
@@ -93,34 +96,35 @@ static struct GNUNET_SERVER_MessageHandler handlers[] = {
93 96
94 97
95static void 98static void
96recv_bounce (void *cls, const struct GNUNET_MessageHeader *got) 99handle_bounce (void *cls,
100 const struct GNUNET_MessageHeader *got)
97{ 101{
98 int *ok = cls; 102 int *ok = cls;
99 struct GNUNET_MessageHeader msg; 103
100 104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
101 GNUNET_assert (got != NULL); /* timeout */ 105 "Receiving bounce, checking content\n");
102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving bounce, checking content\n"); 106 GNUNET_assert (NULL != got);
103 msg.type = htons (MY_TYPE); 107 GNUNET_MQ_destroy (mq);
104 msg.size = htons (sizeof (struct GNUNET_MessageHeader)); 108 mq = NULL;
105 GNUNET_assert (0 == memcmp (got, &msg, sizeof (struct GNUNET_MessageHeader)));
106 GNUNET_CLIENT_disconnect (client);
107 client = NULL;
108 GNUNET_SERVER_destroy (server); 109 GNUNET_SERVER_destroy (server);
109 server = NULL; 110 server = NULL;
110 *ok = 0; 111 *ok = 0;
111} 112}
112 113
113 114
114static size_t 115/**
115make_msg (void *cls, size_t size, void *buf) 116 * Generic error handler, called with the appropriate error code and
117 * the same closure specified at the creation of the message queue.
118 * Not every message queue implementation supports an error handler.
119 *
120 * @param cls closure with the `struct GNUNET_STATISTICS_Handle *`
121 * @param error error code
122 */
123static void
124mq_error_handler (void *cls,
125 enum GNUNET_MQ_Error error)
116{ 126{
117 struct GNUNET_MessageHeader *msg = buf; 127 GNUNET_assert (0); /* should never happen */
118
119 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
120 msg->type = htons (MY_TYPE);
121 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating message for transmission\n");
123 return sizeof (struct GNUNET_MessageHeader);
124} 128}
125 129
126 130
@@ -130,9 +134,22 @@ task (void *cls)
130 struct sockaddr_in sa; 134 struct sockaddr_in sa;
131 struct sockaddr *sap[2]; 135 struct sockaddr *sap[2];
132 socklen_t slens[2]; 136 socklen_t slens[2];
137 struct GNUNET_MQ_Envelope *env;
138 struct GNUNET_MessageHeader *msg;
139 GNUNET_MQ_hd_fixed_size (bounce,
140 MY_TYPE,
141 struct GNUNET_MessageHeader);
142 struct GNUNET_MQ_MessageHandler chandlers[] = {
143 make_bounce_handler (cls),
144 GNUNET_MQ_handler_end ()
145 };
133 146
134 /* test that ill-configured client fails instantly */ 147 /* test that ill-configured client fails instantly */
135 GNUNET_assert (NULL == GNUNET_CLIENT_connect ("invalid-service", cfg)); 148 GNUNET_assert (NULL == GNUNET_CLIENT_connecT (cfg,
149 "invalid-service",
150 NULL,
151 &mq_error_handler,
152 NULL));
136 153
137 /* test IPC between client and server */ 154 /* test IPC between client and server */
138 sap[0] = (struct sockaddr *) &sa; 155 sap[0] = (struct sockaddr *) &sa;
@@ -148,23 +165,21 @@ task (void *cls)
148 server = 165 server =
149 GNUNET_SERVER_create (NULL, NULL, sap, slens, 166 GNUNET_SERVER_create (NULL, NULL, sap, slens,
150 GNUNET_TIME_relative_multiply 167 GNUNET_TIME_relative_multiply
151 (GNUNET_TIME_UNIT_MILLISECONDS, 10000), GNUNET_NO); 168 (GNUNET_TIME_UNIT_SECONDS, 10), GNUNET_NO);
152 GNUNET_assert (server != NULL); 169 GNUNET_assert (server != NULL);
153 handlers[0].callback_cls = cls; 170 handlers[0].callback_cls = cls;
154 handlers[1].callback_cls = cls; 171 handlers[1].callback_cls = cls;
155 GNUNET_SERVER_add_handlers (server, handlers); 172 GNUNET_SERVER_add_handlers (server, handlers);
156 client = GNUNET_CLIENT_connect (MYNAME, cfg); 173 mq = GNUNET_CLIENT_connecT (cfg,
157 GNUNET_assert (client != NULL); 174 MYNAME,
158 GNUNET_assert (NULL != 175 chandlers,
159 GNUNET_CLIENT_notify_transmit_ready (client, 176 &mq_error_handler,
160 sizeof (struct 177 NULL);
161 GNUNET_MessageHeader), 178 GNUNET_assert (NULL != mq);
162 GNUNET_TIME_UNIT_SECONDS, 179 env = GNUNET_MQ_msg (msg,
163 GNUNET_NO, &make_msg, 180 MY_TYPE);
164 NULL)); 181 GNUNET_MQ_send (mq,
165 GNUNET_CLIENT_receive (client, &recv_bounce, cls, 182 env);
166 GNUNET_TIME_relative_multiply
167 (GNUNET_TIME_UNIT_MILLISECONDS, 10000));
168} 183}
169 184
170 185