1 /* gras message exchanges */
3 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "xbt/ex_interface.h"
11 #include "gras/Msg/msg_private.h"
12 #include "gras/Virtu/virtu_interface.h"
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_msg);
18 const char *e_gras_msg_kind_names[e_gras_msg_kind_count] =
19 { "UNKNOWN", "ONEWAY", "RPC call", "RPC answer", "RPC error" };
22 /** \brief Waits for a message to come in over a given socket.
24 * @param timeout: How long should we wait for this message.
25 * @param msgt_want: type of awaited msg (or NULL if I'm enclined to accept any message)
26 * @param expe_want: awaited expeditot (match on hostname, not port; NULL if not relevant)
27 * @param filter: function returning true or false when passed a payload. Messages for which it returns false are not selected. (NULL if not relevant)
28 * @param filter_ctx: context passed as second argument of the filter (a pattern to match?)
29 * @param[out] msg_got: where to write the message we got
31 * Every message of another type received before the one waited will be queued
32 * and used by subsequent call to this function or gras_msg_handle().
36 gras_msg_wait_ext_(double timeout,
37 gras_msgtype_t msgt_want,
38 gras_socket_t expe_want,
39 gras_msg_filter_t filter,
40 void *filter_ctx, gras_msg_t msg_got)
45 gras_msg_procdata_t pd =
46 (gras_msg_procdata_t) gras_libdata_by_id(gras_msg_libdata_id);
49 xbt_assert(msg_got, "msg_got is an output parameter");
51 start = gras_os_time();
52 XBT_VERB("Waiting for message '%s' for %fs",
53 msgt_want ? msgt_want->name : "(any)", timeout);
55 xbt_dynar_foreach(pd->msg_waitqueue, cpt, msg) {
56 if ((!msgt_want || (msg.type->code == msgt_want->code))
57 && (!expe_want || (!strcmp(gras_socket_peer_name(msg.expe),
58 gras_socket_peer_name(expe_want))))
59 && (!filter || filter(&msg, filter_ctx))) {
61 memcpy(msg_got, &msg, sizeof(s_gras_msg_t));
62 xbt_dynar_cursor_rm(pd->msg_waitqueue, &cpt);
63 XBT_VERB("The waited message was queued");
68 xbt_dynar_foreach(pd->msg_queue, cpt, msg) {
69 if ((!msgt_want || (msg.type->code == msgt_want->code))
70 && (!expe_want || (!strcmp(gras_socket_peer_name(msg.expe),
71 gras_socket_peer_name(expe_want))))
72 && (!filter || filter(&msg, filter_ctx))) {
74 memcpy(msg_got, &msg, sizeof(s_gras_msg_t));
75 xbt_dynar_cursor_rm(pd->msg_queue, &cpt);
76 XBT_VERB("The waited message was queued");
85 restart_receive: /* Goto here when the receive of a message failed */
88 memset(&msg, 0, sizeof(msg));
91 xbt_queue_shift_timed(pd->msg_received, &msg,
92 timeout ? timeout - now + start : 0);
95 if (e.category == system_error &&
96 !strncmp("Socket closed by remote side", e.msg,
97 strlen("Socket closed by remote side"))) {
105 goto restart_receive;
107 XBT_DEBUG("Got a message from the socket");
109 if ((!msgt_want || (msg.type->code == msgt_want->code))
110 && (!expe_want || (!strcmp(gras_socket_peer_name(msg.expe),
111 gras_socket_peer_name(expe_want))))
112 && (!filter || filter(&msg, filter_ctx))) {
114 memcpy(msg_got, &msg, sizeof(s_gras_msg_t));
115 XBT_DEBUG("Message matches expectations. Use it.");
118 XBT_DEBUG("Message does not match expectations. Queue it.");
120 /* not expected msg type. Queue it for later */
121 xbt_dynar_push(pd->msg_queue, &msg);
123 now = gras_os_time();
124 if (now - start + 0.001 > timeout) {
125 THROWF(timeout_error, now - start + 0.001 - timeout,
126 "Timeout while waiting for msg '%s'",
127 msgt_want ? msgt_want->name : "(any)");
134 /** \brief Waits for a message to come in over a given socket.
136 * @param timeout: How long should we wait for this message.
137 * @param msgt_want: type of awaited msg
138 * @param[out] expeditor: where to create a socket to answer the incomming message
139 * @param[out] payload: where to write the payload of the incomming message
140 * @return the error code (or no_error).
142 * Every message of another type received before the one waited will be queued
143 * and used by subsequent call to this function or gras_msg_handle().
146 gras_msg_wait_(double timeout,
147 gras_msgtype_t msgt_want,
148 gras_socket_t * expeditor, void *payload)
152 gras_msg_wait_ext_(timeout, msgt_want, NULL, NULL, NULL, &msg);
154 if (msgt_want->ctn_type) {
156 "Message type '%s' convey a payload that you must accept",
160 "No payload was declared for message type '%s' (don't expect one)",
165 memcpy(payload, msg.payl, msg.payl_size);
170 *expeditor = msg.expe;
173 static int gras_msg_wait_or_filter(gras_msg_t msg, void *ctx)
175 xbt_dynar_t dyn = (xbt_dynar_t) ctx;
176 int res = xbt_dynar_member(dyn, msg->type);
178 XBT_VERB("Got matching message (type=%s)", msg->type->name);
180 XBT_VERB("Got message not matching our expectations");
184 /** \brief Waits for a message to come in over a given socket.
186 * @param timeout: How long should we wait for this message.
187 * @param msgt_want: a dynar containing all accepted message type
188 * @param[out] ctx: the context of received message (in case it's a RPC call we want to answer to)
189 * @param[out] msgt_got: indice in the dynar of the type of the received message
190 * @param[out] payload: where to write the payload of the incomming message
191 * @return the error code (or no_error).
193 * Every message of a type not in the accepted list received before the one
194 * waited will be queued and used by subsequent call to this function or
197 * If you are interested in the context, pass the address of a s_gras_msg_cb_ctx_t variable.
199 void gras_msg_wait_or(double timeout,
200 xbt_dynar_t msgt_want,
201 gras_msg_cb_ctx_t * ctx, int *msgt_got,
206 XBT_VERB("Wait %f seconds for several message types", timeout);
207 gras_msg_wait_ext_(timeout,
209 &gras_msg_wait_or_filter, (void *) msgt_want, &msg);
211 if (msg.type->ctn_type) {
213 "Message type '%s' convey a payload you must accept",
216 /* don't check the other side since some of the types may have a payload */
217 if (payload && msg.type->ctn_type) {
218 memcpy(payload, msg.payl, msg.payl_size);
223 *ctx = gras_msg_cb_ctx_new(msg.expe, msg.type, msg.ID,
224 (msg.kind == e_gras_msg_kind_rpccall), 60);
227 *msgt_got = xbt_dynar_search(msgt_want, msg.type);
231 /** \brief Send the data pointed by \a payload as a message of type
232 * \a msgtype to the peer \a sock */
233 void gras_msg_send_(gras_socket_t sock, gras_msgtype_t msgtype,
237 if (msgtype->ctn_type) {
239 "Message type '%s' convey a payload you must provide",
243 "No payload was declared for message type '%s'",
247 XBT_DEBUG("Send a oneway message of type '%s'. Payload=%p",
248 msgtype->name, payload);
249 gras_msg_send_ext(sock, e_gras_msg_kind_oneway, 0, msgtype, payload);
250 XBT_VERB("Sent a oneway message of type '%s'. Payload=%p",
251 msgtype->name, payload);
254 /** @brief Handle all messages arriving within the given period
256 * @param period: How long to wait for incoming messages (in seconds)
258 * Messages are dealed with just like gras_msg_handle() would do. The
259 * difference is that gras_msg_handle() handles at most one message (or wait up
260 * to timeout second when no message arrives) while this function handles any
261 * amount of messages, and lasts the given period in any case.
263 void gras_msg_handleall(double period)
266 double begin = gras_os_time();
270 now = gras_os_time();
272 if (period - now + begin > 0)
273 gras_msg_handle(period - now + begin);
276 if (e.category != timeout_error)
277 RETHROWF("Error while waiting for messages: %s");
280 /* Epsilon to avoid numerical stability issues were the waited interval is so small that the global clock cannot notice the increment */
281 } while (period - now + begin > 0);
284 /** @brief Handle an incomming message or timer (or wait up to \a timeOut seconds)
286 * @param timeOut: How long to wait for incoming messages (in seconds)
287 * @return the error code (or no_error).
289 * Any message arriving in the given interval is passed to the callbacks.
291 * @sa gras_msg_handleall().
293 void gras_msg_handle(volatile double timeOut)
303 gras_msg_procdata_t pd =
304 (gras_msg_procdata_t) gras_libdata_by_id(gras_msg_libdata_id);
305 gras_cblist_t *list = NULL;
307 s_gras_msg_cb_ctx_t ctx;
309 volatile int timerexpected, timeouted;
312 XBT_VERB("Handling message within the next %.2fs", timeOut);
314 untiltimer = gras_msg_timer_handle();
315 XBT_DEBUG("Next timer in %f sec", untiltimer);
316 if (untiltimer == 0.0) {
317 /* A timer was already elapsed and handled */
320 if (untiltimer != -1.0) {
322 timeOut = MIN(timeOut, untiltimer);
327 /* get a message (from the queue or from the net) */
329 if (xbt_dynar_length(pd->msg_queue)) {
330 XBT_DEBUG("Get a message from the queue");
331 xbt_dynar_shift(pd->msg_queue, &msg);
334 xbt_queue_shift_timed(pd->msg_received, &msg, timeOut);
335 // msg.expe = gras_trp_select(timeOut);
338 if (e.category != timeout_error)
340 XBT_DEBUG("Damn. Timeout while getting a message from the queue");
349 /* A timer elapsed before the arrival of any message even if we select()ed a bit */
350 untiltimer = gras_msg_timer_handle();
351 if (untiltimer == 0.0) {
352 /* we served a timer, we're done */
355 xbt_assert(untiltimer > 0, "Negative timer (%f). I'm 'puzzeled'",
358 ("No timer elapsed, in contrary to expectations (next in %f sec)",
360 THROWF(timeout_error, 0,
361 "No timer elapsed, in contrary to expectations (next in %f sec)",
366 /* select timeouted, and no timer elapsed. Nothing to do */
367 THROWF(timeout_error, 0, "No new message or timer (delay was %f)",
373 /* A message was already there or arrived in the meanwhile. handle it */
374 xbt_dynar_foreach(pd->cbl_list, cpt, list) {
375 if (list->id == msg.type->code) {
383 ("No callback for message '%s' (type:%s) from %s:%d. Queue it for later gras_msg_wait() use.",
384 msg.type->name, e_gras_msg_kind_names[msg.kind],
385 gras_socket_peer_name(msg.expe), gras_socket_peer_port(msg.expe));
386 xbt_dynar_push(pd->msg_waitqueue, &msg);
387 return; /* FIXME: maybe we should call ourselves again until the end of the timer or a proper msg is got */
390 ctx.expeditor = msg.expe;
392 ctx.msgtype = msg.type;
393 ctx.answer_due = (msg.kind == e_gras_msg_kind_rpccall);
396 case e_gras_msg_kind_oneway:
397 case e_gras_msg_kind_rpccall:
400 xbt_dynar_foreach(list->cbs, cpt, cb) {
401 volatile unsigned int cpt2 = cpt;
404 ("Use the callback #%d (@%p) for incomming msg '%s' (payload_size=%d)",
405 cpt + 1, cb, msg.type->name, msg.payl_size);
406 if (!(*cb) (&ctx, msg.payl)) {
407 /* cb handled the message */
417 if (msg.type->kind == e_gras_msg_kind_rpccall) {
418 char *old_file = e.file;
419 /* The callback raised an exception, propagate it on the network */
421 /* Make sure we reduce the file name to its basename to avoid issues in tests */
422 char *new_file = strrchr(e.file, '/');
425 /* the exception is born on this machine */
426 e.host = (char *) gras_os_myname();
427 xbt_ex_setup_backtrace(&e);
430 ("Propagate %s exception ('%s') from '%s' RPC cb back to %s:%d",
431 (e.remote ? "remote" : "local"), e.msg, msg.type->name,
432 gras_socket_peer_name(msg.expe),
433 gras_socket_peer_port(msg.expe));
434 if (XBT_LOG_ISENABLED(gras_msg, xbt_log_priority_verbose))
436 gras_msg_send_ext(msg.expe, e_gras_msg_kind_rpcerror,
437 msg.ID, msg.type, &e);
444 ("Callback #%d (@%p) to message '%s' (payload size: %d) raised an exception: %s",
445 cpt + 1, cb, msg.type->name, msg.payl_size);
449 xbt_assert(!ctx.answer_due,
450 "Bug in user code: RPC callback to message '%s' didn't call gras_msg_rpcreturn",
454 ("BUGS BOTH IN USER CODE (RPC callback to message '%s' didn't call gras_msg_rpcreturn) "
455 "AND IN SIMGRID (process wasn't killed by an assert)",
458 THROWF(mismatch_error, 0,
459 "Message '%s' refused by all registered callbacks (maybe your callback misses a 'return 0' at the end)",
461 /* FIXME: gras_datadesc_free not implemented => leaking the payload */
465 case e_gras_msg_kind_rpcanswer:
466 XBT_INFO("Unexpected RPC answer discarded (type: %s; from:%s:%d)",
467 msg.type->name, gras_socket_peer_name(msg.expe),
468 gras_socket_peer_port(msg.expe));
470 ("FIXME: gras_datadesc_free not implemented => leaking the payload");
473 case e_gras_msg_kind_rpcerror:
474 XBT_INFO("Unexpected RPC error discarded (type: %s; from:%s:%d)",
475 msg.type->name, gras_socket_peer_name(msg.expe),
476 gras_socket_peer_port(msg.expe));
478 ("FIXME: gras_datadesc_free not implemented => leaking the payload");
482 THROWF(unknown_error, 0,
483 "Cannot handle messages of kind %d yet", msg.type->kind);