1 /* gras message types and callback registering and retrieving */
3 /* Copyright (c) 2007, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "gras/Msg/msg_private.h"
11 #include "gras/Virtu/virtu_interface.h"
12 #include "gras/DataDesc/datadesc_interface.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_msg, gras, "High level messaging");
16 xbt_set_t _gras_msgtype_set = NULL;
19 /* ******************************************************************** */
21 /* ******************************************************************** */
25 void gras_msgtype_free(void *t)
27 gras_msgtype_t msgtype = (gras_msgtype_t) t;
35 * Dump all declared message types (debugging purpose)
37 void gras_msgtype_dumpall(void)
39 xbt_set_cursor_t cursor;
40 gras_msgtype_t msgtype = NULL;
42 INFO0("Dump of all registered messages:");
43 xbt_set_foreach(_gras_msgtype_set, cursor, msgtype) {
44 INFO6(" Message name: %s (v%d) %s; %s%s%s",
45 msgtype->name, msgtype->version,
46 e_gras_msg_kind_names[msgtype->kind],
47 gras_datadesc_get_name(msgtype->ctn_type),
48 (msgtype->kind == e_gras_msg_kind_rpccall ? " -> " : ""),
50 e_gras_msg_kind_rpccall ?
51 gras_datadesc_get_name(msgtype->answer_type) : ""));
59 * Returns the versionned name of the message.
60 * It's a newly allocated string, make sure to free it.
62 static char *make_namev(const char *name, short int ver)
65 return xbt_strdup(name);
67 return bprintf("%s_v%d", name, ver);
70 /* Internal function doing the crude work of registering messages */
72 gras_msgtype_declare_ext(const char *name,
74 e_gras_msg_kind_t kind,
75 gras_datadesc_type_t payload_request,
76 gras_datadesc_type_t payload_answer)
79 gras_msgtype_t msgtype = NULL;
80 char *namev = make_namev(name, version);
81 volatile int found = 0;
86 (gras_msgtype_t) xbt_set_get_by_name(_gras_msgtype_set, namev);
89 if (e.category != not_found_error) {
98 ("Re-register version %d of message '%s' (same kind & payload, ignored).",
100 xbt_assert3(msgtype->kind == kind,
101 "Message %s re-registered as a %s (it was known as a %s)",
102 namev, e_gras_msg_kind_names[kind],
103 e_gras_msg_kind_names[msgtype->kind]);
104 xbt_assert3(!gras_datadesc_type_cmp
105 (msgtype->ctn_type, payload_request),
106 "Message %s re-registred with another payload (%s was %s)",
107 namev, gras_datadesc_get_name(payload_request),
108 gras_datadesc_get_name(msgtype->ctn_type));
110 xbt_assert3(!gras_datadesc_type_cmp
111 (msgtype->answer_type, payload_answer),
112 "Message %s re-registred with another answer payload (%s was %s)",
113 namev, gras_datadesc_get_name(payload_answer),
114 gras_datadesc_get_name(msgtype->answer_type));
117 return; /* do really ignore it */
120 VERB4("Register version %d of message '%s' "
121 "(payload: %s; answer payload: %s).",
122 version, name, gras_datadesc_get_name(payload_request),
123 gras_datadesc_get_name(payload_answer));
125 msgtype = xbt_new(s_gras_msgtype_t, 1);
126 msgtype->name = namev;
127 msgtype->name_len = strlen(namev);
128 msgtype->version = version;
129 msgtype->kind = kind;
130 msgtype->ctn_type = payload_request;
131 msgtype->answer_type = payload_answer;
133 xbt_set_add(_gras_msgtype_set, (xbt_set_elm_t) msgtype,
138 /** @brief declare a new message type of the given name. It only accepts the given datadesc as payload
140 * @param name: name as it should be used for logging messages (must be uniq)
141 * @param payload: datadescription of the payload
143 void gras_msgtype_declare(const char *name, gras_datadesc_type_t payload)
145 gras_msgtype_declare_ext(name, 0, e_gras_msg_kind_oneway, payload, NULL);
150 /** @brief declare a new versionned message type of the given name and payload
152 * @param name: name as it should be used for logging messages (must be uniq)
153 * @param version: something like versionning symbol
154 * @param payload: datadescription of the payload
156 * Registers a message to the GRAS mechanism. Use this version instead of
157 * gras_msgtype_declare when you change the semantic or syntax of a message and
158 * want your programs to be able to deal with both versions. Internally, each
159 * will be handled as an independent message type, so you can register
160 * differents for each of them.
163 gras_msgtype_declare_v(const char *name,
164 short int version, gras_datadesc_type_t payload)
167 gras_msgtype_declare_ext(name, version,
168 e_gras_msg_kind_oneway, payload, NULL);
171 /** @brief retrieve an existing message type from its name (raises an exception if it does not exist). */
172 gras_msgtype_t gras_msgtype_by_name(const char *name)
174 return gras_msgtype_by_namev(name, 0);
177 /** @brief retrieve an existing message type from its name (or NULL if it does not exist). */
178 gras_msgtype_t gras_msgtype_by_name_or_null(const char *name)
181 gras_msgtype_t res = NULL;
184 res = gras_msgtype_by_namev(name, 0);
192 /** @brief retrieve an existing message type from its name and version. */
193 gras_msgtype_t gras_msgtype_by_namev(const char *name, short int version)
195 gras_msgtype_t res = NULL;
196 char *namev = make_namev(name, version);
197 volatile int found = 0;
201 res = (gras_msgtype_t) xbt_set_get_by_name(_gras_msgtype_set, namev);
207 THROW1(not_found_error, 0, "No registred message of that name: %s",
215 /** @brief retrieve an existing message type from its name and version. */
216 gras_msgtype_t gras_msgtype_by_id(int id)
218 return (gras_msgtype_t) xbt_set_get_by_id(_gras_msgtype_set, id);
221 /* ******************************************************************** */
223 /* ******************************************************************** */
225 XBT_INLINE const char *gras_msgtype_get_name(gras_msgtype_t type)
231 /* ******************************************************************** */
233 /* ******************************************************************** */
234 void gras_cbl_free(void *data)
236 gras_cblist_t *list = *(void **) data;
238 xbt_dynar_free(&(list->cbs));
243 /** \brief Bind the given callback to the given message type
245 * Several callbacks can be attached to a given message type.
246 * The lastly added one will get the message first, and
247 * if it returns a non-null value, the message will be passed to the
248 * second one. And so on until one of the callbacks accepts the message.
250 void gras_cb_register_(gras_msgtype_t msgtype, gras_msg_cb_t cb)
252 gras_msg_procdata_t pd =
253 (gras_msg_procdata_t) gras_libdata_by_id(gras_msg_libdata_id);
254 gras_cblist_t *list = NULL;
257 DEBUG2("Register %p as callback to '%s'", cb, msgtype->name);
259 /* search the list of cb for this message on this host (creating if NULL) */
260 xbt_dynar_foreach(pd->cbl_list, cpt, list) {
261 if (list->id == msgtype->code) {
268 /* First cb? Create room */
269 list = xbt_new(gras_cblist_t, 1);
270 list->id = msgtype->code;
271 list->cbs = xbt_dynar_new(sizeof(gras_msg_cb_t), NULL);
272 xbt_dynar_push(pd->cbl_list, &list);
275 /* Insert the new one into the set */
276 xbt_dynar_insert_at(list->cbs, 0, &cb);
279 /** \brief Unbind the given callback from the given message type */
280 void gras_cb_unregister_(gras_msgtype_t msgtype, gras_msg_cb_t cb)
283 gras_msg_procdata_t pd =
284 (gras_msg_procdata_t) gras_libdata_by_id(gras_msg_libdata_id);
286 gras_msg_cb_t cb_cpt;
290 /* search the list of cb for this message on this host */
291 xbt_dynar_foreach(pd->cbl_list, cpt, list) {
292 if (list->id == msgtype->code) {
299 /* Remove it from the set */
301 xbt_dynar_foreach(list->cbs, cpt, cb_cpt) {
303 xbt_dynar_cursor_rm(list->cbs, &cpt);
309 VERB1("Ignoring removal of unexisting callback to msg id %d",
313 /** \brief Retrieve the expeditor of the message */
314 gras_socket_t gras_msg_cb_ctx_from(gras_msg_cb_ctx_t ctx)
316 return ctx->expeditor;
319 /* \brief Creates a new message exchange context (user should never have to) */
320 gras_msg_cb_ctx_t gras_msg_cb_ctx_new(gras_socket_t expe,
321 gras_msgtype_t msgtype,
322 unsigned long int ID,
323 int answer_due, double timeout)
325 gras_msg_cb_ctx_t res = xbt_new(s_gras_msg_cb_ctx_t, 1);
326 res->expeditor = expe;
327 res->msgtype = msgtype;
329 res->timeout = timeout;
330 res->answer_due = answer_due;
335 /* \brief Frees a message exchange context
337 * This function is mainly useful with \ref gras_msg_wait_or, ie seldom.
339 void gras_msg_cb_ctx_free(gras_msg_cb_ctx_t ctx)