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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg,gras,"High level messaging");
18 xbt_set_t _gras_msgtype_set = NULL;
19 static char GRAS_header[6];
20 static char *make_namev(const char *name, short int ver);
25 * Initialize this submodule.
27 void gras_msg_init(void) {
28 /* only initialize once */
29 if (_gras_msgtype_set != NULL)
32 VERB0("Initializing Msg");
34 _gras_msgtype_set = xbt_set_new();
36 memcpy(GRAS_header,"GRAS", 4);
37 GRAS_header[4]=GRAS_PROTOCOL_VERSION;
38 GRAS_header[5]=(char)GRAS_THISARCH;
44 * Finalize the msg module
49 xbt_set_free(&_gras_msgtype_set);
57 void gras_msgtype_free(void *t) {
58 gras_msgtype_t msgtype=(gras_msgtype_t)t;
60 xbt_free(msgtype->name);
68 * Returns the versionned name of the message. If the version is 0, that's
69 * the name unchanged. Pay attention to this before free'ing the result.
71 static char *make_namev(const char *name, short int ver) {
77 namev = (char*)xbt_malloc(strlen(name)+2+3+1);
80 sprintf(namev,"%s_v%d",name,ver);
86 * gras_msgtype_declare:
87 * @name: name as it should be used for logging messages (must be uniq)
88 * @payload: datadescription of the payload
90 * Registers a message to the GRAS mecanism.
92 void gras_msgtype_declare(const char *name,
93 gras_datadesc_type_t payload) {
94 gras_msgtype_declare_v(name, 0, payload);
98 * gras_msgtype_declare_v:
99 * @name: name as it should be used for logging messages (must be uniq)
100 * @version: something like versionning symbol
101 * @payload: datadescription of the payload
103 * Registers a message to the GRAS mecanism. Use this version instead of
104 * gras_msgtype_declare when you change the semantic or syntax of a message and
105 * want your programs to be able to deal with both versions. Internally, each
106 * will be handled as an independent message type, so you can register
107 * differents for each of them.
110 gras_msgtype_declare_v(const char *name,
112 gras_datadesc_type_t payload) {
115 gras_msgtype_t msgtype;
116 char *namev=make_namev(name,version);
118 errcode = xbt_set_get_by_name(_gras_msgtype_set,
119 namev,(xbt_set_elm_t*)&msgtype);
121 if (errcode == no_error) {
122 VERB2("Re-register version %d of message '%s' (same payload, ignored).",
124 xbt_assert3(!gras_datadesc_type_cmp(msgtype->ctn_type, payload),
125 "Message %s re-registred with another payload (%s was %s)",
126 namev,gras_datadesc_get_name(payload),
127 gras_datadesc_get_name(msgtype->ctn_type));
129 return ; /* do really ignore it */
132 xbt_assert_error(mismatch_error); /* expect this error */
133 VERB3("Register version %d of message '%s' (payload: %s).",
134 version, name, gras_datadesc_get_name(payload));
136 msgtype = xbt_new(s_gras_msgtype_t,1);
137 msgtype->name = (namev == name ? strdup(name) : namev);
138 msgtype->name_len = strlen(namev);
139 msgtype->version = version;
140 msgtype->ctn_type = payload;
142 xbt_set_add(_gras_msgtype_set, (xbt_set_elm_t)msgtype,
147 * gras_msgtype_by_name:
149 * Retrieve a datatype description from its name
151 gras_msgtype_t gras_msgtype_by_name (const char *name) {
152 return gras_msgtype_by_namev(name,0);
155 * gras_msgtype_by_namev:
157 * Retrieve a datatype description from its name and version
159 gras_msgtype_t gras_msgtype_by_namev(const char *name,
164 char *namev = make_namev(name,version);
166 errcode = xbt_set_get_by_name(_gras_msgtype_set, namev,
167 (xbt_set_elm_t*)&res);
168 if (errcode != no_error)
171 WARN1("msgtype_by_name(%s) returns NULL",namev);
181 * Send the given message on the given socket
184 gras_msg_send(gras_socket_t sock,
185 gras_msgtype_t msgtype,
189 static gras_datadesc_type_t string_type=NULL;
192 RAISE0(mismatch_error,
193 "Cannot send the NULL message (did msgtype_by_name fail?)");
196 string_type = gras_datadesc_by_name("string");
197 xbt_assert(string_type);
200 DEBUG3("send '%s' to %s:%d", msgtype->name,
201 gras_socket_peer_name(sock),gras_socket_peer_port(sock));
202 TRY(gras_trp_chunk_send(sock, GRAS_header, 6));
204 TRY(gras_datadesc_send(sock, string_type, &msgtype->name));
205 TRY(gras_datadesc_send(sock, msgtype->ctn_type, payload));
206 TRY(gras_trp_flush(sock));
213 * receive the next message on the given socket.
216 gras_msg_recv(gras_socket_t sock,
217 gras_msgtype_t *msgtype,
222 static gras_datadesc_type_t string_type=NULL;
229 string_type=gras_datadesc_by_name("string");
230 xbt_assert(string_type);
233 TRY(gras_trp_chunk_recv(sock, header, 6));
234 for (cpt=0; cpt<4; cpt++)
235 if (header[cpt] != GRAS_header[cpt])
236 RAISE0(mismatch_error,"Incoming bytes do not look like a GRAS message");
237 if (header[4] != GRAS_header[4])
238 RAISE2(mismatch_error,"GRAS protocol mismatch (got %d, use %d)",
239 (int)header[4], (int)GRAS_header[4]);
240 r_arch = (int)header[5];
241 DEBUG2("Handle an incoming message using protocol %d (remote is %s)",
242 (int)header[4],gras_datadesc_arch_name(r_arch));
244 TRY(gras_datadesc_recv(sock, string_type, r_arch, &msg_name));
245 errcode = xbt_set_get_by_name(_gras_msgtype_set,
246 msg_name,(xbt_set_elm_t*)msgtype);
247 if (errcode != no_error)
249 "Got error %s while retrieving the type associated to messages '%s'",
250 xbt_error_name(errcode),msg_name);
251 /* FIXME: Survive unknown messages */
254 *payload_size=gras_datadesc_size((*msgtype)->ctn_type);
255 xbt_assert2(*payload_size > 0,
257 "Dynamic array as payload is forbided for now (FIXME?).",
258 "Reference to dynamic array is allowed.");
259 *payload = xbt_malloc(*payload_size);
260 TRY(gras_datadesc_recv(sock, (*msgtype)->ctn_type, r_arch, *payload));
267 * @timeout: How long should we wait for this message.
268 * @id: id of awaited msg
269 * @Returns: the error code (or no_error).
271 * Waits for a message to come in over a given socket.
273 * Every message of another type received before the one waited will be queued
274 * and used by subsequent call to this function or MsgHandle().
277 gras_msg_wait(double timeout,
278 gras_msgtype_t msgt_want,
279 gras_socket_t *expeditor,
282 gras_msgtype_t msgt_got;
284 int payload_size_got;
287 gras_procdata_t *pd=gras_procdata_get();
295 RAISE0(mismatch_error,
296 "Cannot wait for the NULL message (did msgtype_by_name fail?)");
298 VERB1("Waiting for message %s",msgt_want->name);
300 start = now = gras_os_time();
302 xbt_dynar_foreach(pd->msg_queue,cpt,msg){
303 if (msg.type->code == msgt_want->code) {
304 *expeditor = msg.expeditor;
305 memcpy(payload, msg.payload, msg.payload_size);
306 xbt_free(msg.payload);
307 xbt_dynar_cursor_rm(pd->msg_queue, &cpt);
308 VERB0("The waited message was queued");
314 TRY(gras_trp_select(timeout - now + start, expeditor));
315 TRY(gras_msg_recv(*expeditor, &msgt_got, &payload_got, &payload_size_got));
316 if (msgt_got->code == msgt_want->code) {
317 memcpy(payload, payload_got, payload_size_got);
318 xbt_free(payload_got);
319 VERB0("Got waited message");
323 /* not expected msg type. Queue it for later */
324 msg.expeditor = *expeditor;
326 msg.payload = payload;
327 msg.payload_size = payload_size_got;
328 xbt_dynar_push(pd->msg_queue,&msg);
331 if (now - start + 0.001 < timeout) {
332 RAISE1(timeout_error,"Timeout while waiting for msg %s",msgt_want->name);
341 * @timeOut: How long to wait for incoming messages
342 * @Returns: the error code (or no_error).
344 * Waits up to #timeOut# seconds to see if a message comes in; if so, calls the
345 * registered listener for that message (see RegisterCallback()).
348 gras_msg_handle(double timeOut) {
354 gras_socket_t expeditor;
357 gras_msgtype_t msgtype;
359 gras_procdata_t*pd=gras_procdata_get();
365 VERB1("Handling message within the next %.2fs",timeOut);
367 /* get a message (from the queue or from the net) */
368 if (xbt_dynar_length(pd->msg_queue)) {
369 xbt_dynar_shift(pd->msg_queue,&msg);
370 expeditor = msg.expeditor;
372 payload = msg.payload;
375 TRY(gras_trp_select(timeOut, &expeditor));
376 TRY(gras_msg_recv(expeditor, &msgtype, &payload, &payload_size));
380 xbt_dynar_foreach(pd->cbl_list,cpt,list) {
381 if (list->id == msgtype->code) {
388 INFO1("No callback for the incomming '%s' message. Discarded.",
390 WARN0("FIXME: gras_datadesc_free not implemented => leaking the payload");
394 xbt_dynar_foreach(list->cbs,cpt,cb) {
395 INFO3("Use the callback #%d (@%p) for incomming msg %s",
396 cpt+1,cb,msgtype->name);
397 if ((*cb)(expeditor,payload)) {
398 /* cb handled the message */
404 INFO1("Message '%s' refused by all registered callbacks", msgtype->name);
405 WARN0("FIXME: gras_datadesc_free not implemented => leaking the payload");
406 return mismatch_error;
410 gras_cbl_free(void *data){
411 gras_cblist_t *list=*(void**)data;
413 xbt_dynar_free(&( list->cbs ));
419 gras_cb_register(gras_msgtype_t msgtype,
421 gras_procdata_t *pd=gras_procdata_get();
422 gras_cblist_t *list=NULL;
425 DEBUG2("Register %p as callback to %s",cb,msgtype->name);
427 /* search the list of cb for this message on this host (creating if NULL) */
428 xbt_dynar_foreach(pd->cbl_list,cpt,list) {
429 if (list->id == msgtype->code) {
436 /* First cb? Create room */
437 list = xbt_new(gras_cblist_t,1);
438 list->id = msgtype->code;
439 list->cbs = xbt_dynar_new(sizeof(gras_cb_t), NULL);
440 xbt_dynar_push(pd->cbl_list,&list);
443 /* Insert the new one into the set */
444 xbt_dynar_insert_at(list->cbs,0,&cb);
448 gras_cb_unregister(gras_msgtype_t msgtype,
451 gras_procdata_t *pd=gras_procdata_get();
457 /* search the list of cb for this message on this host */
458 xbt_dynar_foreach(pd->cbl_list,cpt,list) {
459 if (list->id == msgtype->code) {
466 /* Remove it from the set */
468 xbt_dynar_foreach(list->cbs,cpt,cb_cpt) {
470 xbt_dynar_cursor_rm(list->cbs, &cpt);
476 VERB1("Ignoring removal of unexisting callback to msg id %d",