Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a2f5d07367c20bcbbfc6b354a05714fe6e799e76
[simgrid.git] / src / gras / Msg / gras_msg_types.c
1 /* gras message types and callback registering and retrieving               */
2
3 /* Copyright (c) 2007, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
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. */
8
9 #include "xbt/ex.h"
10 #include "gras/Msg/msg_private.h"
11 #include "gras/Virtu/virtu_interface.h"
12 #include "xbt/datadesc/datadesc_interface.h"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_msg, gras, "High level messaging");
15
16 xbt_set_t _gras_msgtype_set = NULL;
17
18
19 /* ******************************************************************** */
20 /*                      MESSAGE TYPES                                   */
21 /* ******************************************************************** */
22
23
24 /* Reclamed memory */
25 void gras_msgtype_free(void *t)
26 {
27   gras_msgtype_t msgtype = (gras_msgtype_t) t;
28   if (msgtype) {
29     free(msgtype->name);
30     free(msgtype);
31   }
32 }
33
34 /**
35  * Dump all declared message types (debugging purpose)
36  */
37 void gras_msgtype_dumpall(void)
38 {
39   xbt_set_cursor_t cursor;
40   gras_msgtype_t msgtype = NULL;
41
42   XBT_INFO("Dump of all registered messages:");
43   xbt_set_foreach(_gras_msgtype_set, cursor, msgtype) {
44     XBT_INFO("  Message name: %s (v%d) %s; %s%s%s",
45           msgtype->name, msgtype->version,
46           e_gras_msg_kind_names[msgtype->kind],
47           xbt_datadesc_get_name(msgtype->ctn_type),
48           (msgtype->kind == e_gras_msg_kind_rpccall ? " -> " : ""),
49           (msgtype->kind ==
50            e_gras_msg_kind_rpccall ?
51            xbt_datadesc_get_name(msgtype->answer_type) : ""));
52   }
53 }
54
55
56 /**
57  * make_namev:
58  *
59  * Returns the versionned name of the message.
60  *   It's a newly allocated string, make sure to free it.
61  */
62 static char *make_namev(const char *name, short int ver)
63 {
64   if (!ver)
65     return xbt_strdup(name);
66
67   return bprintf("%s_v%d", name, ver);
68 }
69
70 /* Internal function doing the crude work of registering messages */
71 void
72 gras_msgtype_declare_ext(const char *name,
73                          short int version,
74                          e_gras_msg_kind_t kind,
75                          xbt_datadesc_type_t payload_request,
76                          xbt_datadesc_type_t payload_answer)
77 {
78
79   gras_msgtype_t msgtype = NULL;
80   char *namev = make_namev(name, version);
81
82   msgtype = (gras_msgtype_t) xbt_set_get_by_name_or_null(
83       _gras_msgtype_set, (const char*) namev);
84
85   if (msgtype != NULL) {
86     XBT_DEBUG
87         ("Re-register version %d of message '%s' (same kind & payload, ignored).",
88          version, name);
89     xbt_assert(msgtype->kind == kind,
90                 "Message %s re-registered as a %s (it was known as a %s)",
91                 namev, e_gras_msg_kind_names[kind],
92                 e_gras_msg_kind_names[msgtype->kind]);
93     xbt_assert(!xbt_datadesc_type_cmp
94                 (msgtype->ctn_type, payload_request),
95                 "Message %s re-registered with another payload (%s was %s)",
96                 namev, xbt_datadesc_get_name(payload_request),
97                 xbt_datadesc_get_name(msgtype->ctn_type));
98
99     xbt_assert(!xbt_datadesc_type_cmp
100                 (msgtype->answer_type, payload_answer),
101                 "Message %s re-registered with another answer payload (%s was %s)",
102                 namev, xbt_datadesc_get_name(payload_answer),
103                 xbt_datadesc_get_name(msgtype->answer_type));
104
105     xbt_free(namev);
106     return;                     /* do really ignore it */
107   }
108
109   XBT_VERB("Register version %d of message '%s' "
110         "(payload: %s; answer payload: %s).",
111         version, name, xbt_datadesc_get_name(payload_request),
112         xbt_datadesc_get_name(payload_answer));
113
114   msgtype = xbt_new(s_gras_msgtype_t, 1);
115   msgtype->name = namev;
116   msgtype->name_len = strlen(namev);
117   msgtype->version = version;
118   msgtype->kind = kind;
119   msgtype->ctn_type = payload_request;
120   msgtype->answer_type = payload_answer;
121
122   xbt_set_add(_gras_msgtype_set, (xbt_set_elm_t) msgtype,
123               &gras_msgtype_free);
124 }
125
126
127 /** @brief declare a new message type of the given name. It only accepts the given datadesc as payload
128  *
129  * @param name: name as it should be used for logging messages (must be uniq)
130  * @param payload: datadescription of the payload
131  */
132 void gras_msgtype_declare(const char *name, xbt_datadesc_type_t payload)
133 {
134   gras_msgtype_declare_ext(name, 0, e_gras_msg_kind_oneway, payload, NULL);
135 }
136
137
138
139 /** @brief declare a new versionned message type of the given name and payload
140  *
141  * @param name: name as it should be used for logging messages (must be uniq)
142  * @param version: something like versionning symbol
143  * @param payload: datadescription of the payload
144  *
145  * Registers a message to the GRAS mechanism. Use this version instead of
146  * gras_msgtype_declare when you change the semantic or syntax of a message and
147  * want your programs to be able to deal with both versions. Internally, each
148  * will be handled as an independent message type, so you can register
149  * differents for each of them.
150  */
151 void
152 gras_msgtype_declare_v(const char *name,
153                        short int version, xbt_datadesc_type_t payload)
154 {
155
156   gras_msgtype_declare_ext(name, version,
157                            e_gras_msg_kind_oneway, payload, NULL);
158 }
159
160 /** @brief retrieve an existing message type from its name (raises an exception if it does not exist). */
161 gras_msgtype_t gras_msgtype_by_name(const char *name)
162 {
163   return gras_msgtype_by_namev(name, 0);
164 }
165
166 /** @brief retrieve an existing message type from its name (or NULL if it does not exist). */
167 gras_msgtype_t gras_msgtype_by_name_or_null(const char *name)
168 {
169   xbt_ex_t e;
170   gras_msgtype_t res = NULL;
171
172   TRY {
173     res = gras_msgtype_by_namev(name, 0);
174   }
175   CATCH(e) {
176     res = NULL;
177     xbt_ex_free(e);
178   }
179   return res;
180 }
181
182 /** @brief retrieve an existing message type from its name and version. */
183 gras_msgtype_t gras_msgtype_by_namev(const char *name, short int version)
184 {
185   gras_msgtype_t res = NULL;
186   char *namev = make_namev(name, version);
187
188   res = (gras_msgtype_t) xbt_set_get_by_name_or_null(_gras_msgtype_set, namev);
189   free(namev);
190
191   if (res == NULL)
192     THROWF(not_found_error, 0, "No registered message of that name: %s",
193            name);
194
195   return res;
196 }
197
198 /** @brief retrieve an existing message type from its name and version. */
199 gras_msgtype_t gras_msgtype_by_id(int id)
200 {
201   return (gras_msgtype_t) xbt_set_get_by_id(_gras_msgtype_set, id);
202 }
203
204 /* ******************************************************************** */
205 /*                         GETTERS                                      */
206 /* ******************************************************************** */
207
208 XBT_INLINE const char *gras_msgtype_get_name(gras_msgtype_t type)
209 {
210   return type->name;
211 }
212
213
214 /* ******************************************************************** */
215 /*                        CALLBACKS                                     */
216 /* ******************************************************************** */
217 void gras_cbl_free(void *data)
218 {
219   gras_cblist_t *list = *(void **) data;
220   if (list) {
221     xbt_dynar_free(&(list->cbs));
222     free(list);
223   }
224 }
225
226 /** \brief Bind the given callback to the given message type
227  *
228  * Several callbacks can be attached to a given message type.
229  * The lastly added one will get the message first, and
230  * if it returns a non-null value, the message will be passed to the
231  * second one. And so on until one of the callbacks accepts the message.
232  */
233 void gras_cb_register_(gras_msgtype_t msgtype, gras_msg_cb_t cb)
234 {
235   gras_msg_procdata_t pd =
236       (gras_msg_procdata_t) gras_libdata_by_id(gras_msg_libdata_id);
237   gras_cblist_t *list = NULL;
238   unsigned int cpt;
239
240   XBT_DEBUG("Register %p as callback to '%s'", cb, msgtype->name);
241
242   /* search the list of cb for this message on this host (creating if NULL) */
243   xbt_dynar_foreach(pd->cbl_list, cpt, list) {
244     if (list->id == msgtype->code) {
245       break;
246     } else {
247       list = NULL;
248     }
249   }
250   if (!list) {
251     /* First cb? Create room */
252     list = xbt_new(gras_cblist_t, 1);
253     list->id = msgtype->code;
254     list->cbs = xbt_dynar_new(sizeof(gras_msg_cb_t), NULL);
255     xbt_dynar_push(pd->cbl_list, &list);
256   }
257
258   /* Insert the new one into the set */
259   xbt_dynar_insert_at(list->cbs, 0, &cb);
260 }
261
262 /** \brief Unbind the given callback from the given message type */
263 void gras_cb_unregister_(gras_msgtype_t msgtype, gras_msg_cb_t cb)
264 {
265
266   gras_msg_procdata_t pd =
267       (gras_msg_procdata_t) gras_libdata_by_id(gras_msg_libdata_id);
268   gras_cblist_t *list;
269   gras_msg_cb_t cb_cpt;
270   unsigned int cpt;
271   int found = 0;
272
273   /* search the list of cb for this message on this host */
274   xbt_dynar_foreach(pd->cbl_list, cpt, list) {
275     if (list->id == msgtype->code) {
276       break;
277     } else {
278       list = NULL;
279     }
280   }
281
282   /* Remove it from the set */
283   if (list) {
284     xbt_dynar_foreach(list->cbs, cpt, cb_cpt) {
285       if (cb == cb_cpt) {
286         xbt_dynar_cursor_rm(list->cbs, &cpt);
287         found = 1;
288       }
289     }
290   }
291   if (!found)
292     XBT_VERB("Ignoring removal of unexisting callback to msg id %d",
293           msgtype->code);
294 }
295
296 /** \brief Retrieve the expeditor of the message */
297 xbt_socket_t gras_msg_cb_ctx_from(gras_msg_cb_ctx_t ctx)
298 {
299   return ctx->expeditor;
300 }
301
302 /* \brief Creates a new message exchange context (user should never have to) */
303 gras_msg_cb_ctx_t gras_msg_cb_ctx_new(xbt_socket_t expe,
304                                       gras_msgtype_t msgtype,
305                                       unsigned long int ID,
306                                       int answer_due, double timeout)
307 {
308   gras_msg_cb_ctx_t res = xbt_new(s_gras_msg_cb_ctx_t, 1);
309   res->expeditor = expe;
310   res->msgtype = msgtype;
311   res->ID = ID;
312   res->timeout = timeout;
313   res->answer_due = answer_due;
314
315   return res;
316 }
317
318 /* \brief Frees a message exchange context
319  *
320  * This function is mainly useful with \ref gras_msg_wait_or, ie seldom.
321  */
322 void gras_msg_cb_ctx_free(gras_msg_cb_ctx_t ctx)
323 {
324   free(ctx);
325 }