Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
merge branches
[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 "gras/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           gras_datadesc_get_name(msgtype->ctn_type),
48           (msgtype->kind == e_gras_msg_kind_rpccall ? " -> " : ""),
49           (msgtype->kind ==
50            e_gras_msg_kind_rpccall ?
51            gras_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                          gras_datadesc_type_t payload_request,
76                          gras_datadesc_type_t payload_answer)
77 {
78
79   gras_msgtype_t msgtype = NULL;
80   char *namev = make_namev(name, version);
81   volatile int found = 0;
82   xbt_ex_t e;
83
84   TRY {
85     msgtype =
86         (gras_msgtype_t) xbt_set_get_by_name(_gras_msgtype_set, namev);
87     found = 1;
88   }
89   CATCH(e) {
90     if (e.category != not_found_error) {
91       xbt_free(namev);
92       RETHROW;
93     }
94     xbt_ex_free(e);
95   }
96
97   if (found) {
98     XBT_DEBUG
99         ("Re-register version %d of message '%s' (same kind & payload, ignored).",
100          version, name);
101     xbt_assert(msgtype->kind == kind,
102                 "Message %s re-registered as a %s (it was known as a %s)",
103                 namev, e_gras_msg_kind_names[kind],
104                 e_gras_msg_kind_names[msgtype->kind]);
105     xbt_assert(!gras_datadesc_type_cmp
106                 (msgtype->ctn_type, payload_request),
107                 "Message %s re-registred with another payload (%s was %s)",
108                 namev, gras_datadesc_get_name(payload_request),
109                 gras_datadesc_get_name(msgtype->ctn_type));
110
111     xbt_assert(!gras_datadesc_type_cmp
112                 (msgtype->answer_type, payload_answer),
113                 "Message %s re-registred with another answer payload (%s was %s)",
114                 namev, gras_datadesc_get_name(payload_answer),
115                 gras_datadesc_get_name(msgtype->answer_type));
116
117     xbt_free(namev);
118     return;                     /* do really ignore it */
119   }
120
121   XBT_VERB("Register version %d of message '%s' "
122         "(payload: %s; answer payload: %s).",
123         version, name, gras_datadesc_get_name(payload_request),
124         gras_datadesc_get_name(payload_answer));
125
126   msgtype = xbt_new(s_gras_msgtype_t, 1);
127   msgtype->name = namev;
128   msgtype->name_len = strlen(namev);
129   msgtype->version = version;
130   msgtype->kind = kind;
131   msgtype->ctn_type = payload_request;
132   msgtype->answer_type = payload_answer;
133
134   xbt_set_add(_gras_msgtype_set, (xbt_set_elm_t) msgtype,
135               &gras_msgtype_free);
136 }
137
138
139 /** @brief declare a new message type of the given name. It only accepts the given datadesc as payload
140  *
141  * @param name: name as it should be used for logging messages (must be uniq)
142  * @param payload: datadescription of the payload
143  */
144 void gras_msgtype_declare(const char *name, gras_datadesc_type_t payload)
145 {
146   gras_msgtype_declare_ext(name, 0, e_gras_msg_kind_oneway, payload, NULL);
147 }
148
149
150
151 /** @brief declare a new versionned message type of the given name and payload
152  *
153  * @param name: name as it should be used for logging messages (must be uniq)
154  * @param version: something like versionning symbol
155  * @param payload: datadescription of the payload
156  *
157  * Registers a message to the GRAS mechanism. Use this version instead of
158  * gras_msgtype_declare when you change the semantic or syntax of a message and
159  * want your programs to be able to deal with both versions. Internally, each
160  * will be handled as an independent message type, so you can register
161  * differents for each of them.
162  */
163 void
164 gras_msgtype_declare_v(const char *name,
165                        short int version, gras_datadesc_type_t payload)
166 {
167
168   gras_msgtype_declare_ext(name, version,
169                            e_gras_msg_kind_oneway, payload, NULL);
170 }
171
172 /** @brief retrieve an existing message type from its name (raises an exception if it does not exist). */
173 gras_msgtype_t gras_msgtype_by_name(const char *name)
174 {
175   return gras_msgtype_by_namev(name, 0);
176 }
177
178 /** @brief retrieve an existing message type from its name (or NULL if it does not exist). */
179 gras_msgtype_t gras_msgtype_by_name_or_null(const char *name)
180 {
181   xbt_ex_t e;
182   gras_msgtype_t res = NULL;
183
184   TRY {
185     res = gras_msgtype_by_namev(name, 0);
186   }
187   CATCH(e) {
188     res = NULL;
189     xbt_ex_free(e);
190   }
191   return res;
192 }
193
194 /** @brief retrieve an existing message type from its name and version. */
195 gras_msgtype_t gras_msgtype_by_namev(const char *name, short int version)
196 {
197   gras_msgtype_t res = NULL;
198   char *namev = make_namev(name, version);
199   volatile int found = 0;
200   xbt_ex_t e;
201
202   TRY {
203     res = (gras_msgtype_t) xbt_set_get_by_name(_gras_msgtype_set, namev);
204     found = 1;
205   }
206   CATCH(e) {
207     xbt_ex_free(e);
208   }
209   if (!found)
210     THROWF(not_found_error, 0, "No registred message of that name: %s",
211            name);
212
213   free(namev);
214
215   return res;
216 }
217
218 /** @brief retrieve an existing message type from its name and version. */
219 gras_msgtype_t gras_msgtype_by_id(int id)
220 {
221   return (gras_msgtype_t) xbt_set_get_by_id(_gras_msgtype_set, id);
222 }
223
224 /* ******************************************************************** */
225 /*                         GETTERS                                      */
226 /* ******************************************************************** */
227
228 XBT_INLINE const char *gras_msgtype_get_name(gras_msgtype_t type)
229 {
230   return type->name;
231 }
232
233
234 /* ******************************************************************** */
235 /*                        CALLBACKS                                     */
236 /* ******************************************************************** */
237 void gras_cbl_free(void *data)
238 {
239   gras_cblist_t *list = *(void **) data;
240   if (list) {
241     xbt_dynar_free(&(list->cbs));
242     free(list);
243   }
244 }
245
246 /** \brief Bind the given callback to the given message type
247  *
248  * Several callbacks can be attached to a given message type.
249  * The lastly added one will get the message first, and
250  * if it returns a non-null value, the message will be passed to the
251  * second one. And so on until one of the callbacks accepts the message.
252  */
253 void gras_cb_register_(gras_msgtype_t msgtype, gras_msg_cb_t cb)
254 {
255   gras_msg_procdata_t pd =
256       (gras_msg_procdata_t) gras_libdata_by_id(gras_msg_libdata_id);
257   gras_cblist_t *list = NULL;
258   unsigned int cpt;
259
260   XBT_DEBUG("Register %p as callback to '%s'", cb, msgtype->name);
261
262   /* search the list of cb for this message on this host (creating if NULL) */
263   xbt_dynar_foreach(pd->cbl_list, cpt, list) {
264     if (list->id == msgtype->code) {
265       break;
266     } else {
267       list = NULL;
268     }
269   }
270   if (!list) {
271     /* First cb? Create room */
272     list = xbt_new(gras_cblist_t, 1);
273     list->id = msgtype->code;
274     list->cbs = xbt_dynar_new(sizeof(gras_msg_cb_t), NULL);
275     xbt_dynar_push(pd->cbl_list, &list);
276   }
277
278   /* Insert the new one into the set */
279   xbt_dynar_insert_at(list->cbs, 0, &cb);
280 }
281
282 /** \brief Unbind the given callback from the given message type */
283 void gras_cb_unregister_(gras_msgtype_t msgtype, gras_msg_cb_t cb)
284 {
285
286   gras_msg_procdata_t pd =
287       (gras_msg_procdata_t) gras_libdata_by_id(gras_msg_libdata_id);
288   gras_cblist_t *list;
289   gras_msg_cb_t cb_cpt;
290   unsigned int cpt;
291   int found = 0;
292
293   /* search the list of cb for this message on this host */
294   xbt_dynar_foreach(pd->cbl_list, cpt, list) {
295     if (list->id == msgtype->code) {
296       break;
297     } else {
298       list = NULL;
299     }
300   }
301
302   /* Remove it from the set */
303   if (list) {
304     xbt_dynar_foreach(list->cbs, cpt, cb_cpt) {
305       if (cb == cb_cpt) {
306         xbt_dynar_cursor_rm(list->cbs, &cpt);
307         found = 1;
308       }
309     }
310   }
311   if (!found)
312     XBT_VERB("Ignoring removal of unexisting callback to msg id %d",
313           msgtype->code);
314 }
315
316 /** \brief Retrieve the expeditor of the message */
317 gras_socket_t gras_msg_cb_ctx_from(gras_msg_cb_ctx_t ctx)
318 {
319   return ctx->expeditor;
320 }
321
322 /* \brief Creates a new message exchange context (user should never have to) */
323 gras_msg_cb_ctx_t gras_msg_cb_ctx_new(gras_socket_t expe,
324                                       gras_msgtype_t msgtype,
325                                       unsigned long int ID,
326                                       int answer_due, double timeout)
327 {
328   gras_msg_cb_ctx_t res = xbt_new(s_gras_msg_cb_ctx_t, 1);
329   res->expeditor = expe;
330   res->msgtype = msgtype;
331   res->ID = ID;
332   res->timeout = timeout;
333   res->answer_due = answer_due;
334
335   return res;
336 }
337
338 /* \brief Frees a message exchange context
339  *
340  * This function is mainly useful with \ref gras_msg_wait_or, ie seldom.
341  */
342 void gras_msg_cb_ctx_free(gras_msg_cb_ctx_t ctx)
343 {
344   free(ctx);
345 }