3 /* messaging - Function related to messaging (code shared between RL and SG)*/
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "gras/Msg/msg_private.h"
12 #include "gras/DataDesc/datadesc_interface.h"
13 #include "gras/Transport/transport_interface.h" /* gras_trp_chunk_send/recv */
14 #include "gras/Virtu/virtu_interface.h"
16 #define MIN(a,b) ((a) < (b) ? (a) : (b))
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_msg,gras,"High level messaging");
20 xbt_set_t _gras_msgtype_set = NULL;
21 static char GRAS_header[6];
22 static char *make_namev(const char *name, short int ver);
25 * Creating procdata for this module
27 static void *gras_msg_procdata_new() {
28 gras_msg_procdata_t res = xbt_new(s_gras_msg_procdata_t,1);
30 res->msg_queue = xbt_dynar_new(sizeof(s_gras_msg_t), NULL);
31 res->cbl_list = xbt_dynar_new(sizeof(gras_cblist_t *),gras_cbl_free);
32 res->timers = xbt_dynar_new(sizeof(s_gras_timer_t), NULL);
38 * Freeing procdata for this module
40 static void gras_msg_procdata_free(void *data) {
41 gras_msg_procdata_t res = (gras_msg_procdata_t)data;
43 xbt_dynar_free(&( res->msg_queue ));
44 xbt_dynar_free(&( res->cbl_list ));
45 xbt_dynar_free(&( res->timers ));
53 void gras_msg_register() {
54 gras_procdata_add("gras_msg",gras_msg_procdata_new, gras_msg_procdata_free);
58 * Initialize this submodule.
60 void gras_msg_init(void) {
61 /* only initialize once */
62 if (_gras_msgtype_set != NULL)
65 VERB0("Initializing Msg");
67 _gras_msgtype_set = xbt_set_new();
69 memcpy(GRAS_header,"GRAS", 4);
70 GRAS_header[4]=GRAS_PROTOCOL_VERSION;
71 GRAS_header[5]=(char)GRAS_THISARCH;
75 * Finalize the msg module
80 xbt_set_free(&_gras_msgtype_set);
86 void gras_msgtype_free(void *t) {
87 gras_msgtype_t msgtype=(gras_msgtype_t)t;
97 * Returns the versionned name of the message. If the version is 0, that's
98 * the name unchanged. Pay attention to this before free'ing the result.
100 static char *make_namev(const char *name, short int ver) {
106 namev = (char*)xbt_malloc(strlen(name)+2+3+1);
109 sprintf(namev,"%s_v%d",name,ver);
114 /** @brief declare a new message type of the given name. It only accepts the given datadesc as payload
116 * @param name: name as it should be used for logging messages (must be uniq)
117 * @param payload: datadescription of the payload
119 void gras_msgtype_declare(const char *name,
120 gras_datadesc_type_t payload) {
121 gras_msgtype_declare_v(name, 0, payload);
124 /** @brief declare a new versionned message type of the given name and payload
126 * @param name: name as it should be used for logging messages (must be uniq)
127 * @param version: something like versionning symbol
128 * @param payload: datadescription of the payload
130 * Registers a message to the GRAS mechanism. Use this version instead of
131 * gras_msgtype_declare when you change the semantic or syntax of a message and
132 * want your programs to be able to deal with both versions. Internally, each
133 * will be handled as an independent message type, so you can register
134 * differents for each of them.
137 gras_msgtype_declare_v(const char *name,
139 gras_datadesc_type_t payload) {
142 gras_msgtype_t msgtype;
143 char *namev=make_namev(name,version);
145 errcode = xbt_set_get_by_name(_gras_msgtype_set,
146 namev,(xbt_set_elm_t*)&msgtype);
148 if (errcode == no_error) {
149 VERB2("Re-register version %d of message '%s' (same payload, ignored).",
151 xbt_assert3(!gras_datadesc_type_cmp(msgtype->ctn_type, payload),
152 "Message %s re-registred with another payload (%s was %s)",
153 namev,gras_datadesc_get_name(payload),
154 gras_datadesc_get_name(msgtype->ctn_type));
156 return ; /* do really ignore it */
159 xbt_assert_error(mismatch_error); /* expect this error */
160 VERB3("Register version %d of message '%s' (payload: %s).",
161 version, name, gras_datadesc_get_name(payload));
163 msgtype = xbt_new(s_gras_msgtype_t,1);
164 msgtype->name = (namev == name ? strdup(name) : namev);
165 msgtype->name_len = strlen(namev);
166 msgtype->version = version;
167 msgtype->ctn_type = payload;
169 xbt_set_add(_gras_msgtype_set, (xbt_set_elm_t)msgtype,
173 /** @brief retrive an existing message type from its name. */
174 gras_msgtype_t gras_msgtype_by_name (const char *name) {
175 return gras_msgtype_by_namev(name,0);
178 /** @brief retrive an existing message type from its name and version. */
179 gras_msgtype_t gras_msgtype_by_namev(const char *name,
184 char *namev = make_namev(name,version);
186 errcode = xbt_set_get_by_name(_gras_msgtype_set, namev,
187 (xbt_set_elm_t*)&res);
188 if (errcode != no_error)
191 WARN1("msgtype_by_name(%s) returns NULL",namev);
198 /** \brief Send the data pointed by \a payload as a message of type
199 * \a msgtype to the peer \a sock */
201 gras_msg_send(gras_socket_t sock,
202 gras_msgtype_t msgtype,
206 static gras_datadesc_type_t string_type=NULL;
209 RAISE0(mismatch_error,
210 "Cannot send the NULL message (did msgtype_by_name fail?)");
213 string_type = gras_datadesc_by_name("string");
214 xbt_assert(string_type);
217 DEBUG3("send '%s' to %s:%d", msgtype->name,
218 gras_socket_peer_name(sock),gras_socket_peer_port(sock));
219 TRY(gras_trp_chunk_send(sock, GRAS_header, 6));
221 TRY(gras_datadesc_send(sock, string_type, &msgtype->name));
222 TRY(gras_datadesc_send(sock, msgtype->ctn_type, payload));
223 TRY(gras_trp_flush(sock));
228 * receive the next message on the given socket.
231 gras_msg_recv(gras_socket_t sock,
232 gras_msgtype_t *msgtype,
237 static gras_datadesc_type_t string_type=NULL;
244 string_type=gras_datadesc_by_name("string");
245 xbt_assert(string_type);
248 TRY(gras_trp_chunk_recv(sock, header, 6));
249 for (cpt=0; cpt<4; cpt++)
250 if (header[cpt] != GRAS_header[cpt])
251 RAISE2(mismatch_error,"Incoming bytes do not look like a GRAS message (header='%.4s' not '%.4s')",header,GRAS_header);
252 if (header[4] != GRAS_header[4])
253 RAISE2(mismatch_error,"GRAS protocol mismatch (got %d, use %d)",
254 (int)header[4], (int)GRAS_header[4]);
255 r_arch = (int)header[5];
256 DEBUG2("Handle an incoming message using protocol %d (remote is %s)",
257 (int)header[4],gras_datadesc_arch_name(r_arch));
259 TRY(gras_datadesc_recv(sock, string_type, r_arch, &msg_name));
260 errcode = xbt_set_get_by_name(_gras_msgtype_set,
261 msg_name,(xbt_set_elm_t*)msgtype);
262 if (errcode != no_error)
264 "Got error %s while retrieving the type associated to messages '%s'",
265 xbt_error_name(errcode),msg_name);
266 /* FIXME: Survive unknown messages */
269 *payload_size=gras_datadesc_size((*msgtype)->ctn_type);
270 xbt_assert2(*payload_size > 0,
272 "Dynamic array as payload is forbided for now (FIXME?).",
273 "Reference to dynamic array is allowed.");
274 *payload = xbt_malloc(*payload_size);
275 TRY(gras_datadesc_recv(sock, (*msgtype)->ctn_type, r_arch, *payload));
280 /** \brief Waits for a message to come in over a given socket.
282 * @param timeout: How long should we wait for this message.
283 * @param msgt_want: type of awaited msg
284 * @param[out] expeditor: where to create a socket to answer the incomming message
285 * @param[out] payload: where to write the payload of the incomming message
286 * @return the error code (or no_error).
288 * Every message of another type received before the one waited will be queued
289 * and used by subsequent call to this function or gras_msg_handle().
292 gras_msg_wait(double timeout,
293 gras_msgtype_t msgt_want,
294 gras_socket_t *expeditor,
297 gras_msgtype_t msgt_got;
299 int payload_size_got;
302 gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_get("gras_msg");
305 gras_socket_t expeditor_res = NULL;
310 RAISE0(mismatch_error,
311 "Cannot wait for the NULL message (did msgtype_by_name fail?)");
313 VERB1("Waiting for message '%s'",msgt_want->name);
315 start = now = gras_os_time();
317 xbt_dynar_foreach(pd->msg_queue,cpt,msg){
318 if (msg.type->code == msgt_want->code) {
320 *expeditor = msg.expeditor;
321 memcpy(payload, msg.payload, msg.payload_size);
323 xbt_dynar_cursor_rm(pd->msg_queue, &cpt);
324 VERB0("The waited message was queued");
330 TRY(gras_trp_select(timeout - now + start, &expeditor_res));
331 TRY(gras_msg_recv(expeditor_res, &msgt_got, &payload_got, &payload_size_got));
332 if (msgt_got->code == msgt_want->code) {
334 *expeditor=expeditor_res;
335 memcpy(payload, payload_got, payload_size_got);
337 VERB0("Got waited message");
341 /* not expected msg type. Queue it for later */
342 msg.expeditor = expeditor_res;
344 msg.payload = payload;
345 msg.payload_size = payload_size_got;
346 xbt_dynar_push(pd->msg_queue,&msg);
349 if (now - start + 0.001 < timeout) {
350 RAISE1(timeout_error,"Timeout while waiting for msg %s",msgt_want->name);
357 /** @brief Handle an incomming message or timer (or wait up to \a timeOut seconds)
359 * @param timeOut: How long to wait for incoming messages (in seconds)
360 * @return the error code (or no_error).
362 * Messages are passed to the callbacks.
365 gras_msg_handle(double timeOut) {
373 gras_socket_t expeditor;
376 gras_msgtype_t msgtype;
378 gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_get("gras_msg");
379 gras_cblist_t *list=NULL;
384 VERB1("Handling message within the next %.2fs",timeOut);
386 untiltimer = gras_msg_timer_handle();
387 DEBUG2("[%.0f] Next timer in %f sec", gras_os_time(), untiltimer);
388 if (untiltimer == 0.0) {
389 /* A timer was already elapsed and handled */
392 if (untiltimer != -1.0) {
394 timeOut = MIN(timeOut, untiltimer);
399 /* get a message (from the queue or from the net) */
400 if (xbt_dynar_length(pd->msg_queue)) {
401 DEBUG0("Get a message from the queue");
402 xbt_dynar_shift(pd->msg_queue,&msg);
403 expeditor = msg.expeditor;
405 payload = msg.payload;
408 errcode = gras_trp_select(timeOut, &expeditor);
409 if (errcode != no_error && errcode != timeout_error)
411 if (errcode != timeout_error)
412 TRY(gras_msg_recv(expeditor, &msgtype, &payload, &payload_size));
415 if (errcode == timeout_error ) {
418 /* A timer elapsed before the arrival of any message even if we select()ed a bit */
419 untiltimer = gras_msg_timer_handle();
420 if (untiltimer == 0.0) {
423 xbt_assert1(untiltimer>0, "Negative timer (%f). I'm puzzeled", untiltimer);
424 ERROR1("No timer elapsed, in contrary to expectations (next in %f sec)",
426 return timeout_error;
430 /* select timeouted, and no timer elapsed. Nothing to do */
431 return timeout_error;
436 /* A message was already there or arrived in the meanwhile. handle it */
437 xbt_dynar_foreach(pd->cbl_list,cpt,list) {
438 if (list->id == msgtype->code) {
445 INFO1("No callback for the incomming '%s' message. Discarded.",
447 WARN0("FIXME: gras_datadesc_free not implemented => leaking the payload");
451 xbt_dynar_foreach(list->cbs,cpt,cb) {
452 VERB3("Use the callback #%d (@%p) for incomming msg %s",
453 cpt+1,cb,msgtype->name);
454 if ((*cb)(expeditor,payload)) {
455 /* cb handled the message */
461 INFO1("Message '%s' refused by all registered callbacks", msgtype->name);
462 WARN0("FIXME: gras_datadesc_free not implemented => leaking the payload");
463 return mismatch_error;
467 gras_cbl_free(void *data){
468 gras_cblist_t *list=*(void**)data;
470 xbt_dynar_free(&( list->cbs ));
475 /** \brief Bind the given callback to the given message type
477 * Several callbacks can be attached to a given message type. The lastly added one will get the message first, and
478 * if it returns false, the message will be passed to the second one.
479 * And so on until one of the callbacks accepts the message.
482 gras_cb_register(gras_msgtype_t msgtype,
484 gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_get("gras_msg");
485 gras_cblist_t *list=NULL;
488 DEBUG2("Register %p as callback to '%s'",cb,msgtype->name);
490 /* search the list of cb for this message on this host (creating if NULL) */
491 xbt_dynar_foreach(pd->cbl_list,cpt,list) {
492 if (list->id == msgtype->code) {
499 /* First cb? Create room */
500 list = xbt_new(gras_cblist_t,1);
501 list->id = msgtype->code;
502 list->cbs = xbt_dynar_new(sizeof(gras_msg_cb_t), NULL);
503 xbt_dynar_push(pd->cbl_list,&list);
506 /* Insert the new one into the set */
507 xbt_dynar_insert_at(list->cbs,0,&cb);
510 /** \brief Unbind the given callback from the given message type */
512 gras_cb_unregister(gras_msgtype_t msgtype,
515 gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_get("gras_msg");
517 gras_msg_cb_t cb_cpt;
521 /* search the list of cb for this message on this host */
522 xbt_dynar_foreach(pd->cbl_list,cpt,list) {
523 if (list->id == msgtype->code) {
530 /* Remove it from the set */
532 xbt_dynar_foreach(list->cbs,cpt,cb_cpt) {
534 xbt_dynar_cursor_rm(list->cbs, &cpt);
540 VERB1("Ignoring removal of unexisting callback to msg id %d",