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 "Msg/msg_private.h"
12 #include "DataDesc/datadesc_interface.h"
13 #include "Transport/transport_interface.h" /* gras_trp_chunk_send/recv */
14 #include "Virtu/virtu_interface.h"
16 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(msg,GRAS);
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 TRYFAIL(gras_set_new(&_gras_msgtype_set));
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;
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 = 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.
96 gras_msgtype_declare(const char *name,
97 gras_datadesc_type_t *payload) {
98 return gras_msgtype_declare_v(name, 0, payload);
102 * gras_msgtype_declare_v:
103 * @name: name as it should be used for logging messages (must be uniq)
104 * @version: something like versionning symbol
105 * @payload: datadescription of the payload
107 * Registers a message to the GRAS mecanism. Use this version instead of
108 * gras_msgtype_declare when you change the semantic or syntax of a message and
109 * want your programs to be able to deal with both versions. Internally, each
110 * will be handled as an independent message type, so you can register
111 * differents for each of them.
114 gras_msgtype_declare_v(const char *name,
116 gras_datadesc_type_t *payload) {
118 gras_error_t errcode;
119 gras_msgtype_t *msgtype;
120 char *namev=make_namev(name,version);
125 errcode = gras_set_get_by_name(_gras_msgtype_set,
126 namev,(gras_set_elm_t**)&msgtype);
128 if (errcode == no_error) {
129 VERB2("Re-register version %d of message '%s' (same payload, ignored).",
131 gras_assert3(!gras_datadesc_type_cmp(msgtype->ctn_type, payload),
132 "Message %s re-registred with another payload (%s was %s)",
133 namev,gras_datadesc_get_name(payload),
134 gras_datadesc_get_name(msgtype->ctn_type));
135 } else if (errcode == mismatch_error) {
136 INFO3("Register version %d of message '%s' (payload: %s).",
137 version, name, gras_datadesc_get_name(payload));
139 return errcode; /* Was expecting for mismatch_error */
142 /* create type anyway so that the old type gets removed from here, and
143 hopefully free'd when ref counter gets 0 */
144 if (! (msgtype = malloc(sizeof(gras_msgtype_t))) )
147 msgtype->name = (namev == name ? strdup(name) : namev);
148 msgtype->name_len = strlen(namev);
149 msgtype->version = version;
150 msgtype->ctn_type = payload;
151 gras_datadesc_addref(payload);
153 TRY(gras_set_add(_gras_msgtype_set, (gras_set_elm_t*)msgtype,
154 &gras_msgtype_free));
160 * gras_msgtype_by_name:
162 * Retrieve a datatype description from its name
165 gras_msgtype_by_name (const char *name,
166 gras_msgtype_t **dst) {
167 return gras_msgtype_by_namev(name,0,dst);
170 * gras_msgtype_by_namev:
172 * Retrieve a datatype description from its name and version
175 gras_msgtype_by_namev(const char *name,
177 gras_msgtype_t **dst) {
179 gras_error_t errcode;
180 char *namev = make_namev(name,version);
182 errcode = gras_set_get_by_name(_gras_msgtype_set, namev,
183 (gras_set_elm_t**)dst);
193 * Send the given message on the given socket
196 gras_msg_send(gras_socket_t *sock,
197 gras_msgtype_t *msgtype,
200 gras_error_t errcode;
201 static gras_datadesc_type_t *string_type=NULL;
203 string_type = gras_datadesc_by_name("string");
204 gras_assert(string_type);
207 DEBUG3("send '%s' to %s:%d", msgtype->name,
208 gras_socket_peer_name(sock),gras_socket_peer_port(sock));
209 TRY(gras_trp_chunk_send(sock, GRAS_header, 6));
211 TRY(gras_datadesc_send(sock, string_type, &msgtype->name));
212 TRY(gras_datadesc_send(sock, msgtype->ctn_type, payload));
213 TRY(gras_trp_flush(sock));
220 * receive the next message on the given socket.
223 gras_msg_recv(gras_socket_t *sock,
224 gras_msgtype_t **msgtype,
228 gras_error_t errcode;
229 static gras_datadesc_type_t *string_type=NULL;
236 string_type=gras_datadesc_by_name("string");
237 gras_assert(string_type);
240 TRY(gras_trp_chunk_recv(sock, header, 6));
241 for (cpt=0; cpt<4; cpt++)
242 if (header[cpt] != GRAS_header[cpt])
243 RAISE0(mismatch_error,"Incoming bytes do not look like a GRAS message");
244 if (header[4] != GRAS_header[4])
245 RAISE2(mismatch_error,"GRAS protocol mismatch (got %d, use %d)",
246 (int)header[4], (int)GRAS_header[4]);
247 r_arch = (int)header[5];
248 DEBUG2("Handle an incoming message using protocol %d (remote is %s)",
249 (int)header[4],gras_datadesc_arch_name(r_arch));
251 TRY(gras_datadesc_recv(sock, string_type, r_arch, &msg_name));
252 errcode = gras_set_get_by_name(_gras_msgtype_set,
253 msg_name,(gras_set_elm_t**)msgtype);
254 if (errcode != no_error)
256 "Got error %s while retrieving the type associated to messages '%s'",
257 gras_error_name(errcode),msg_name);
258 /* FIXME: Survive unknown messages */
261 *payload_size=gras_datadesc_size((*msgtype)->ctn_type);
262 gras_assert2(*payload_size > 0,
264 "Dynamic array as payload is forbided for now (FIXME?).",
265 "Reference to dynamic array is allowed.");
266 *payload = malloc(*payload_size);
267 TRY(gras_datadesc_recv(sock, (*msgtype)->ctn_type, r_arch, *payload));
274 * @timeout: How long should we wait for this message.
275 * @id: id of awaited msg
276 * @Returns: the error code (or no_error).
278 * Waits for a message to come in over a given socket.
280 * Every message of another type received before the one waited will be queued
281 * and used by subsequent call to this function or MsgHandle().
284 gras_msg_wait(double timeout,
285 gras_msgtype_t *msgt_want,
286 gras_socket_t **expeditor,
289 gras_msgtype_t *msgt_got;
291 int payload_size_got;
292 gras_error_t errcode;
294 gras_procdata_t *pd=gras_procdata_get();
301 VERB1("Waiting for message %s",msgt_want->name);
303 start = now = gras_os_time();
305 gras_dynar_foreach(pd->msg_queue,cpt,msg){
306 if (msg.type->code == msgt_want->code) {
307 *expeditor = msg.expeditor;
308 memcpy(payload, msg.payload, msg.payload_size);
310 gras_dynar_cursor_rm(pd->msg_queue, &cpt);
311 VERB0("The waited message was queued");
317 TRY(gras_trp_select(timeout - now + start, expeditor));
318 TRY(gras_msg_recv(*expeditor, &msgt_got, &payload_got, &payload_size_got));
319 if (msgt_got->code == msgt_want->code) {
320 memcpy(payload, payload_got, payload_size_got);
322 VERB0("Got waited message");
326 /* not expected msg type. Queue it for later */
327 msg.expeditor = *expeditor;
329 msg.payload = payload;
330 msg.payload_size = payload_size_got;
331 TRY(gras_dynar_push(pd->msg_queue,&msg));
334 if (now - start + 0.001 < timeout) {
335 RAISE1(timeout_error,"Timeout while waiting for msg %s",msgt_want->name);
344 * @timeOut: How long to wait for incoming messages
345 * @Returns: the error code (or no_error).
347 * Waits up to #timeOut# seconds to see if a message comes in; if so, calls the
348 * registered listener for that message (see RegisterCallback()).
351 gras_msg_handle(double timeOut) {
353 gras_error_t errcode;
357 gras_socket_t *expeditor;
360 gras_msgtype_t *msgtype;
362 gras_procdata_t*pd=gras_procdata_get();
368 VERB1("Handling message within the next %.2fs",timeOut);
370 /* get a message (from the queue or from the net) */
371 if (gras_dynar_length(pd->msg_queue)) {
372 gras_dynar_shift(pd->msg_queue,&msg);
373 expeditor = msg.expeditor;
375 payload = msg.payload;
378 TRY(gras_trp_select(timeOut, &expeditor));
379 TRY(gras_msg_recv(expeditor, &msgtype, &payload, &payload_size));
383 gras_dynar_foreach(pd->cbl_list,cpt,list) {
384 if (list->id == msgtype->code) {
391 INFO1("No callback for the incomming '%s' message. Discarded.",
393 WARN0("FIXME: gras_datadesc_free not implemented => leaking the payload");
397 gras_dynar_foreach(list->cbs,cpt,cb) {
398 INFO3("Invoque the callback #%d (@%p) for incomming msg %s",
399 cpt+1,cb,msgtype->name);
400 if ((*cb)(expeditor,payload)) {
401 /* cb handled the message */
407 INFO1("Message '%s' refused by all registered callbacks", msgtype->name);
408 WARN0("FIXME: gras_datadesc_free not implemented => leaking the payload");
409 return mismatch_error;
413 gras_cbl_free(void *data){
414 gras_cblist_t *list=*(void**)data;
416 gras_dynar_free(list->cbs);
422 gras_cb_register(gras_msgtype_t *msgtype,
424 gras_error_t errcode;
425 gras_procdata_t *pd=gras_procdata_get();
426 gras_cblist_t *list=NULL;
429 DEBUG2("Register %p as callback to %s",cb,msgtype->name);
431 /* search the list of cb for this message on this host (creating if NULL) */
432 gras_dynar_foreach(pd->cbl_list,cpt,list) {
433 if (list->id == msgtype->code) {
440 /* First cb? Create room */
441 list = malloc(sizeof(gras_cblist_t));
445 list->id = msgtype->code;
446 TRY(gras_dynar_new(&(list->cbs), sizeof(gras_cb_t), NULL));
447 TRY(gras_dynar_push(pd->cbl_list,&list));
450 /* Insert the new one into the set */
451 TRY(gras_dynar_insert_at(list->cbs,0,&cb));
457 gras_cb_unregister(gras_msgtype_t *msgtype,
460 gras_procdata_t *pd=gras_procdata_get();
466 /* search the list of cb for this message on this host */
467 gras_dynar_foreach(pd->cbl_list,cpt,list) {
468 if (list->id == msgtype->code) {
475 /* Remove it from the set */
477 gras_dynar_foreach(list->cbs,cpt,cb_cpt) {
479 gras_dynar_cursor_rm(list->cbs, &cpt);
485 VERB1("Ignoring removal of unexisting callback to msg id %d",