Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
no error checking were made for SIMIX_network_waitany, and cleanup were ... fragile
[simgrid.git] / include / gras / messages.h
1 /* messaging - high level communication (send/receive messages)             */
2 /* module's public interface exported to end user.                          */
3
4 /* Copyright (c) 2004, 2005, 2006, 2007, 2009, 2010. The SimGrid Team.
5  * All rights reserved.                                                     */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #ifndef GRAS_MESSAGES_H
11 #define GRAS_MESSAGES_H
12
13 #include "xbt/misc.h"
14 #include "gras/transport.h"
15 #include "gras/datadesc.h"
16
17 SG_BEGIN_DECL()
18
19 /** @addtogroup GRAS_msg
20  *  @brief Defining messages and callbacks, and exchanging messages
21  * 
22  *  There is two way to receive messages in GRAS. The first one is to
23  *  register a given function as callback to a given type of messages (see
24  *  \ref gras_cb_register and associated section). But you can also
25  *  explicitely wait for a given message with the \ref gras_msg_wait
26  *  function.
27  * 
28  *  Usually, both ways are not intended to be mixed of a given type of
29  *  messages. But if you do so, it shouldn't trigger any issue.  If the
30  *  message arrives when gras_msg_wait is blocked, then it will be routed to
31  *  it. If it arrives when before or after \ref gras_msg_wait, it will be
32  *  passed to the callback.
33  * 
34  *  For an example of use, please refer to \ref GRAS_ex_ping. The archive
35  *  contains much more examples, but their are not properly integrated into
36  *  this documentation yet. 
37  */
38 /** @defgroup GRAS_msg_decl Message declaration and retrival 
39  *  @ingroup  GRAS_msg
40  *  
41  *  GRAS messages can only accept one type of payload. See \ref GRAS_dd for
42  *  more information on how to describe data in GRAS.
43  *
44  *  If you absolutely want use a message able to convey several datatypes,
45  *  you can always say that it conveys a generic reference (see
46  *  \ref gras_datadesc_ref_generic).
47  * 
48  *  In order to ease the upgrade of GRAS applications, it is possible to \e
49  *  version the messages, ie to add a version number to the message (by
50  *  default, the version is set to 0). Any messages of the wrong version will
51  *  be ignored by the applications not providing any specific callback for
52  *  them.
53  *  
54  *  This mechanism (stolen from the dynamic loader one) should ensure you to
55  *  change the semantic of a given message while still understanding the old
56  *  one.
57  */
58 /** @{ */
59 /** \brief Opaque type */
60      typedef struct s_gras_msgtype *gras_msgtype_t;
61
62 XBT_PUBLIC(void) gras_msgtype_declare(const char *name,
63                                       gras_datadesc_type_t payload);
64 XBT_PUBLIC(void) gras_msgtype_declare_v(const char *name,
65                                         short int version,
66                                         gras_datadesc_type_t payload);
67
68 XBT_PUBLIC(gras_msgtype_t) gras_msgtype_by_name(const char *name);
69 XBT_PUBLIC(gras_msgtype_t) gras_msgtype_by_name_or_null(const char *name);
70 XBT_PUBLIC(gras_msgtype_t) gras_msgtype_by_namev(const char *name,
71                                                  short int version);
72 XBT_PUBLIC(gras_msgtype_t) gras_msgtype_by_id(int id);
73
74 XBT_PUBLIC(void) gras_msgtype_dumpall(void);
75
76
77 /** @} */
78 /** @defgroup GRAS_msg_cb Callback declaration and use
79  *  @ingroup  GRAS_msg
80  * 
81  *
82  * This is how to register a given function so that it gets called when a
83  * given type of message arrives.
84  * 
85  * You can register several callbacks to the same kind of messages, and
86  * they will get stacked. The lastly added callback gets the message first.
87  * If it consumes the message, it should return a true value when done. If
88  * not, it should return 0, and the message will be passed to the second
89  * callback of the stack, if any.
90  * 
91  * @{
92  */
93
94   /** \brief Context of callbacks (opaque structure, created by the middleware only, never by user) */
95      typedef struct s_gras_msg_cb_ctx *gras_msg_cb_ctx_t;
96
97 XBT_PUBLIC(void) gras_msg_cb_ctx_free(gras_msg_cb_ctx_t ctx);
98 XBT_PUBLIC(gras_socket_t) gras_msg_cb_ctx_from(gras_msg_cb_ctx_t ctx);
99
100   /** \brief Type of message callback functions. 
101    *
102    * \param expeditor: a socket to contact who sent this message
103    * \param payload: the message itself
104    *
105    * \return true if the message was consumed by the callback, 
106    *  false if the message was refused by the callback (and should be 
107    *  passed to the next callback of the stack for this message)
108    *
109    * Once a such a function is registered to handle messages of a given
110    * type with \ref gras_cb_register(), it will be called each time such
111    * a message arrives (unless a gras_msg_wait() intercepts it on arrival).
112    *
113    * If the callback accepts the message, it should free it after use.
114    */
115      typedef int (*gras_msg_cb_t) (gras_msg_cb_ctx_t ctx, void *payload);
116
117  /**
118   * @brief Bind the given callback to the given message type (described by its name)
119   * @hideinitializer
120   * 
121   * Several callbacks can be attached to a given message type. The lastly added one will get the message first, and
122   * if it returns a non-null value, the message will be passed to the second one.
123   * And so on until one of the callbacks accepts the message.
124   * 
125   * Using gras_cb_register is a bit slower than using gras_cb_register_ since GRAS
126   * has to search for the given msgtype in the hash table, but you don't care in most case.
127   */
128 #define gras_cb_register(msgtype_name, cb)   gras_cb_register_(gras_msgtype_by_name(msgtype_name),cb)
129
130  /**
131   * @brief Unbind the given callback to the given message type (described by its name)
132   * @hideinitializer
133   * 
134   * Using gras_cb_unregister is a bit slower than using gras_cb_unregister_ since GRAS
135   * has to search for the given msgtype in the hash table, but you don't care in most case.
136   */
137 #define gras_cb_unregister(msgtype_name, cb) gras_cb_unregister_(gras_msgtype_by_name(msgtype_name),cb)
138
139 XBT_PUBLIC(void) gras_cb_register_(gras_msgtype_t msgtype, gras_msg_cb_t cb);
140 XBT_PUBLIC(void) gras_cb_unregister_(gras_msgtype_t msgtype,
141                                      gras_msg_cb_t cb);
142
143 /** @} */
144
145 /** @defgroup GRAS_msg_exchange Message exchange 
146  *  @ingroup  GRAS_msg
147  *
148  */
149 /** @{ */
150
151 /** \brief Send the data pointed by \a payload as a message \a msgname on the \a sock
152  *  @hideinitializer
153  *
154  * Using gras_msg_wait() is a bit slower than using gras_msg_wait_() since GRAS
155  * has to search for the given msgtype in the hash table.
156  */
157 #define gras_msg_send(sock,name,payload) gras_msg_send_(sock,gras_msgtype_by_name(name),payload)
158 XBT_PUBLIC(void) gras_msg_send_(gras_socket_t sock,
159                                 gras_msgtype_t msgtype, void *payload);
160
161 /** \brief Waits for a message to come in over a given socket
162  *  @hideinitializer
163  * @param timeout: How long should we wait for this message.
164  * @param msgt_want: type of awaited msg
165  * @param[out] expeditor: where to create a socket to answer the incomming message
166  * @param[out] payload: where to write the payload of the incomming message
167  * @return the error code (or no_error).
168  *
169  * Every message of another type received before the one waited will be queued
170  * and used by subsequent call to this function or gras_msg_handle().
171  *
172  * Using gras_msg_wait() is a bit slower than using gras_msg_wait_() since GRAS
173  * has to search for the given msgtype in the hash table.
174  */
175
176 #define gras_msg_wait(timeout,msgt_want,expeditor,payload) gras_msg_wait_(timeout,gras_msgtype_by_name(msgt_want),expeditor,payload)
177 XBT_PUBLIC(void) gras_msg_wait_(double timeout,
178                                 gras_msgtype_t msgt_want,
179                                 gras_socket_t * expeditor, void *payload);
180 XBT_PUBLIC(void) gras_msg_handleall(double period);
181 XBT_PUBLIC(void) gras_msg_handle(double timeOut);
182
183 /** @} */
184
185 /** @defgroup GRAS_msg_rpc RPC specific functions
186  *  @ingroup  GRAS_msg
187  *
188  * Remote Procedure Call (RPC) are a classical mecanism to request a service
189  * from a remote host. Using this set of functions, you let GRAS doing most of
190  * the work of sending the request, wait for an answer, make sure it is the
191  * right answer from the right host and so on.  Any exception raised on the
192  * server is also passed over the network to the client.
193  * 
194  * Callbacks are attached to RPC incomming messages the regular way using
195  * \ref gras_cb_register.
196  * 
197  * For an example of use, check the examples/gras/rpc directory of the distribution.
198  */
199 /** @{ */
200
201 /* declaration */
202 XBT_PUBLIC(void) gras_msgtype_declare_rpc(const char *name,
203                                           gras_datadesc_type_t
204                                           payload_request,
205                                           gras_datadesc_type_t
206                                           payload_answer);
207
208 XBT_PUBLIC(void) gras_msgtype_declare_rpc_v(const char *name,
209                                             short int version,
210                                             gras_datadesc_type_t
211                                             payload_request,
212                                             gras_datadesc_type_t
213                                             payload_answer);
214
215 /* client side */
216
217 /** @brief Conduct a RPC call
218  *  @hideinitializer
219  */
220 #define gras_msg_rpccall(server,timeout,msg,req,ans) gras_msg_rpccall_(server,timeout,gras_msgtype_by_name(msg),req,ans)
221 XBT_PUBLIC(void) gras_msg_rpccall_(gras_socket_t server,
222                                    double timeOut,
223                                    gras_msgtype_t msgtype,
224                                    void *request, void *answer);
225 XBT_PUBLIC(gras_msg_cb_ctx_t)
226
227 /** @brief Launch a RPC call, but do not block for the answer
228  *  @hideinitializer
229  */
230 #define gras_msg_rpc_async_call(server,timeout,msg,req) gras_msg_rpc_async_call_(server,timeout,gras_msgtype_by_name(msg),req)
231   gras_msg_rpc_async_call_(gras_socket_t server,
232                          double timeOut,
233                          gras_msgtype_t msgtype, void *request);
234 XBT_PUBLIC(void) gras_msg_rpc_async_wait(gras_msg_cb_ctx_t ctx, void *answer);
235
236 /* server side */
237 XBT_PUBLIC(void) gras_msg_rpcreturn(double timeOut, gras_msg_cb_ctx_t ctx,
238                                     void *answer);
239
240
241 /** @} */
242
243 /** @defgroup GRAS_msg_exchangeadv Message exchange (advanced interface)
244  *  @ingroup  GRAS_msg
245  *
246  */
247 /** @{ */
248
249 /** @brief Message kind (internal enum) */
250      typedef enum {
251        e_gras_msg_kind_unknown = 0,
252
253        e_gras_msg_kind_oneway = 1,
254                                /**< good old regular messages */
255
256        e_gras_msg_kind_rpccall = 2,
257                                /**< RPC request */
258        /* HACK: e_gras_msg_kind_rpccall also designate RPC message *type* in 
259           msgtype_t, not only in msg_t */
260        e_gras_msg_kind_rpcanswer = 3,
261                                /**< RPC successful answer */
262        e_gras_msg_kind_rpcerror = 4,
263                                /**< RPC failure on server (payload=exception); should not leak to user-space */
264
265        /* future:
266           call cancel, and others
267           even after:
268           forwarding request and other application level routing stuff
269           group communication
270         */
271
272        e_gras_msg_kind_count = 5        /* sentinel, dont mess with */
273      } e_gras_msg_kind_t;
274
275
276 /** @brief Message instance (internal struct) */
277      typedef struct {
278        gras_socket_t expe;
279        e_gras_msg_kind_t kind;
280        gras_msgtype_t type;
281        unsigned long int ID;
282        void *payl;
283        int payl_size;
284      } s_gras_msg_t, *gras_msg_t;
285
286      typedef int (*gras_msg_filter_t) (gras_msg_t msg, void *ctx);
287
288 #define gras_msg_wait_ext(timeout, msg, expe, filter, fctx,got) gras_msg_wait_ext_(timeout, gras_msgtype_by_name(msg), expe, filter, fctx,got)
289 XBT_PUBLIC(void) gras_msg_wait_ext_(double timeout,
290                                     gras_msgtype_t msgt_want,
291                                     gras_socket_t expe_want,
292                                     gras_msg_filter_t filter,
293                                     void *filter_ctx, gras_msg_t msg_got);
294
295 XBT_PUBLIC(void) gras_msg_wait_or(double timeout,
296                                   xbt_dynar_t msgt_want,
297                                   gras_msg_cb_ctx_t * ctx,
298                                   int *msgt_got, void *payload);
299
300
301 /* @} */
302
303 SG_END_DECL()
304 #endif /* GRAS_MSG_H */