3 /* messaging - Function related to messaging (code shared between RL and SG)*/
5 /* Authors: Martin Quinson */
6 /* Copyright (C) 2003 the OURAGAN project. */
8 /* This program is free software; you can redistribute it and/or modify it
9 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 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(msg,gras,"High level messaging");
18 gras_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) {
30 /* only initialize once */
31 if (_gras_msgtype_set != NULL)
34 VERB0("Initializing Msg");
36 _gras_msgtype_set = gras_set_new();
38 memcpy(GRAS_header,"GRAS", 4);
39 GRAS_header[4]=GRAS_PROTOCOL_VERSION;
40 GRAS_header[5]=(char)GRAS_THISARCH;
46 * Finalize the msg module
51 gras_set_free(&_gras_msgtype_set);
52 _gras_msgtype_set = NULL;
60 void gras_msgtype_free(void *t) {
61 gras_msgtype_t *msgtype=(gras_msgtype_t *)t;
63 gras_free(msgtype->name);
71 * Returns the versionned name of the message. If the version is 0, that's
72 * the name unchanged. Pay attention to this before free'ing the result.
74 static char *make_namev(const char *name, short int ver) {
80 namev = (char*)gras_malloc(strlen(name)+2+3+1);
83 sprintf(namev,"%s_v%d",name,ver);
89 * gras_msgtype_declare:
90 * @name: name as it should be used for logging messages (must be uniq)
91 * @payload: datadescription of the payload
93 * Registers a message to the GRAS mecanism.
95 void gras_msgtype_declare(const char *name,
96 gras_datadesc_type_t *payload) {
97 gras_msgtype_declare_v(name, 0, payload);
101 * gras_msgtype_declare_v:
102 * @name: name as it should be used for logging messages (must be uniq)
103 * @version: something like versionning symbol
104 * @payload: datadescription of the payload
106 * Registers a message to the GRAS mecanism. Use this version instead of
107 * gras_msgtype_declare when you change the semantic or syntax of a message and
108 * want your programs to be able to deal with both versions. Internally, each
109 * will be handled as an independent message type, so you can register
110 * differents for each of them.
113 gras_msgtype_declare_v(const char *name,
115 gras_datadesc_type_t *payload) {
117 gras_error_t errcode;
118 gras_msgtype_t *msgtype;
119 char *namev=make_namev(name,version);
121 errcode = gras_set_get_by_name(_gras_msgtype_set,
122 namev,(gras_set_elm_t**)&msgtype);
124 if (errcode == no_error) {
125 VERB2("Re-register version %d of message '%s' (same payload, ignored).",
127 gras_assert3(!gras_datadesc_type_cmp(msgtype->ctn_type, payload),
128 "Message %s re-registred with another payload (%s was %s)",
129 namev,gras_datadesc_get_name(payload),
130 gras_datadesc_get_name(msgtype->ctn_type));
132 return ; /* do really ignore it */
135 gras_assert_error(mismatch_error); /* expect this error */
136 INFO3("Register version %d of message '%s' (payload: %s).",
137 version, name, gras_datadesc_get_name(payload));
139 msgtype = gras_new(gras_msgtype_t,1);
140 msgtype->name = (namev == name ? strdup(name) : namev);
141 msgtype->name_len = strlen(namev);
142 msgtype->version = version;
143 msgtype->ctn_type = payload;
145 gras_set_add(_gras_msgtype_set, (gras_set_elm_t*)msgtype,
150 * gras_msgtype_by_name:
152 * Retrieve a datatype description from its name
154 gras_msgtype_t * gras_msgtype_by_name (const char *name) {
155 return gras_msgtype_by_namev(name,0);
158 * gras_msgtype_by_namev:
160 * Retrieve a datatype description from its name and version
162 gras_msgtype_t * gras_msgtype_by_namev(const char *name,
166 gras_error_t errcode;
167 char *namev = make_namev(name,version);
169 errcode = gras_set_get_by_name(_gras_msgtype_set, namev,
170 (gras_set_elm_t**)&res);
171 if (errcode != no_error)
182 * Send the given message on the given socket
185 gras_msg_send(gras_socket_t *sock,
186 gras_msgtype_t *msgtype,
189 gras_error_t errcode;
190 static gras_datadesc_type_t *string_type=NULL;
193 RAISE0(mismatch_error,
194 "Cannot send the NULL message (did msgtype_by_name fail?)");
197 string_type = gras_datadesc_by_name("string");
198 gras_assert(string_type);
201 DEBUG3("send '%s' to %s:%d", msgtype->name,
202 gras_socket_peer_name(sock),gras_socket_peer_port(sock));
203 TRY(gras_trp_chunk_send(sock, GRAS_header, 6));
205 TRY(gras_datadesc_send(sock, string_type, &msgtype->name));
206 TRY(gras_datadesc_send(sock, msgtype->ctn_type, payload));
207 TRY(gras_trp_flush(sock));
214 * receive the next message on the given socket.
217 gras_msg_recv(gras_socket_t *sock,
218 gras_msgtype_t **msgtype,
222 gras_error_t errcode;
223 static gras_datadesc_type_t *string_type=NULL;
230 string_type=gras_datadesc_by_name("string");
231 gras_assert(string_type);
234 TRY(gras_trp_chunk_recv(sock, header, 6));
235 for (cpt=0; cpt<4; cpt++)
236 if (header[cpt] != GRAS_header[cpt])
237 RAISE0(mismatch_error,"Incoming bytes do not look like a GRAS message");
238 if (header[4] != GRAS_header[4])
239 RAISE2(mismatch_error,"GRAS protocol mismatch (got %d, use %d)",
240 (int)header[4], (int)GRAS_header[4]);
241 r_arch = (int)header[5];
242 DEBUG2("Handle an incoming message using protocol %d (remote is %s)",
243 (int)header[4],gras_datadesc_arch_name(r_arch));
245 TRY(gras_datadesc_recv(sock, string_type, r_arch, &msg_name));
246 errcode = gras_set_get_by_name(_gras_msgtype_set,
247 msg_name,(gras_set_elm_t**)msgtype);
248 if (errcode != no_error)
250 "Got error %s while retrieving the type associated to messages '%s'",
251 gras_error_name(errcode),msg_name);
252 /* FIXME: Survive unknown messages */
255 *payload_size=gras_datadesc_size((*msgtype)->ctn_type);
256 gras_assert2(*payload_size > 0,
258 "Dynamic array as payload is forbided for now (FIXME?).",
259 "Reference to dynamic array is allowed.");
260 *payload = gras_malloc(*payload_size);
261 TRY(gras_datadesc_recv(sock, (*msgtype)->ctn_type, r_arch, *payload));
268 * @timeout: How long should we wait for this message.
269 * @id: id of awaited msg
270 * @Returns: the error code (or no_error).
272 * Waits for a message to come in over a given socket.
274 * Every message of another type received before the one waited will be queued
275 * and used by subsequent call to this function or MsgHandle().
278 gras_msg_wait(double timeout,
279 gras_msgtype_t *msgt_want,
280 gras_socket_t **expeditor,
283 gras_msgtype_t *msgt_got;
285 int payload_size_got;
286 gras_error_t errcode;
288 gras_procdata_t *pd=gras_procdata_get();
296 RAISE0(mismatch_error,
297 "Cannot wait for the NULL message (did msgtype_by_name fail?)");
299 VERB1("Waiting for message %s",msgt_want->name);
301 start = now = gras_os_time();
303 gras_dynar_foreach(pd->msg_queue,cpt,msg){
304 if (msg.type->code == msgt_want->code) {
305 *expeditor = msg.expeditor;
306 memcpy(payload, msg.payload, msg.payload_size);
307 gras_free(msg.payload);
308 gras_dynar_cursor_rm(pd->msg_queue, &cpt);
309 VERB0("The waited message was queued");
315 TRY(gras_trp_select(timeout - now + start, expeditor));
316 TRY(gras_msg_recv(*expeditor, &msgt_got, &payload_got, &payload_size_got));
317 if (msgt_got->code == msgt_want->code) {
318 memcpy(payload, payload_got, payload_size_got);
319 gras_free(payload_got);
320 VERB0("Got waited message");
324 /* not expected msg type. Queue it for later */
325 msg.expeditor = *expeditor;
327 msg.payload = payload;
328 msg.payload_size = payload_size_got;
329 gras_dynar_push(pd->msg_queue,&msg);
332 if (now - start + 0.001 < timeout) {
333 RAISE1(timeout_error,"Timeout while waiting for msg %s",msgt_want->name);
342 * @timeOut: How long to wait for incoming messages
343 * @Returns: the error code (or no_error).
345 * Waits up to #timeOut# seconds to see if a message comes in; if so, calls the
346 * registered listener for that message (see RegisterCallback()).
349 gras_msg_handle(double timeOut) {
351 gras_error_t errcode;
355 gras_socket_t *expeditor;
358 gras_msgtype_t *msgtype;
360 gras_procdata_t*pd=gras_procdata_get();
366 VERB1("Handling message within the next %.2fs",timeOut);
368 /* get a message (from the queue or from the net) */
369 if (gras_dynar_length(pd->msg_queue)) {
370 gras_dynar_shift(pd->msg_queue,&msg);
371 expeditor = msg.expeditor;
373 payload = msg.payload;
376 TRY(gras_trp_select(timeOut, &expeditor));
377 TRY(gras_msg_recv(expeditor, &msgtype, &payload, &payload_size));
381 gras_dynar_foreach(pd->cbl_list,cpt,list) {
382 if (list->id == msgtype->code) {
389 INFO1("No callback for the incomming '%s' message. Discarded.",
391 WARN0("FIXME: gras_datadesc_free not implemented => leaking the payload");
395 gras_dynar_foreach(list->cbs,cpt,cb) {
396 INFO3("Invoque the callback #%d (@%p) for incomming msg %s",
397 cpt+1,cb,msgtype->name);
398 if ((*cb)(expeditor,payload)) {
399 /* cb handled the message */
405 INFO1("Message '%s' refused by all registered callbacks", msgtype->name);
406 WARN0("FIXME: gras_datadesc_free not implemented => leaking the payload");
407 return mismatch_error;
411 gras_cbl_free(void *data){
412 gras_cblist_t *list=*(void**)data;
414 gras_dynar_free(list->cbs);
420 gras_cb_register(gras_msgtype_t *msgtype,
422 gras_procdata_t *pd=gras_procdata_get();
423 gras_cblist_t *list=NULL;
426 DEBUG2("Register %p as callback to %s",cb,msgtype->name);
428 /* search the list of cb for this message on this host (creating if NULL) */
429 gras_dynar_foreach(pd->cbl_list,cpt,list) {
430 if (list->id == msgtype->code) {
437 /* First cb? Create room */
438 list = gras_new(gras_cblist_t,1);
439 list->id = msgtype->code;
440 list->cbs = gras_dynar_new(sizeof(gras_cb_t), NULL);
441 gras_dynar_push(pd->cbl_list,&list);
444 /* Insert the new one into the set */
445 gras_dynar_insert_at(list->cbs,0,&cb);
449 gras_cb_unregister(gras_msgtype_t *msgtype,
452 gras_procdata_t *pd=gras_procdata_get();
458 /* search the list of cb for this message on this host */
459 gras_dynar_foreach(pd->cbl_list,cpt,list) {
460 if (list->id == msgtype->code) {
467 /* Remove it from the set */
469 gras_dynar_foreach(list->cbs,cpt,cb_cpt) {
471 gras_dynar_cursor_rm(list->cbs, &cpt);
477 VERB1("Ignoring removal of unexisting callback to msg id %d",