Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do not push newly created sockets into the dynar before they get initialized, or...
[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   gras_msgtype_t msgtype=(gras_msgtype_t)t;
29   if (msgtype) {
30     free(msgtype->name);
31     free(msgtype);
32   }
33 }
34 /**
35  * Dump all declared message types (debugging purpose)
36  */
37 void gras_msgtype_dumpall(void) {   
38   xbt_set_cursor_t cursor;
39   gras_msgtype_t msgtype=NULL;
40    
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) : ""));
48   }   
49 }
50
51
52 /**
53  * make_namev:
54  *
55  * Returns the versionned name of the message. 
56  *   It's a newly allocated string, make sure to free it.
57  */
58 static char *make_namev(const char *name, short int ver) {
59   if (!ver)
60     return xbt_strdup(name);
61
62   return bprintf("%s_v%d",name,ver);
63 }
64
65 /* Internal function doing the crude work of registering messages */
66 void 
67 gras_msgtype_declare_ext(const char           *name,
68                          short int             version,
69                          e_gras_msg_kind_t     kind, 
70                          gras_datadesc_type_t  payload_request,
71                          gras_datadesc_type_t  payload_answer) {
72
73   gras_msgtype_t msgtype=NULL;
74   char *namev=make_namev(name,version);
75   volatile int found = 0;
76   xbt_ex_t e;    
77   
78   TRY {
79     msgtype = (gras_msgtype_t)xbt_set_get_by_name(_gras_msgtype_set,namev);
80     found = 1;
81   } CATCH(e) {
82     if (e.category != not_found_error) {
83       xbt_free(namev);
84       RETHROW;
85     }
86     xbt_ex_free(e);
87   }
88
89   if (found) {
90     VERB2("Re-register version %d of message '%s' (same kind & payload, ignored).",
91           version, name);
92     xbt_assert3(msgtype->kind == kind,
93                 "Message %s re-registered as a %s (it was known as a %s)",
94                 namev,e_gras_msg_kind_names[kind],e_gras_msg_kind_names[msgtype->kind]);
95     xbt_assert3(!gras_datadesc_type_cmp(msgtype->ctn_type, payload_request),
96                  "Message %s re-registred with another payload (%s was %s)",
97                  namev,gras_datadesc_get_name(payload_request),
98                  gras_datadesc_get_name(msgtype->ctn_type));
99
100     xbt_assert3(!gras_datadesc_type_cmp(msgtype->answer_type, payload_answer),
101              "Message %s re-registred with another answer payload (%s was %s)",
102                  namev,gras_datadesc_get_name(payload_answer),
103                  gras_datadesc_get_name(msgtype->answer_type));
104
105     xbt_free(namev);
106     return ; /* do really ignore it */
107   }
108
109   VERB4("Register version %d of message '%s' "
110         "(payload: %s; answer payload: %s).", 
111         version, name, gras_datadesc_get_name(payload_request),
112         gras_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,
133                           gras_datadesc_type_t  payload) {
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,
154                        gras_datadesc_type_t  payload) {
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   return gras_msgtype_by_namev(name,0);
163 }
164 /** @brief retrieve an existing message type from its name (or NULL if it does not exist). */
165 gras_msgtype_t gras_msgtype_by_name_or_null (const char *name) {
166   xbt_ex_t e;
167   gras_msgtype_t res = NULL;
168    
169   TRY {
170      res = gras_msgtype_by_namev(name,0);
171   } CATCH(e) {
172      res = NULL;
173      xbt_ex_free(e);
174   }
175   return res;
176 }
177
178 /** @brief retrieve an existing message type from its name and version. */
179 gras_msgtype_t gras_msgtype_by_namev(const char      *name,
180                                      short int        version) {
181   gras_msgtype_t res = NULL;
182   char *namev = make_namev(name,version);
183   volatile int found=0;
184   xbt_ex_t e;
185
186   TRY {
187     res = (gras_msgtype_t)xbt_set_get_by_name(_gras_msgtype_set, namev);
188     found=1;
189   } CATCH(e) {
190     xbt_ex_free(e);
191   }
192   if (!found)
193     THROW1(not_found_error,0,"No registred message of that name: %s",name);
194
195   free(namev);
196   
197   return res;
198 }
199 /** @brief retrieve an existing message type from its name and version. */
200 gras_msgtype_t gras_msgtype_by_id(int id) {
201   return (gras_msgtype_t)xbt_set_get_by_id(_gras_msgtype_set, id);
202 }
203
204
205
206 /* ******************************************************************** */
207 /*                        CALLBACKS                                     */
208 /* ******************************************************************** */
209 void
210 gras_cbl_free(void *data){
211   gras_cblist_t *list=*(void**)data;
212   if (list) {
213     xbt_dynar_free(&( list->cbs ));
214     free(list);
215   }
216 }
217
218 /** \brief Bind the given callback to the given message type 
219  *
220  * Several callbacks can be attached to a given message type. 
221  * The lastly added one will get the message first, and 
222  * if it returns a non-null value, the message will be passed to the
223  * second one. And so on until one of the callbacks accepts the message.
224  */
225 void
226 gras_cb_register_(gras_msgtype_t msgtype,
227                   gras_msg_cb_t cb) {
228   gras_msg_procdata_t pd=
229     (gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id);
230   gras_cblist_t *list=NULL;
231   int cpt;
232
233   DEBUG2("Register %p as callback to '%s'",cb,msgtype->name);
234
235   /* search the list of cb for this message on this host (creating if NULL) */
236   xbt_dynar_foreach(pd->cbl_list,cpt,list) {
237     if (list->id == msgtype->code) {
238       break;
239     } else {
240       list=NULL;
241     }
242   }
243   if (!list) {
244     /* First cb? Create room */
245     list = xbt_new(gras_cblist_t,1);
246     list->id = msgtype->code;
247     list->cbs = xbt_dynar_new(sizeof(gras_msg_cb_t), NULL);
248     xbt_dynar_push(pd->cbl_list,&list);
249   }
250
251   /* Insert the new one into the set */
252   xbt_dynar_insert_at(list->cbs,0,&cb);
253 }
254
255 /** \brief Unbind the given callback from the given message type */
256 void
257 gras_cb_unregister_(gras_msgtype_t msgtype,
258                     gras_msg_cb_t cb) {
259
260   gras_msg_procdata_t pd=
261     (gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id);
262   gras_cblist_t *list;
263   gras_msg_cb_t cb_cpt;
264   int cpt;
265   int found = 0;
266
267   /* search the list of cb for this message on this host */
268   xbt_dynar_foreach(pd->cbl_list,cpt,list) {
269     if (list->id == msgtype->code) {
270       break;
271     } else {
272       list=NULL;
273     }
274   }
275
276   /* Remove it from the set */
277   if (list) {
278     xbt_dynar_foreach(list->cbs,cpt,cb_cpt) {
279       if (cb == cb_cpt) {
280         xbt_dynar_cursor_rm(list->cbs, &cpt);
281         found = 1;
282       }
283     }
284   }
285   if (!found)
286     VERB1("Ignoring removal of unexisting callback to msg id %d",
287           msgtype->code);
288 }
289
290 /** \brief Retrieve the expeditor of the message */
291 gras_socket_t gras_msg_cb_ctx_from(gras_msg_cb_ctx_t ctx) {
292   return ctx->expeditor;
293 }
294 /* \brief Creates a new message exchange context (user should never have to) */
295 gras_msg_cb_ctx_t gras_msg_cb_ctx_new(gras_socket_t expe, 
296                                       gras_msgtype_t msgtype,
297                                       unsigned long int ID,
298                                       int answer_due,
299                                       double timeout) {
300   gras_msg_cb_ctx_t res=xbt_new(s_gras_msg_cb_ctx_t,1);
301   res->expeditor = expe;
302   res->msgtype = msgtype;
303   res->ID = ID;
304   res->timeout = timeout;
305   res->answer_due = answer_due;
306
307   return res;
308 }
309 /* \brief Frees a message exchange context 
310  *
311  * This function is mainly useful with \ref gras_msg_wait_or, ie seldom.
312  */
313 void gras_msg_cb_ctx_free(gras_msg_cb_ctx_t ctx) {
314   free(ctx);
315 }