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"
13 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(msg,GRAS);
15 gras_set_t *_gras_msgtype_set = NULL;
16 static char GRAS_header[6];
17 static char *make_namev(const char *name, short int ver);
22 * Initialize this submodule.
24 void gras_msg_init(void) {
27 /* only initialize once */
28 if (_gras_msgtype_set != NULL)
31 VERB0("Initializing Msg");
33 TRYFAIL(gras_set_new(&_gras_msgtype_set));
35 memcpy(GRAS_header,"GRAS", 4);
36 GRAS_header[4]=GRAS_PROTOCOL_VERSION;
37 GRAS_header[5]=(char)GRAS_THISARCH;
43 * Finalize the msg module
48 gras_set_free(&_gras_msgtype_set);
49 _gras_msgtype_set = NULL;
57 void gras_msgtype_free(void *t) {
58 gras_msgtype_t *msgtype=(gras_msgtype_t *)t;
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 = 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.
93 gras_msgtype_declare(const char *name,
94 gras_datadesc_type_t *payload,
95 gras_msgtype_t **dst) {
96 return gras_msgtype_declare_v(name, 0, payload, dst);
100 * gras_msgtype_declare_v:
101 * @name: name as it should be used for logging messages (must be uniq)
102 * @version: something like versionning symbol
103 * @payload: datadescription of the payload
105 * Registers a message to the GRAS mecanism. Use this version instead of
106 * gras_msgtype_declare when you change the semantic or syntax of a message and
107 * want your programs to be able to deal with both versions. Internally, each
108 * will be handled as an independent message type, so you can register
109 * differents for each of them.
112 gras_msgtype_declare_v(const char *name,
114 gras_datadesc_type_t *payload,
115 gras_msgtype_t **dst) {
117 gras_error_t errcode;
118 gras_msgtype_t *msgtype;
119 char *namev=make_namev(name,version);
124 errcode = gras_set_get_by_name(_gras_msgtype_set,
125 namev,(gras_set_elm_t**)&msgtype);
127 if (errcode == no_error) {
128 VERB2("Re-register version %d of message '%s' (same payload, ignored).",
130 gras_assert3(!gras_datadesc_type_cmp(msgtype->ctn_type, payload),
131 "Message %s re-registred with another payload (%s was %s)",
132 namev,gras_datadesc_get_name(payload),
133 gras_datadesc_get_name(msgtype->ctn_type));
134 } else if (errcode == mismatch_error) {
135 INFO3("Register version %d of message '%s' (payload: %s).",
136 version, name, gras_datadesc_get_name(payload));
138 return errcode; /* Was expecting for mismatch_error */
141 /* create type anyway so that the old type gets removed from here, and
142 hopefully free'd when ref counter gets 0 */
143 if (! (msgtype = malloc(sizeof(gras_msgtype_t))) )
146 msgtype->name = (namev == name ? strdup(name) : namev);
147 msgtype->name_len = strlen(namev);
148 msgtype->version = version;
149 msgtype->ctn_type = payload;
150 gras_datadesc_ref(payload);
152 TRY(gras_set_add(_gras_msgtype_set, (gras_set_elm_t*)msgtype,
153 &gras_msgtype_free));
159 * gras_msgtype_by_name:
161 * Retrieve a datatype description from its name
164 gras_msgtype_by_name (const char *name,
165 gras_msgtype_t **dst) {
166 return gras_msgtype_by_namev(name,0,dst);
169 * gras_msgtype_by_namev:
171 * Retrieve a datatype description from its name and version
174 gras_msgtype_by_namev(const char *name,
176 gras_msgtype_t **dst) {
178 gras_error_t errcode;
179 char *namev = make_namev(name,version);
181 errcode = gras_set_get_by_name(_gras_msgtype_set, namev,
182 (gras_set_elm_t**)dst);
192 * Send the given message on the given socket
195 gras_msg_send(gras_socket_t *sock,
196 gras_msgtype_t *msgtype,
199 gras_error_t errcode;
200 static gras_datadesc_type_t *string_type=NULL;
202 string_type = gras_datadesc_by_name("string");
203 gras_assert(string_type);
206 DEBUG3("send %s to %s:%d", msgtype->name,
207 gras_socket_peer_name(sock),gras_socket_peer_port(sock));
208 TRY(gras_trp_chunk_send(sock, GRAS_header, 6));
210 TRY(gras_datadesc_send(sock, string_type, &msgtype->name));
211 TRY(gras_datadesc_send(sock, msgtype->ctn_type, payload));
218 * receive the next message on the given socket.
221 gras_msg_recv(gras_socket_t *sock,
222 gras_msgtype_t **msgtype,
226 gras_error_t errcode;
227 static gras_datadesc_type_t *string_type=NULL;
234 string_type=gras_datadesc_by_name("string");
235 gras_assert(string_type);
238 TRY(gras_trp_chunk_recv(sock, header, 6));
239 for (cpt=0; cpt<4; cpt++)
240 if (header[cpt] != GRAS_header[cpt])
241 RAISE0(mismatch_error,"Incoming bytes do not look like a GRAS message");
242 if (header[4] != GRAS_header[4])
243 RAISE2(mismatch_error,"GRAS protocol mismatch (got %d, use %d)",
244 (int)header[4], (int)GRAS_header[4]);
245 r_arch = (int)header[5];
246 DEBUG2("Handle an incoming message using protocol %d (remote is %s)",
247 (int)header[4],gras_datadesc_arch_name(r_arch));
249 TRY(gras_datadesc_recv(sock, string_type, r_arch, &msg_name));
250 TRY(gras_set_get_by_name(_gras_msgtype_set,
251 msg_name,(gras_set_elm_t**)msgtype));
254 *payload_size=gras_datadesc_size((*msgtype)->ctn_type);
255 gras_assert2(*payload_size > 0,
257 "Dynamic array as payload is forbided for now (FIXME?).",
258 "Reference to dynamic array is allowed.");
259 *payload = 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;
285 gras_error_t errcode;
287 gras_procdata_t *pd=gras_procdata_get();
294 VERB1("Waiting for message %s",msgt_want->name);
296 start = now = gras_time();
298 gras_dynar_foreach(pd->msg_queue,cpt,msg){
299 if (msg.type->code == msgt_want->code) {
300 *expeditor = msg.expeditor;
301 memcpy(payload, msg.payload, msg.payload_size);
303 gras_dynar_cursor_rm(pd->msg_queue, &cpt);
304 VERB0("The waited message was queued");
310 TRY(gras_trp_select(timeout - now + start, expeditor));
311 TRY(gras_msg_recv(*expeditor, &msgt_got, &payload_got, &payload_size_got));
312 if (msgt_got->code == msgt_want->code) {
313 memcpy(payload, payload_got, payload_size_got);
315 VERB0("Got waited message");
319 /* not expected msg type. Queue it for later */
320 msg.expeditor = *expeditor;
322 msg.payload = payload;
323 msg.payload_size = payload_size_got;
324 TRY(gras_dynar_push(pd->msg_queue,&msg));
327 if (now - start + 0.001 < timeout) {
328 RAISE1(timeout_error,"Timeout while waiting for msg %s",msgt_want->name);
337 * @timeOut: How long to wait for incoming messages
338 * @Returns: the error code (or no_error).
340 * Waits up to #timeOut# seconds to see if a message comes in; if so, calls the
341 * registered listener for that message (see RegisterCallback()).
344 gras_msg_handle(double timeOut) {
346 gras_error_t errcode;
350 gras_socket_t *expeditor;
353 gras_msgtype_t *msgtype;
355 gras_procdata_t*pd=gras_procdata_get();
361 VERB1("Handling message within the next %.2fs",timeOut);
363 /* get a message (from the queue or from the net) */
364 if (gras_dynar_length(pd->msg_queue)) {
365 gras_dynar_shift(pd->msg_queue,&msg);
366 expeditor = msg.expeditor;
368 payload = msg.payload;
371 TRY(gras_trp_select(timeOut, &expeditor));
372 TRY(gras_msg_recv(expeditor, &msgtype, &payload, &payload_size));
376 gras_dynar_foreach(pd->cbl_list,cpt,list) {
377 if (list->id == msgtype->code) {
384 INFO1("No callback for the incomming '%s' message. Discarded.",
386 WARN0("FIXME: gras_datadesc_free not implemented => leaking the payload");
390 gras_dynar_foreach(list->cbs,cpt,cb) {
391 INFO3("Invoque the callback #%d (@%p) for incomming msg %s",
392 cpt+1,cb,msgtype->name);
393 if ((*cb)(expeditor,payload)) {
394 /* cb handled the message */
400 INFO1("Message '%s' refused by all registered callbacks", msgtype->name);
401 WARN0("FIXME: gras_datadesc_free not implemented => leaking the payload");
402 return mismatch_error;
406 gras_cbl_free(void *data){
407 gras_cblist_t *list=*(void**)data;
409 gras_dynar_free(list->cbs);
415 gras_cb_register(gras_msgtype_t *msgtype,
417 gras_error_t errcode;
418 gras_procdata_t *pd=gras_procdata_get();
419 gras_cblist_t *list=NULL;
422 DEBUG2("Register %p as callback to %s",cb,msgtype->name);
424 /* search the list of cb for this message on this host (creating if NULL) */
425 gras_dynar_foreach(pd->cbl_list,cpt,list) {
426 if (list->id == msgtype->code) {
433 /* First cb? Create room */
434 list = malloc(sizeof(gras_cblist_t));
438 list->id = msgtype->code;
439 TRY(gras_dynar_new(&(list->cbs), sizeof(gras_cb_t), NULL));
440 TRY(gras_dynar_push(pd->cbl_list,&list));
443 /* Insert the new one into the set */
444 TRY(gras_dynar_insert_at(list->cbs,0,&cb));
450 gras_cb_unregister(gras_msgtype_t *msgtype,
453 gras_procdata_t *pd=gras_procdata_get();
459 /* search the list of cb for this message on this host */
460 gras_dynar_foreach(pd->cbl_list,cpt,list) {
461 if (list->id == msgtype->code) {
468 /* Remove it from the set */
470 gras_dynar_foreach(list->cbs,cpt,cb_cpt) {
472 gras_dynar_cursor_rm(list->cbs, &cpt);
478 VERB1("Ignoring removal of unexisting callback to msg id %d",