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/Virtu/virtu_interface.h"
13 #include "gras/DataDesc/datadesc_interface.h"
14 #include "gras/Transport/transport_interface.h" /* gras_select */
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 *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->name = xbt_strdup("gras_msg");
32 res->msg_queue = xbt_dynar_new(sizeof(s_gras_msg_t), NULL);
33 res->cbl_list = xbt_dynar_new(sizeof(gras_cblist_t *),gras_cbl_free);
34 res->timers = xbt_dynar_new(sizeof(s_gras_timer_t), NULL);
40 * Freeing procdata for this module
42 static void gras_msg_procdata_free(void *data) {
43 gras_msg_procdata_t res = (gras_msg_procdata_t)data;
45 xbt_dynar_free(&( res->msg_queue ));
46 xbt_dynar_free(&( res->cbl_list ));
47 xbt_dynar_free(&( res->timers ));
55 int gras_msg_libdata_id;
56 void gras_msg_register() {
57 gras_msg_libdata_id = gras_procdata_add("gras_msg",gras_msg_procdata_new, gras_msg_procdata_free);
61 * Initialize this submodule.
63 void gras_msg_init(void) {
64 /* only initialize once */
65 if (_gras_msgtype_set != NULL)
68 VERB0("Initializing Msg");
70 _gras_msgtype_set = xbt_set_new();
72 memcpy(_GRAS_header,"GRAS", 4);
73 _GRAS_header[4]=GRAS_PROTOCOL_VERSION;
74 _GRAS_header[5]=(char)GRAS_THISARCH;
78 * Finalize the msg module
83 xbt_set_free(&_gras_msgtype_set);
89 void gras_msgtype_free(void *t) {
90 gras_msgtype_t msgtype=(gras_msgtype_t)t;
100 * Returns the versionned name of the message. If the version is 0, that's
101 * the name unchanged. Pay attention to this before free'ing the result.
103 static char *make_namev(const char *name, short int ver) {
109 namev = (char*)xbt_malloc(strlen(name)+2+3+1);
112 sprintf(namev,"%s_v%d",name,ver);
117 /** @brief declare a new message type of the given name. It only accepts the given datadesc as payload
119 * @param name: name as it should be used for logging messages (must be uniq)
120 * @param payload: datadescription of the payload
122 void gras_msgtype_declare(const char *name,
123 gras_datadesc_type_t payload) {
124 gras_msgtype_declare_v(name, 0, payload);
127 /** @brief declare a new versionned message type of the given name and payload
129 * @param name: name as it should be used for logging messages (must be uniq)
130 * @param version: something like versionning symbol
131 * @param payload: datadescription of the payload
133 * Registers a message to the GRAS mechanism. Use this version instead of
134 * gras_msgtype_declare when you change the semantic or syntax of a message and
135 * want your programs to be able to deal with both versions. Internally, each
136 * will be handled as an independent message type, so you can register
137 * differents for each of them.
140 gras_msgtype_declare_v(const char *name,
142 gras_datadesc_type_t payload) {
144 gras_msgtype_t msgtype=NULL;
145 char *namev=make_namev(name,version);
146 volatile int found = 0;
150 msgtype = (gras_msgtype_t)xbt_set_get_by_name(_gras_msgtype_set,namev);
153 if (e.category != not_found_error)
159 VERB2("Re-register version %d of message '%s' (same payload, ignored).",
161 xbt_assert3(!gras_datadesc_type_cmp(msgtype->ctn_type, payload),
162 "Message %s re-registred with another payload (%s was %s)",
163 namev,gras_datadesc_get_name(payload),
164 gras_datadesc_get_name(msgtype->ctn_type));
166 return ; /* do really ignore it */
170 VERB3("Register version %d of message '%s' (payload: %s).",
171 version, name, gras_datadesc_get_name(payload));
173 msgtype = xbt_new(s_gras_msgtype_t,1);
174 msgtype->name = (namev == name ? strdup(name) : namev);
175 msgtype->name_len = strlen(namev);
176 msgtype->version = version;
177 msgtype->ctn_type = payload;
179 xbt_set_add(_gras_msgtype_set, (xbt_set_elm_t)msgtype,
183 /** @brief retrive an existing message type from its name. */
184 gras_msgtype_t gras_msgtype_by_name (const char *name) {
185 return gras_msgtype_by_namev(name,0);
188 /** @brief retrive an existing message type from its name and version. */
189 gras_msgtype_t gras_msgtype_by_namev(const char *name,
192 char *namev = make_namev(name,version);
194 res = (gras_msgtype_t)xbt_set_get_by_name(_gras_msgtype_set, namev);
200 /** @brief retrive an existing message type from its name and version. */
201 gras_msgtype_t gras_msgtype_by_id(int id) {
202 return (gras_msgtype_t)xbt_set_get_by_id(_gras_msgtype_set, id);
205 /** \brief Waits for a message to come in over a given socket.
207 * @param timeout: How long should we wait for this message.
208 * @param msgt_want: type of awaited msg (or NULL if I'm enclined to accept any message)
209 * @param expe_want: awaited expeditot (match on hostname, not port; NULL if not relevant)
210 * @param payl_filter: function returning true or false when passed a payload. Messages for which it returns false are not selected. (NULL if not relevant)
211 * @param filter_ctx: context passed as second argument of the filter (a pattern to match?)
212 * @param[out] msgt_got: where to write the descriptor of the message we got
213 * @param[out] expe_got: where to create a socket to answer the incomming message
214 * @param[out] payl_got: where to write the payload of the incomming message
216 * Every message of another type received before the one waited will be queued
217 * and used by subsequent call to this function or gras_msg_handle().
221 gras_msg_wait_ext(double timeout,
222 gras_msgtype_t msgt_want,
223 gras_socket_t expe_want,
224 int_f_pvoid_pvoid_t payl_filter,
226 gras_msgtype_t *msgt_got,
227 gras_socket_t *expe_got,
232 gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id);
235 xbt_assert0(msgt_want,"Cannot wait for the NULL message");
237 VERB1("Waiting for message '%s'",msgt_want->name);
239 start = now = gras_os_time();
241 xbt_dynar_foreach(pd->msg_queue,cpt,msg){
242 if ( ( !msgt_want || (msg.type->code == msgt_want->code))
243 && (!expe_want || (!strcmp( gras_socket_peer_name(msg.expe),
244 gras_socket_peer_name(expe_want))))
245 && (!payl_filter || payl_filter(msg.payl,filter_ctx))) {
248 *expe_got = msg.expe;
250 *msgt_got = msg.type;
252 memcpy(payl_got, msg.payl, msg.payl_size);
254 xbt_dynar_cursor_rm(pd->msg_queue, &cpt);
255 VERB0("The waited message was queued");
261 memset(&msg,sizeof(msg),0);
263 msg.expe = gras_trp_select(timeout - now + start);
264 gras_msg_recv(msg.expe, &msg);
266 if ( ( !msgt_want || (msg.type->code == msgt_want->code))
267 && (!expe_want || (!strcmp( gras_socket_peer_name(msg.expe),
268 gras_socket_peer_name(expe_want))))
269 && (!payl_filter || payl_filter(msg.payl,filter_ctx))) {
274 *msgt_got = msg.type;
276 memcpy(payl_got, msg.payl, msg.payl_size);
281 /* not expected msg type. Queue it for later */
282 xbt_dynar_push(pd->msg_queue,&msg);
285 if (now - start + 0.001 < timeout) {
286 THROW1(timeout_error, now-start+0.001-timeout,
287 "Timeout while waiting for msg %s",msgt_want->name);
293 /** \brief Waits for a message to come in over a given socket.
295 * @param timeout: How long should we wait for this message.
296 * @param msgt_want: type of awaited msg
297 * @param[out] expeditor: where to create a socket to answer the incomming message
298 * @param[out] payload: where to write the payload of the incomming message
299 * @return the error code (or no_error).
301 * Every message of another type received before the one waited will be queued
302 * and used by subsequent call to this function or gras_msg_handle().
305 gras_msg_wait(double timeout,
306 gras_msgtype_t msgt_want,
307 gras_socket_t *expeditor,
310 return gras_msg_wait_ext(timeout,
311 msgt_want, NULL, NULL, NULL,
312 NULL, expeditor, payload);
316 /** \brief Send the data pointed by \a payload as a message of type
317 * \a msgtype to the peer \a sock */
319 gras_msg_send(gras_socket_t sock,
320 gras_msgtype_t msgtype,
323 gras_msg_send_ext(sock, e_gras_msg_kind_oneway, msgtype, payload);
326 /** @brief Handle an incomming message or timer (or wait up to \a timeOut seconds)
328 * @param timeOut: How long to wait for incoming messages (in seconds)
329 * @return the error code (or no_error).
331 * Messages are passed to the callbacks.
334 gras_msg_handle(double timeOut) {
341 gras_socket_t expeditor=NULL;
343 gras_msgtype_t msgtype=NULL;
345 gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id);
346 gras_cblist_t *list=NULL;
349 int timerexpected, timeouted;
352 VERB1("Handling message within the next %.2fs",timeOut);
354 untiltimer = gras_msg_timer_handle();
355 DEBUG2("[%.0f] Next timer in %f sec", gras_os_time(), untiltimer);
356 if (untiltimer == 0.0) {
357 /* A timer was already elapsed and handled */
360 if (untiltimer != -1.0) {
362 timeOut = MIN(timeOut, untiltimer);
367 /* get a message (from the queue or from the net) */
369 if (xbt_dynar_length(pd->msg_queue)) {
370 DEBUG0("Get a message from the queue");
371 xbt_dynar_shift(pd->msg_queue,&msg);
372 expeditor = msg.expe;
377 expeditor = gras_trp_select(timeOut);
379 if (e.category != timeout_error)
387 /* FIXME: if not the right kind, queue it and recall ourself or goto >:-) */
388 gras_msg_recv(expeditor, &msg);
392 RETHROW1("Error caught while receiving a message on select()ed socket %p: %s",
401 /* A timer elapsed before the arrival of any message even if we select()ed a bit */
402 untiltimer = gras_msg_timer_handle();
403 if (untiltimer == 0.0) {
404 /* we served a timer, we're done */
407 xbt_assert1(untiltimer>0, "Negative timer (%f). I'm 'puzzeled'", untiltimer);
408 WARN1("No timer elapsed, in contrary to expectations (next in %f sec)",
410 THROW1(timeout_error,0,
411 "No timer elapsed, in contrary to expectations (next in %f sec)",
416 /* select timeouted, and no timer elapsed. Nothing to do */
417 THROW0(timeout_error, 0, "No new message or timer");
422 /* A message was already there or arrived in the meanwhile. handle it */
423 xbt_dynar_foreach(pd->cbl_list,cpt,list) {
424 if (list->id == msgtype->code) {
431 INFO1("No callback for the incomming '%s' message. Discarded.",
433 WARN0("FIXME: gras_datadesc_free not implemented => leaking the payload");
437 xbt_dynar_foreach(list->cbs,cpt,cb) {
438 VERB3("Use the callback #%d (@%p) for incomming msg %s",
439 cpt+1,cb,msgtype->name);
440 if ((*cb)(expeditor,payload)) {
441 /* cb handled the message */
447 /* FIXME: gras_datadesc_free not implemented => leaking the payload */
448 THROW1(mismatch_error,0,
449 "Message '%s' refused by all registered callbacks", msgtype->name);
453 gras_cbl_free(void *data){
454 gras_cblist_t *list=*(void**)data;
456 xbt_dynar_free(&( list->cbs ));
461 /** \brief Bind the given callback to the given message type
463 * Several callbacks can be attached to a given message type. The lastly added one will get the message first, and
464 * if it returns false, the message will be passed to the second one.
465 * And so on until one of the callbacks accepts the message.
468 gras_cb_register(gras_msgtype_t msgtype,
470 gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id);
471 gras_cblist_t *list=NULL;
474 DEBUG2("Register %p as callback to '%s'",cb,msgtype->name);
476 /* search the list of cb for this message on this host (creating if NULL) */
477 xbt_dynar_foreach(pd->cbl_list,cpt,list) {
478 if (list->id == msgtype->code) {
485 /* First cb? Create room */
486 list = xbt_new(gras_cblist_t,1);
487 list->id = msgtype->code;
488 list->cbs = xbt_dynar_new(sizeof(gras_msg_cb_t), NULL);
489 xbt_dynar_push(pd->cbl_list,&list);
492 /* Insert the new one into the set */
493 xbt_dynar_insert_at(list->cbs,0,&cb);
496 /** \brief Unbind the given callback from the given message type */
498 gras_cb_unregister(gras_msgtype_t msgtype,
501 gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id);
503 gras_msg_cb_t cb_cpt;
507 /* search the list of cb for this message on this host */
508 xbt_dynar_foreach(pd->cbl_list,cpt,list) {
509 if (list->id == msgtype->code) {
516 /* Remove it from the set */
518 xbt_dynar_foreach(list->cbs,cpt,cb_cpt) {
520 xbt_dynar_cursor_rm(list->cbs, &cpt);
526 VERB1("Ignoring removal of unexisting callback to msg id %d",