Logo AND Algorithmique Numérique Distribuée

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