3 /* gras message types and callback registering and retrieving */
5 /* Copyright (c) 2003, 2004, 2005, 2006, 2007 Martin Quinson. */
6 /* All rights reserved. */
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. */
12 #include "gras/Msg/msg_private.h"
13 #include "gras/Virtu/virtu_interface.h"
14 #include "gras/DataDesc/datadesc_interface.h"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_msg,gras,"High level messaging");
18 xbt_set_t _gras_msgtype_set = NULL;
21 /* ******************************************************************** */
23 /* ******************************************************************** */
27 void gras_msgtype_free(void *t) {
28 gras_msgtype_t msgtype=(gras_msgtype_t)t;
35 * Dump all declared message types (debugging purpose)
37 void gras_msgtype_dumpall(void) {
38 xbt_set_cursor_t cursor;
39 gras_msgtype_t msgtype=NULL;
41 INFO0("Dump of all registered messages:");
42 xbt_set_foreach(_gras_msgtype_set, cursor, msgtype) {
43 INFO6(" Message name: %s (v%d) %s; %s%s%s",
44 msgtype->name, msgtype->version, e_gras_msg_kind_names[msgtype->kind],
45 gras_datadesc_get_name(msgtype->ctn_type),
46 (msgtype->kind==e_gras_msg_kind_rpccall ? " -> ":""),
47 (msgtype->kind==e_gras_msg_kind_rpccall ? gras_datadesc_get_name(msgtype->answer_type) : ""));
55 * Returns the versionned name of the message.
56 * It's a newly allocated string, make sure to free it.
58 static char *make_namev(const char *name, short int ver) {
60 return xbt_strdup(name);
62 return bprintf("%s_v%d",name,ver);
65 /* Internal function doing the crude work of registering messages */
67 gras_msgtype_declare_ext(const char *name,
69 e_gras_msg_kind_t kind,
70 gras_datadesc_type_t payload_request,
71 gras_datadesc_type_t payload_answer) {
73 gras_msgtype_t msgtype=NULL;
74 char *namev=make_namev(name,version);
75 volatile int found = 0;
79 msgtype = (gras_msgtype_t)xbt_set_get_by_name(_gras_msgtype_set,namev);
82 if (e.category != not_found_error)
88 VERB2("Re-register version %d of message '%s' (same kind & payload, ignored).",
90 xbt_assert3(msgtype->kind == kind,
91 "Message %s re-registered as a %s (it was known as a %s)",
92 namev,e_gras_msg_kind_names[kind],e_gras_msg_kind_names[msgtype->kind]);
93 xbt_assert3(!gras_datadesc_type_cmp(msgtype->ctn_type, payload_request),
94 "Message %s re-registred with another payload (%s was %s)",
95 namev,gras_datadesc_get_name(payload_request),
96 gras_datadesc_get_name(msgtype->ctn_type));
98 xbt_assert3(!gras_datadesc_type_cmp(msgtype->answer_type, payload_answer),
99 "Message %s re-registred with another answer payload (%s was %s)",
100 namev,gras_datadesc_get_name(payload_answer),
101 gras_datadesc_get_name(msgtype->answer_type));
103 return ; /* do really ignore it */
106 VERB4("Register version %d of message '%s' "
107 "(payload: %s; answer payload: %s).",
108 version, name, gras_datadesc_get_name(payload_request),
109 gras_datadesc_get_name(payload_answer));
111 msgtype = xbt_new(s_gras_msgtype_t,1);
112 msgtype->name = namev;
113 msgtype->name_len = strlen(namev);
114 msgtype->version = version;
115 msgtype->kind = kind;
116 msgtype->ctn_type = payload_request;
117 msgtype->answer_type = payload_answer;
119 xbt_set_add(_gras_msgtype_set, (xbt_set_elm_t)msgtype,
124 /** @brief declare a new message type of the given name. It only accepts the given datadesc as payload
126 * @param name: name as it should be used for logging messages (must be uniq)
127 * @param payload: datadescription of the payload
129 void gras_msgtype_declare(const char *name,
130 gras_datadesc_type_t payload) {
131 gras_msgtype_declare_ext(name, 0, e_gras_msg_kind_oneway, payload, NULL);
136 /** @brief declare a new versionned message type of the given name and payload
138 * @param name: name as it should be used for logging messages (must be uniq)
139 * @param version: something like versionning symbol
140 * @param payload: datadescription of the payload
142 * Registers a message to the GRAS mechanism. Use this version instead of
143 * gras_msgtype_declare when you change the semantic or syntax of a message and
144 * want your programs to be able to deal with both versions. Internally, each
145 * will be handled as an independent message type, so you can register
146 * differents for each of them.
149 gras_msgtype_declare_v(const char *name,
151 gras_datadesc_type_t payload) {
153 gras_msgtype_declare_ext(name, version,
154 e_gras_msg_kind_oneway, payload, NULL);
157 /** @brief retrieve an existing message type from its name (raises an exception if it does not exist). */
158 gras_msgtype_t gras_msgtype_by_name (const char *name) {
159 return gras_msgtype_by_namev(name,0);
161 /** @brief retrieve an existing message type from its name (or NULL if it does not exist). */
162 gras_msgtype_t gras_msgtype_by_name_or_null (const char *name) {
164 gras_msgtype_t res = NULL;
167 res = gras_msgtype_by_namev(name,0);
175 /** @brief retrieve an existing message type from its name and version. */
176 gras_msgtype_t gras_msgtype_by_namev(const char *name,
178 gras_msgtype_t res = NULL;
179 char *namev = make_namev(name,version);
180 volatile int found=0;
184 res = (gras_msgtype_t)xbt_set_get_by_name(_gras_msgtype_set, namev);
190 THROW1(not_found_error,0,"No registred message of that name: %s",name);
196 /** @brief retrieve an existing message type from its name and version. */
197 gras_msgtype_t gras_msgtype_by_id(int id) {
198 return (gras_msgtype_t)xbt_set_get_by_id(_gras_msgtype_set, id);
203 /* ******************************************************************** */
205 /* ******************************************************************** */
207 gras_cbl_free(void *data){
208 gras_cblist_t *list=*(void**)data;
210 xbt_dynar_free(&( list->cbs ));
215 /** \brief Bind the given callback to the given message type
217 * Several callbacks can be attached to a given message type.
218 * The lastly added one will get the message first, and
219 * if it returns a non-null value, the message will be passed to the
220 * second one. And so on until one of the callbacks accepts the message.
223 gras_cb_register_(gras_msgtype_t msgtype,
225 gras_msg_procdata_t pd=
226 (gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id);
227 gras_cblist_t *list=NULL;
230 DEBUG2("Register %p as callback to '%s'",cb,msgtype->name);
232 /* search the list of cb for this message on this host (creating if NULL) */
233 xbt_dynar_foreach(pd->cbl_list,cpt,list) {
234 if (list->id == msgtype->code) {
241 /* First cb? Create room */
242 list = xbt_new(gras_cblist_t,1);
243 list->id = msgtype->code;
244 list->cbs = xbt_dynar_new(sizeof(gras_msg_cb_t), NULL);
245 xbt_dynar_push(pd->cbl_list,&list);
248 /* Insert the new one into the set */
249 xbt_dynar_insert_at(list->cbs,0,&cb);
252 /** \brief Unbind the given callback from the given message type */
254 gras_cb_unregister_(gras_msgtype_t msgtype,
257 gras_msg_procdata_t pd=
258 (gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id);
260 gras_msg_cb_t cb_cpt;
264 /* search the list of cb for this message on this host */
265 xbt_dynar_foreach(pd->cbl_list,cpt,list) {
266 if (list->id == msgtype->code) {
273 /* Remove it from the set */
275 xbt_dynar_foreach(list->cbs,cpt,cb_cpt) {
277 xbt_dynar_cursor_rm(list->cbs, &cpt);
283 VERB1("Ignoring removal of unexisting callback to msg id %d",
287 /** \brief Retrieve the expeditor of the message */
288 gras_socket_t gras_msg_cb_ctx_from(gras_msg_cb_ctx_t ctx) {
289 return ctx->expeditor;
291 /* \brief Creates a new message exchange context (user should never have to) */
292 gras_msg_cb_ctx_t gras_msg_cb_ctx_new(gras_socket_t expe,
293 gras_msgtype_t msgtype,
294 unsigned long int ID,
297 gras_msg_cb_ctx_t res=xbt_new(s_gras_msg_cb_ctx_t,1);
298 res->expeditor = expe;
299 res->msgtype = msgtype;
301 res->timeout = timeout;
302 res->answer_due = answer_due;
306 /* \brief Frees a message exchange context
308 * This function is mainly useful with \ref gras_msg_wait_or, ie seldom.
310 void gras_msg_cb_ctx_free(gras_msg_cb_ctx_t ctx) {