Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Display the name of the canceled RPC
[simgrid.git] / src / gras / Msg / msg.c
1 /* $Id$ */
2
3 /* messaging - Function related to messaging (code shared between RL and SG)*/
4
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
6
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. */
9
10 #include "xbt/ex.h"
11 #include "xbt/ex_interface.h"
12 #include "gras/Msg/msg_private.h"
13 #include "gras/Virtu/virtu_interface.h"
14 #include "gras/DataDesc/datadesc_interface.h"
15 #include "gras/Transport/transport_interface.h" /* gras_select */
16 #include "portable.h" /* execinfo when available to propagate exceptions */
17
18 #ifndef MIN
19 #define MIN(a,b) ((a) < (b) ? (a) : (b))
20 #endif
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_msg,gras,"High level messaging");
23
24 xbt_set_t _gras_msgtype_set = NULL;
25 static char *make_namev(const char *name, short int ver);
26 char _GRAS_header[6];
27 const char *e_gras_msg_kind_names[e_gras_msg_kind_count]=
28   {"UNKNOWN","ONEWAY","RPC call","RPC answer","RPC error"};
29
30 /*
31  * Creating procdata for this module
32  */
33 static void *gras_msg_procdata_new() {
34    gras_msg_procdata_t res = xbt_new(s_gras_msg_procdata_t,1);
35    
36    res->name = xbt_strdup("gras_msg");
37    res->name_len = 0;
38    res->msg_queue = xbt_dynar_new(sizeof(s_gras_msg_t),   NULL);
39    res->cbl_list  = xbt_dynar_new(sizeof(gras_cblist_t *),gras_cbl_free);
40    res->timers    = xbt_dynar_new(sizeof(s_gras_timer_t), NULL);
41    
42    return (void*)res;
43 }
44
45 /*
46  * Freeing procdata for this module
47  */
48 static void gras_msg_procdata_free(void *data) {
49    gras_msg_procdata_t res = (gras_msg_procdata_t)data;
50    
51    xbt_dynar_free(&( res->msg_queue ));
52    xbt_dynar_free(&( res->cbl_list ));
53    xbt_dynar_free(&( res->timers ));
54
55    free(res->name);
56    free(res);
57 }
58
59 /*
60  * Module registration
61  */
62 int gras_msg_libdata_id;
63 void gras_msg_register() {
64    gras_msg_libdata_id = gras_procdata_add("gras_msg",gras_msg_procdata_new, gras_msg_procdata_free);
65 }
66
67 /*
68  * Initialize this submodule.
69  */
70 void gras_msg_init(void) {
71   /* only initialize once */
72   if (_gras_msgtype_set != NULL)
73     return;
74
75   VERB0("Initializing Msg");
76   
77   _gras_msgtype_set = xbt_set_new();
78
79   memcpy(_GRAS_header,"GRAS", 4);
80   _GRAS_header[4]=GRAS_PROTOCOL_VERSION;
81   _GRAS_header[5]=(char)GRAS_THISARCH;
82 }
83
84 /*
85  * Finalize the msg module
86  */
87 void
88 gras_msg_exit(void) {
89   VERB0("Exiting Msg");
90   xbt_set_free(&_gras_msgtype_set);
91 }
92
93 /*
94  * Reclamed memory
95  */
96 void gras_msgtype_free(void *t) {
97   gras_msgtype_t msgtype=(gras_msgtype_t)t;
98   if (msgtype) {
99     free(msgtype->name);
100     free(msgtype);
101   }
102 }
103
104 /**
105  * make_namev:
106  *
107  * Returns the versionned name of the message. If the version is 0, that's 
108  * the name unchanged. Pay attention to this before free'ing the result.
109  */
110 static char *make_namev(const char *name, short int ver) {
111   char *namev;
112
113   if (!ver)
114     return (char *)name;
115
116   namev = (char*)xbt_malloc(strlen(name)+2+3+1);
117
118   if (namev)
119       sprintf(namev,"%s_v%d",name,ver);
120
121   return namev;
122 }
123
124 /* Internal function doing the crude work of registering messages */
125 void 
126 gras_msgtype_declare_ext(const char           *name,
127                          short int             version,
128                          e_gras_msg_kind_t     kind, 
129                          gras_datadesc_type_t  payload_request,
130                          gras_datadesc_type_t  payload_answer) {
131
132   gras_msgtype_t msgtype=NULL;
133   char *namev=make_namev(name,version);
134   volatile int found = 0;
135   xbt_ex_t e;    
136   
137   TRY {
138     msgtype = (gras_msgtype_t)xbt_set_get_by_name(_gras_msgtype_set,namev);
139     found = 1;
140   } CATCH(e) {
141     if (e.category != not_found_error)
142       RETHROW;
143     xbt_ex_free(e);
144   }
145
146   if (found) {
147     VERB2("Re-register version %d of message '%s' (same kind & payload, ignored).",
148           version, name);
149     xbt_assert3(msgtype->kind == kind,
150                 "Message %s re-registered as a %s (it was known as a %s)",
151                 namev,e_gras_msg_kind_names[kind],e_gras_msg_kind_names[msgtype->kind]);
152     xbt_assert3(!gras_datadesc_type_cmp(msgtype->ctn_type, payload_request),
153                  "Message %s re-registred with another payload (%s was %s)",
154                  namev,gras_datadesc_get_name(payload_request),
155                  gras_datadesc_get_name(msgtype->ctn_type));
156
157     xbt_assert3(!gras_datadesc_type_cmp(msgtype->answer_type, payload_answer),
158              "Message %s re-registred with another answer payload (%s was %s)",
159                  namev,gras_datadesc_get_name(payload_answer),
160                  gras_datadesc_get_name(msgtype->answer_type));
161
162     return ; /* do really ignore it */
163
164   }
165
166   VERB4("Register version %d of message '%s' "
167         "(payload: %s; answer payload: %s).", 
168         version, name, gras_datadesc_get_name(payload_request),
169         gras_datadesc_get_name(payload_answer));    
170
171   msgtype = xbt_new(s_gras_msgtype_t,1);
172   msgtype->name = (namev == name ? strdup(name) : namev);
173   msgtype->name_len = strlen(namev);
174   msgtype->version = version;
175   msgtype->kind = kind;
176   msgtype->ctn_type = payload_request;
177   msgtype->answer_type = payload_answer;
178
179   xbt_set_add(_gras_msgtype_set, (xbt_set_elm_t)msgtype,
180                &gras_msgtype_free);
181 }
182
183
184 /** @brief declare a new message type of the given name. It only accepts the given datadesc as payload
185  *
186  * @param name: name as it should be used for logging messages (must be uniq)
187  * @param payload: datadescription of the payload
188  */
189 void gras_msgtype_declare(const char           *name,
190                           gras_datadesc_type_t  payload) {
191    gras_msgtype_declare_ext(name, 0, e_gras_msg_kind_oneway, payload, NULL);
192 }
193
194
195
196 /** @brief declare a new versionned message type of the given name and payload
197  *
198  * @param name: name as it should be used for logging messages (must be uniq)
199  * @param version: something like versionning symbol
200  * @param payload: datadescription of the payload
201  *
202  * Registers a message to the GRAS mechanism. Use this version instead of 
203  * gras_msgtype_declare when you change the semantic or syntax of a message and
204  * want your programs to be able to deal with both versions. Internally, each
205  * will be handled as an independent message type, so you can register 
206  * differents for each of them.
207  */
208 void
209 gras_msgtype_declare_v(const char           *name,
210                        short int             version,
211                        gras_datadesc_type_t  payload) {
212  
213    gras_msgtype_declare_ext(name, version, 
214                             e_gras_msg_kind_oneway, payload, NULL);
215 }
216
217 /** @brief retrieve an existing message type from its name. */
218 gras_msgtype_t gras_msgtype_by_name (const char *name) {
219   return gras_msgtype_by_namev(name,0);
220 }
221
222 /** @brief retrieve an existing message type from its name and version. */
223 gras_msgtype_t gras_msgtype_by_namev(const char      *name,
224                                      short int        version) {
225   gras_msgtype_t res = NULL;
226   char *namev = make_namev(name,version); 
227   xbt_ex_t e;
228
229   TRY {
230     res = (gras_msgtype_t)xbt_set_get_by_name(_gras_msgtype_set, namev);
231   } CATCH(e) {
232     xbt_ex_free(e);
233     THROW1(not_found_error,0,"No registred message of that name: %s",name);
234   }
235   if (name != namev) 
236     free(namev);
237   
238   return res;
239 }
240 /** @brief retrieve an existing message type from its name and version. */
241 gras_msgtype_t gras_msgtype_by_id(int id) {
242   return (gras_msgtype_t)xbt_set_get_by_id(_gras_msgtype_set, id);
243 }
244
245 /** \brief Waits for a message to come in over a given socket. 
246  *
247  * @param timeout: How long should we wait for this message.
248  * @param msgt_want: type of awaited msg (or NULL if I'm enclined to accept any message)
249  * @param expe_want: awaited expeditot (match on hostname, not port; NULL if not relevant)
250  * @param filter: function returning true or false when passed a payload. Messages for which it returns false are not selected. (NULL if not relevant)
251  * @param filter_ctx: context passed as second argument of the filter (a pattern to match?)
252  * @param[out] msg_got: where to write the message we got
253  *
254  * Every message of another type received before the one waited will be queued
255  * and used by subsequent call to this function or gras_msg_handle().
256  */
257
258 void
259 gras_msg_wait_ext(double           timeout,    
260
261                   gras_msgtype_t   msgt_want,
262                   gras_socket_t    expe_want,
263                   gras_msg_filter_t filter,
264                   void             *filter_ctx, 
265
266                   gras_msg_t       msg_got) {
267
268   s_gras_msg_t msg;
269   double start, now;
270   gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id);
271   int cpt;
272
273   xbt_assert0(msg_got,"msg_got is an output parameter");
274
275   start = gras_os_time();
276   VERB1("Waiting for message '%s'",msgt_want?msgt_want->name:"(any)");
277
278   xbt_dynar_foreach(pd->msg_queue,cpt,msg){
279     if ( (   !msgt_want || (msg.type->code == msgt_want->code)) 
280          && (!expe_want || (!strcmp( gras_socket_peer_name(msg.expe),
281                                      gras_socket_peer_name(expe_want))))
282          && (!filter || filter(&msg,filter_ctx))) {
283
284       memcpy(msg_got,&msg,sizeof(s_gras_msg_t));
285       xbt_dynar_cursor_rm(pd->msg_queue, &cpt);
286       VERB0("The waited message was queued");
287       return;
288     }
289   }
290
291   while (1) {
292     int need_restart;
293     xbt_ex_t e;
294
295   restart_receive: /* Goto here when the receive of a message failed */
296     need_restart=0;
297     now=gras_os_time();
298     memset(&msg,sizeof(msg),0);
299
300     TRY {
301       msg.expe = gras_trp_select(timeout ? timeout - now + start : 0);
302       gras_msg_recv(msg.expe, &msg);
303     } CATCH(e) {
304       if (e.category == system_error &&
305           !strncmp("Socket closed by remote side",e.msg,
306                   strlen("Socket closed by remote side"))) {
307         xbt_ex_free(e);
308         need_restart=1;
309       } else {
310         RETHROW;
311       }
312     }
313     if (need_restart)
314       goto restart_receive;
315
316     DEBUG0("Got a message from the socket");
317
318     if ( (   !msgt_want || (msg.type->code == msgt_want->code)) 
319          && (!expe_want || (!strcmp( gras_socket_peer_name(msg.expe),
320                                      gras_socket_peer_name(expe_want))))
321          && (!filter || filter(&msg,filter_ctx))) {
322
323       memcpy(msg_got,&msg,sizeof(s_gras_msg_t));
324       DEBUG0("Message matches expectations. Use it.");
325       return;
326     }
327     DEBUG0("Message does not match expectations. Queue it.");
328
329     /* not expected msg type. Queue it for later */
330     xbt_dynar_push(pd->msg_queue,&msg);
331     
332     now=gras_os_time();
333     if (now - start + 0.001 > timeout) {
334       THROW1(timeout_error,  now-start+0.001-timeout,
335              "Timeout while waiting for msg '%s'",
336              msgt_want?msgt_want->name:"(any)");
337     }
338   }
339
340   THROW_IMPOSSIBLE;
341 }
342 /** \brief Waits for a message to come in over a given socket. 
343  *
344  * @param timeout: How long should we wait for this message.
345  * @param msgt_want: type of awaited msg
346  * @param[out] expeditor: where to create a socket to answer the incomming message
347  * @param[out] payload: where to write the payload of the incomming message
348  * @return the error code (or no_error).
349  *
350  * Every message of another type received before the one waited will be queued
351  * and used by subsequent call to this function or gras_msg_handle().
352  */
353 void
354 gras_msg_wait(double           timeout,    
355               gras_msgtype_t   msgt_want,
356               gras_socket_t   *expeditor,
357               void            *payload) {
358   s_gras_msg_t msg;
359
360   gras_msg_wait_ext(timeout,
361                     msgt_want, NULL,      NULL, NULL,
362                     &msg);
363
364   if (msgt_want->ctn_type) {
365     xbt_assert1(payload,
366                 "Message type '%s' convey a payload you must accept",
367                 msgt_want->name);
368   } else {
369     xbt_assert1(!payload,
370                 "No payload was declared for message type '%s'",
371                 msgt_want->name);
372   }
373
374   if (payload) {
375     memcpy(payload,msg.payl,msg.payl_size);
376     free(msg.payl);
377   }
378
379   if (expeditor)
380     *expeditor = msg.expe;
381 }
382
383 static int gras_msg_wait_or_filter(gras_msg_t msg, void *ctx) {
384   xbt_dynar_t dyn=(xbt_dynar_t)ctx;
385   int res =  xbt_dynar_member(dyn,msg->type);
386   if (res)
387     VERB1("Got matching message (type=%s)",msg->type->name);
388   else
389     VERB0("Got message not matching our expectations");
390   return res;
391 }
392 /** \brief Waits for a message to come in over a given socket. 
393  *
394  * @param timeout: How long should we wait for this message.
395  * @param msgt_want: a dynar containing all accepted message type
396  * @param[out] ctx: the context of received message (in case it's a RPC call we want to answer to)
397  * @param[out] msgt_got: indice in the dynar of the type of the received message 
398  * @param[out] payload: where to write the payload of the incomming message
399  * @return the error code (or no_error).
400  *
401  * Every message of a type not in the accepted list received before the one
402  * waited will be queued and used by subsequent call to this function or
403  * gras_msg_handle().
404  *
405  * If you are interested in the context, pass the address of a s_gras_msg_cb_ctx_t variable.
406  */
407 void gras_msg_wait_or(double         timeout,
408                       xbt_dynar_t    msgt_want,
409                       gras_msg_cb_ctx_t *ctx,
410                       int           *msgt_got,
411                       void          *payload) {
412   s_gras_msg_t msg;
413
414   VERB1("Wait %f seconds for several message types",timeout);
415   gras_msg_wait_ext(timeout,
416                     NULL, NULL,      
417                     &gras_msg_wait_or_filter, (void*)msgt_want,
418                     &msg);
419
420   if (msg.type->ctn_type) {
421     xbt_assert1(payload,
422                 "Message type '%s' convey a payload you must accept",
423                 msg.type->name);
424   } /* don't check the other side since some of the types may have a payload */
425
426   if (payload && msg.type->ctn_type) {
427     memcpy(payload,msg.payl,msg.payl_size);
428     free(msg.payl);
429   }
430
431   if (ctx) 
432     *ctx=gras_msg_cb_ctx_new(msg.expe,msg.type,msg.ID,60);
433
434   if (msgt_got)
435     *msgt_got = xbt_dynar_search(msgt_want,msg.type);
436 }
437
438
439 /** \brief Send the data pointed by \a payload as a message of type
440  * \a msgtype to the peer \a sock */
441 void
442 gras_msg_send(gras_socket_t   sock,
443               gras_msgtype_t  msgtype,
444               void           *payload) {
445
446   if (msgtype->ctn_type) {
447     xbt_assert1(payload,
448                 "Message type '%s' convey a payload you must provide",
449                 msgtype->name);
450   } else {
451     xbt_assert1(!payload,
452                 "No payload was declared for message type '%s'",
453                 msgtype->name);
454   }
455
456   DEBUG2("Send a oneway message of type '%s'. Payload=%p",
457          msgtype->name,payload);
458   gras_msg_send_ext(sock, e_gras_msg_kind_oneway,0, msgtype, payload);
459   VERB2("Sent a oneway message of type '%s'. Payload=%p",
460         msgtype->name,payload);
461 }
462
463 /** @brief Handle all messages arriving within the given period
464  *
465  * @param period: How long to wait for incoming messages (in seconds)
466  *
467  * Messages are dealed with just like gras_msg_handle() would do. The
468  * difference is that gras_msg_handle() handles at most one message (or wait up
469  * to timeout second when no message arrives) while this function handles any
470  * amount of messages, and lasts the given period in any case.
471  */
472 void 
473 gras_msg_handleall(double period) {
474   xbt_ex_t e;
475   double begin=gras_os_time();
476   double now;
477
478   do {
479     now=gras_os_time();
480     TRY{
481       if (period - now + begin > 0)
482         gras_msg_handle(period - now + begin);
483     } CATCH(e) {
484       if (e.category != timeout_error) 
485         RETHROW0("Error while waiting for messages: %s");
486       xbt_ex_free(e);
487     }
488   } while (now - begin < period);
489 }
490
491 /** @brief Handle an incomming message or timer (or wait up to \a timeOut seconds)
492  *
493  * @param timeOut: How long to wait for incoming messages (in seconds)
494  * @return the error code (or no_error).
495  *
496  * Messages are passed to the callbacks. See also gras_msg_handleall().
497  */
498 void
499 gras_msg_handle(double timeOut) {
500   
501   double          untiltimer;
502    
503   int             cpt, ran_ok;
504
505   s_gras_msg_t    msg;
506
507   gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id);
508   gras_cblist_t  *list=NULL;
509   gras_msg_cb_t       cb;
510   s_gras_msg_cb_ctx_t ctx;
511    
512   int timerexpected, timeouted;
513   xbt_ex_t e;
514
515   VERB1("Handling message within the next %.2fs",timeOut);
516   
517   untiltimer = gras_msg_timer_handle();
518   DEBUG1("Next timer in %f sec", untiltimer);
519   if (untiltimer == 0.0) {
520      /* A timer was already elapsed and handled */
521      return;
522   }
523   if (untiltimer != -1.0) {
524      timerexpected = 1;
525      timeOut = MIN(timeOut, untiltimer);
526   } else {
527      timerexpected = 0;
528   }
529    
530   /* get a message (from the queue or from the net) */
531   timeouted = 0;
532   if (xbt_dynar_length(pd->msg_queue)) {
533     DEBUG0("Get a message from the queue");
534     xbt_dynar_shift(pd->msg_queue,&msg);
535   } else {
536     TRY {
537       msg.expe = gras_trp_select(timeOut);
538     } CATCH(e) {
539       if (e.category != timeout_error)
540         RETHROW;
541       xbt_ex_free(e);
542       timeouted = 1;
543     }
544
545     if (!timeouted) {
546       TRY {
547         /* FIXME: if not the right kind, queue it and recall ourself or goto >:-) */
548         gras_msg_recv(msg.expe, &msg);
549         DEBUG1("Received a msg from the socket kind:%s",
550                e_gras_msg_kind_names[msg.kind]);
551     
552       } CATCH(e) {
553         RETHROW4("Error while receiving a message on select()ed socket %p to [%s]%s:%d: %s",
554                  msg.expe,
555                  gras_socket_peer_proc(msg.expe),gras_socket_peer_name(msg.expe),
556                  gras_socket_peer_port(msg.expe));
557       }
558     }
559   }
560
561   if (timeouted) {
562      if (timerexpected) {
563           
564         /* A timer elapsed before the arrival of any message even if we select()ed a bit */
565         untiltimer = gras_msg_timer_handle();
566         if (untiltimer == 0.0) {
567           /* we served a timer, we're done */
568           return;
569         } else {
570            xbt_assert1(untiltimer>0, "Negative timer (%f). I'm 'puzzeled'", untiltimer);
571            WARN1("No timer elapsed, in contrary to expectations (next in %f sec)",
572                   untiltimer);
573            THROW1(timeout_error,0,
574                   "No timer elapsed, in contrary to expectations (next in %f sec)",
575                   untiltimer);
576         }
577         
578      } else {
579         /* select timeouted, and no timer elapsed. Nothing to do */
580        THROW1(timeout_error, 0, "No new message or timer (delay was %f)",
581               timeOut);
582      }
583      
584   }
585    
586   /* A message was already there or arrived in the meanwhile. handle it */
587   xbt_dynar_foreach(pd->cbl_list,cpt,list) {
588     if (list->id == msg.type->code) {
589       break;
590     } else {
591       list=NULL;
592     }
593   }
594   if (!list) {
595     INFO3("No callback for the incomming '%s' message (from %s:%d). Discarded.", 
596           msg.type->name,
597           gras_socket_peer_name(msg.expe),gras_socket_peer_port(msg.expe));
598     WARN0("FIXME: gras_datadesc_free not implemented => leaking the payload");
599     return;
600   }
601   
602   ctx.expeditor = msg.expe;
603   ctx.ID = msg.ID;
604   ctx.msgtype = msg.type;
605
606   switch (msg.kind) {
607   case e_gras_msg_kind_oneway:
608   case e_gras_msg_kind_rpccall:
609     ran_ok=0;
610     TRY {
611       xbt_dynar_foreach(list->cbs,cpt,cb) { 
612         if (!ran_ok) {
613           DEBUG4("Use the callback #%d (@%p) for incomming msg %s (payload_size=%d)",
614                 cpt+1,cb,msg.type->name,msg.payl_size);
615           if ((*cb)(&ctx,msg.payl)) {
616             /* cb handled the message */
617             free(msg.payl);
618             ran_ok = 1;
619           }
620         }
621       }
622     } CATCH(e) {
623       free(msg.payl);
624       if (msg.type->kind == e_gras_msg_kind_rpccall) {
625         /* The callback raised an exception, propagate it on the network */
626         if (!e.remote) { /* the exception is born on this machine */
627           e.host = (char*)gras_os_myname();
628           xbt_ex_setup_backtrace(&e);
629         } 
630         VERB5("Propagate %s exception ('%s') from '%s' RPC cb back to %s:%d",
631               (e.remote ? "remote" : "local"),
632               e.msg,
633               msg.type->name,
634               gras_socket_peer_name(msg.expe),
635               gras_socket_peer_port(msg.expe));
636         gras_msg_send_ext(msg.expe, e_gras_msg_kind_rpcerror,
637                           msg.ID, msg.type, &e);
638         xbt_ex_free(e);
639         ran_ok=1;
640       } else {
641         RETHROW0("Callback raised an exception: %s");
642       }
643     }
644     if (!ran_ok)
645       THROW1(mismatch_error,0,
646              "Message '%s' refused by all registered callbacks", msg.type->name);
647     /* FIXME: gras_datadesc_free not implemented => leaking the payload */
648     break;
649
650
651   case e_gras_msg_kind_rpcanswer:
652     INFO1("Unexpected RPC answer discarded (type: %s)", msg.type->name);
653     WARN0("FIXME: gras_datadesc_free not implemented => leaking the payload");
654     return;
655
656   case e_gras_msg_kind_rpcerror:
657     INFO1("Unexpected RPC error discarded (type: %s)", msg.type->name);
658     WARN0("FIXME: gras_datadesc_free not implemented => leaking the payload");
659     return;
660
661   default:
662     THROW1(unknown_error,0,
663            "Cannot handle messages of kind %d yet",msg.type->kind);
664   }
665
666 }
667
668 void
669 gras_cbl_free(void *data){
670   gras_cblist_t *list=*(void**)data;
671   if (list) {
672     xbt_dynar_free(&( list->cbs ));
673     free(list);
674   }
675 }
676
677 /** \brief Bind the given callback to the given message type 
678  *
679  * Several callbacks can be attached to a given message type. The lastly added one will get the message first, and 
680  * if it returns false, the message will be passed to the second one. 
681  * And so on until one of the callbacks accepts the message.
682  */
683 void
684 gras_cb_register(gras_msgtype_t msgtype,
685                  gras_msg_cb_t cb) {
686   gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id);
687   gras_cblist_t *list=NULL;
688   int cpt;
689
690   DEBUG2("Register %p as callback to '%s'",cb,msgtype->name);
691
692   /* search the list of cb for this message on this host (creating if NULL) */
693   xbt_dynar_foreach(pd->cbl_list,cpt,list) {
694     if (list->id == msgtype->code) {
695       break;
696     } else {
697       list=NULL;
698     }
699   }
700   if (!list) {
701     /* First cb? Create room */
702     list = xbt_new(gras_cblist_t,1);
703     list->id = msgtype->code;
704     list->cbs = xbt_dynar_new(sizeof(gras_msg_cb_t), NULL);
705     xbt_dynar_push(pd->cbl_list,&list);
706   }
707
708   /* Insert the new one into the set */
709   xbt_dynar_insert_at(list->cbs,0,&cb);
710 }
711
712 /** \brief Unbind the given callback from the given message type */
713 void
714 gras_cb_unregister(gras_msgtype_t msgtype,
715                    gras_msg_cb_t cb) {
716
717   gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id);
718   gras_cblist_t *list;
719   gras_msg_cb_t cb_cpt;
720   int cpt;
721   int found = 0;
722
723   /* search the list of cb for this message on this host */
724   xbt_dynar_foreach(pd->cbl_list,cpt,list) {
725     if (list->id == msgtype->code) {
726       break;
727     } else {
728       list=NULL;
729     }
730   }
731
732   /* Remove it from the set */
733   if (list) {
734     xbt_dynar_foreach(list->cbs,cpt,cb_cpt) {
735       if (cb == cb_cpt) {
736         xbt_dynar_cursor_rm(list->cbs, &cpt);
737         found = 1;
738       }
739     }
740   }
741   if (!found)
742     VERB1("Ignoring removal of unexisting callback to msg id %d",
743           msgtype->code);
744 }
745
746 /** \brief Retrieve the expeditor of the message */
747 gras_socket_t gras_msg_cb_ctx_from(gras_msg_cb_ctx_t ctx) {
748   return ctx->expeditor;
749 }
750 /* \brief Creates a new message exchange context (user should never have to) */
751 gras_msg_cb_ctx_t gras_msg_cb_ctx_new(gras_socket_t expe, 
752                                       gras_msgtype_t msgtype,
753                                       unsigned long int ID,
754                                       double timeout) {
755   gras_msg_cb_ctx_t res=xbt_new(s_gras_msg_cb_ctx_t,1);
756   res->expeditor = expe;
757   res->msgtype = msgtype;
758   res->ID = ID;
759   res->timeout = timeout;
760   return res;
761 }
762 /* \brief Frees a message exchange context 
763  *
764  * This function is mainly useful with \ref gras_msg_wait_or, ie seldom.
765  */
766 void gras_msg_cb_ctx_free(gras_msg_cb_ctx_t ctx) {
767   free(ctx);
768 }