Logo AND Algorithmique Numérique Distribuée

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