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) {
72 char *namev=malloc(strlen(name)+2+3+1);
78 sprintf(namev,"%s_v%d",name,ver);
84 * gras_msgtype_declare:
85 * @name: name as it should be used for logging messages (must be uniq)
86 * @payload: datadescription of the payload
88 * Registers a message to the GRAS mecanism.
91 gras_msgtype_declare(const char *name,
92 gras_datadesc_type_t *payload,
93 gras_msgtype_t **dst) {
94 return gras_msgtype_declare_v(name, 0, payload, dst);
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,
113 gras_msgtype_t **dst) {
115 gras_error_t errcode;
116 gras_msgtype_t *msgtype;
117 char *namev=make_namev(name,version);
122 errcode = gras_set_get_by_name(_gras_msgtype_set,
123 namev,(gras_set_elm_t**)&msgtype);
124 if (errcode == mismatch_error) {
126 if (! (msgtype = malloc(sizeof(gras_msgtype_t))) )
129 msgtype->name = (namev == name ? strdup(name) : namev);
130 msgtype->name_len = strlen(namev);
131 msgtype->version = version;
132 msgtype->ctn_type = payload;
134 TRY(gras_set_add(_gras_msgtype_set, (gras_set_elm_t*)msgtype,
135 &gras_msgtype_free));
137 DEBUG2("Register version %d of message '%s'.", version, name);
138 } else if (errcode == no_error) { /* found */
142 gras_assert1(!gras_datadesc_type_cmp(msgtype->ctn_type, payload),
143 "Message %s registred again with a different payload",
148 return errcode; /* Error is set lookup */
155 * gras_msgtype_by_name:
157 * Retrieve a datatype description from its name
160 gras_msgtype_by_name (const char *name,
161 gras_msgtype_t **dst) {
162 return gras_msgtype_by_namev(name,0,dst);
165 * gras_msgtype_by_namev:
167 * Retrieve a datatype description from its name and version
170 gras_msgtype_by_namev(const char *name,
172 gras_msgtype_t **dst) {
174 gras_error_t errcode;
175 char *namev = make_namev(name,version);
177 TRY(gras_set_get_by_name(_gras_msgtype_set, namev,
178 (gras_set_elm_t**)dst));
188 * Send the given message on the given socket
191 gras_msg_send(gras_socket_t *sock,
192 gras_msgtype_t *msgtype,
195 gras_error_t errcode;
196 static gras_datadesc_type_t *string_type=NULL;
198 string_type = gras_datadesc_by_name("string");
199 gras_assert(string_type);
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));
212 * receive the next message on the given socket (which should be dropped
213 * when the function returns an error)
216 gras_msg_recv(gras_socket_t *sock,
217 gras_msgtype_t **msgtype,
220 gras_error_t errcode;
221 static gras_datadesc_type_t *string_type=NULL;
228 string_type=gras_datadesc_by_name("string");
229 gras_assert(string_type);
232 TRY(gras_trp_chunk_recv(sock, header, 6));
233 for (cpt=0; cpt<4; cpt++)
234 if (header[cpt] != GRAS_header[cpt])
235 RAISE0(mismatch_error,"Incoming bytes do not look like a GRAS message");
236 if (header[4] != GRAS_header[4])
237 RAISE2(mismatch_error,"GRAS protocol mismatch (got %d, use %d)",
238 (int)header[4], (int)GRAS_header[4]);
239 r_arch = (int)header[5];
241 TRY(gras_datadesc_recv(sock, string_type, r_arch,(void**) &msg_name));
242 TRY(gras_set_get_by_name(_gras_msgtype_set,
243 msg_name,(gras_set_elm_t**)msgtype));
244 TRY(gras_datadesc_recv(sock, (*msgtype)->ctn_type, r_arch, payload));
251 * @timeout: How long should we wait for this message.
252 * @id: id of awaited msg
253 * @Returns: the error code (or no_error).
255 * Waits for a message to come in over a given socket.
257 * Every message of another type received before the one waited will be queued
258 * and used by subsequent call to this function or MsgHandle().
261 gras_msg_wait(double timeout,
262 gras_msgtype_t *msgt_want,
263 gras_socket_t **expeditor,
266 gras_msgtype_t *msgt_got;
267 gras_error_t errcode;
269 gras_procdata_t *pd=gras_procdata_get();
276 VERB1("Waiting for message %s",msgt_want->name);
278 start = now = gras_time();
280 gras_dynar_foreach(pd->msg_queue,cpt,msg){
281 if (msg.type->code == msgt_want->code) {
282 *expeditor = msg.expeditor;
283 *payload = msg.payload;
284 gras_dynar_cursor_rm(pd->msg_queue, &cpt);
285 VERB0("Waited message was queued");
291 TRY(gras_trp_select(timeout - now + start, expeditor));
292 TRY(gras_msg_recv(*expeditor, &msgt_got, payload));
293 if (msgt_got->code == msgt_want->code) {
294 VERB0("Got waited message");
298 /* not expected msg type. Queue it for later */
299 msg.expeditor = *expeditor;
301 msg.payload = *payload;
302 TRY(gras_dynar_push(pd->msg_queue,&msg));
305 if (now - start + 0.001 < timeout) {
306 RAISE1(timeout_error,"Timeout while waiting for msg %s",msgt_want->name);
315 * @timeOut: How long to wait for incoming messages
316 * @Returns: the error code (or no_error).
318 * Waits up to #timeOut# seconds to see if a message comes in; if so, calls the
319 * registered listener for that message (see RegisterCallback()).
322 gras_msg_handle(double timeOut) {
324 gras_error_t errcode;
328 gras_socket_t *expeditor;
330 gras_msgtype_t *msgtype;
332 gras_procdata_t*pd=gras_procdata_get();
338 VERB1("Handling message within the next %d",timeOut);
340 /* get a message (from the queue or from the net) */
341 if (gras_dynar_length(pd->msg_queue)) {
342 gras_dynar_shift(pd->msg_queue,&msg);
343 expeditor = msg.expeditor;
345 payload = msg.payload;
348 TRY(gras_trp_select(timeOut, &expeditor));
349 TRY(gras_msg_recv(expeditor, &msgtype, &payload));
353 gras_dynar_foreach(pd->cbl_list,cpt,list) {
354 if (list->id == msgtype->code) {
361 INFO1("Unexpected message '%s' ignored", msgtype->name);
362 WARN0("FIXME: gras_datadesc_free not implemented => leaking the payload");
366 gras_dynar_foreach(list->cbs,cpt,cb) {
367 if (cb(expeditor,msgtype->ctn_type,payload)) {
368 /* cb handled the message */
373 INFO1("Message '%s' refused by all registered callbacks", msgtype->name);
374 WARN0("FIXME: gras_datadesc_free not implemented => leaking the payload");
375 return mismatch_error;
379 gras_cb_register(gras_msgtype_t *msgtype,
381 gras_error_t errcode;
382 gras_procdata_t *pd=gras_procdata_get();
386 /* search the list of cb for this message on this host (creating if NULL) */
387 gras_dynar_foreach(pd->cbl_list,cpt,list) {
388 if (list->id == msgtype->code) {
395 /* First cb? Create room */
396 list = malloc(sizeof(gras_cblist_t));
400 list->id = msgtype->code;
401 TRY(gras_dynar_new(&(list->cbs), sizeof(gras_cb_t), NULL));
402 TRY(gras_dynar_push(pd->cbl_list,&list));
405 /* Insert the new one into the set */
406 TRY(gras_dynar_insert_at(list->cbs,0,cb));
412 gras_cb_unregister(gras_msgtype_t *msgtype,
415 gras_procdata_t *pd=gras_procdata_get();
421 /* search the list of cb for this message on this host */
422 gras_dynar_foreach(pd->cbl_list,cpt,list) {
423 if (list->id == msgtype->code) {
430 /* Remove it from the set */
432 gras_dynar_foreach(list->cbs,cpt,cb_cpt) {
434 gras_dynar_cursor_rm(list->cbs, &cpt);
440 VERB1("Ignoring removal of unexisting callback to msg id %d",