Logo AND Algorithmique Numérique Distribuée

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