Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
* Add a gras_msgtype_dumpall() function to get the listing of all existing
[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, 2004 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
40 /** @defgroup GRAS_msg_decl Message declaration and retrival 
41  *  @ingroup  GRAS_msg
42  *  
43  *  GRAS messages can only accept one type of payload. See \ref GRAS_dd for
44  *  more information on how to describe data in GRAS.
45  *
46  *  If you absolutely want use a message able to convey several datatypes,
47  *  you can always say that it conveys a generic reference (see
48  *  \ref gras_datadesc_ref_generic).
49  * 
50  *  In order to ease the upgrade of GRAS applications, it is possible to \e
51  *  version the messages, ie to add a version number to the message (by
52  *  default, the version is set to 0). Any messages of the wrong version will
53  *  be ignored by the applications not providing any specific callback for
54  *  them.
55  *  
56  *  This mechanism (stolen from the dynamic loader one) should ensure you to
57  *  change the semantic of a given message while still understanding the old
58  *  one.
59  */
60 /** @{ */  
61 /** \brief Opaque type */
62 typedef struct s_gras_msgtype *gras_msgtype_t;
63
64   XBT_PUBLIC void gras_msgtype_declare  (const char           *name,
65                               gras_datadesc_type_t  payload);
66   XBT_PUBLIC void gras_msgtype_declare_v(const char           *name,
67                               short int             version,
68                               gras_datadesc_type_t  payload);
69
70   XBT_PUBLIC gras_msgtype_t gras_msgtype_by_name (const char *name);
71   XBT_PUBLIC gras_msgtype_t gras_msgtype_by_name_or_null (const char *name);
72   XBT_PUBLIC gras_msgtype_t gras_msgtype_by_namev(const char *name, 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) */
96   typedef struct s_gras_msg_cb_ctx *gras_msg_cb_ctx_t;
97
98 XBT_PUBLIC gras_socket_t gras_msg_cb_ctx_from(gras_msg_cb_ctx_t ctx);
99 XBT_PUBLIC gras_msg_cb_ctx_t gras_msg_cb_ctx_new(gras_socket_t expe, 
100                                       gras_msgtype_t msgtype,
101                                       unsigned long int ID,
102                                       double timeout);
103 XBT_PUBLIC void gras_msg_cb_ctx_free(gras_msg_cb_ctx_t ctx) ;
104    
105    
106   /** \brief Type of message callback functions. 
107    *
108    * \param expeditor: a socket to contact who sent this message
109    * \param payload: the message itself
110    *
111    * \return true if the message was consumed by the callback, 
112    *  false if the message was refused by the callback (and should be 
113    *  passed to the next callback of the stack for this message)
114    *
115    * Once a such a function is registered to handle messages of a given
116    * type with \ref gras_cb_register(), it will be called each time such
117    * a message arrives (unless a gras_msg_wait() intercepts it on arrival).
118    *
119    * If the callback accepts the message, it should free it after use.
120    */
121   typedef int (*gras_msg_cb_t)(gras_msg_cb_ctx_t  ctx,
122                                void             *payload);
123
124   XBT_PUBLIC void gras_cb_register  (gras_msgtype_t msgtype, gras_msg_cb_t cb);
125   XBT_PUBLIC void gras_cb_unregister(gras_msgtype_t msgtype, gras_msg_cb_t cb);
126
127 /** @} */  
128
129 /** @defgroup GRAS_msg_exchange Message exchange 
130  *  @ingroup  GRAS_msg
131  *
132  */
133 /** @{ */
134
135
136   XBT_PUBLIC void gras_msg_send(gras_socket_t   sock,
137                      gras_msgtype_t  msgtype,
138                      void           *payload);
139   XBT_PUBLIC void gras_msg_wait(double          timeout,    
140                      gras_msgtype_t  msgt_want,
141                      gras_socket_t  *expeditor,
142                      void           *payload);
143   XBT_PUBLIC void gras_msg_handleall(double period);   
144   XBT_PUBLIC void gras_msg_handle(double timeOut);
145
146 /** @} */
147
148 /** @defgroup GRAS_msg_rpc RPC specific functions
149  *  @ingroup  GRAS_msg
150  *
151  * Remote Procedure Call (RPC) are a classical mecanism to request a service
152  * from a remote host. Using this set of functions, you let GRAS doing most of
153  * the work of sending the request, wait for an answer, make sure it is the
154  * right answer from the right host and so on.  Any exception raised on the
155  * server is also passed over the network to the client.
156  * 
157  * Callbacks are attached to RPC incomming messages the regular way using
158  * \ref gras_cb_register.
159  * 
160  * For an example of use, check the examples/gras/rpc directory of the distribution.
161  */
162 /** @{ */
163
164 /* declaration */
165 XBT_PUBLIC void gras_msgtype_declare_rpc(const char           *name,
166                               gras_datadesc_type_t  payload_request,
167                               gras_datadesc_type_t  payload_answer);
168
169 XBT_PUBLIC void gras_msgtype_declare_rpc_v(const char           *name,
170                                 short int             version,
171                                 gras_datadesc_type_t  payload_request,
172                                 gras_datadesc_type_t  payload_answer);
173
174 /* client side */
175 XBT_PUBLIC void gras_msg_rpccall(gras_socket_t server,
176                       double timeOut,
177                       gras_msgtype_t msgtype,
178                       void *request, void *answer);
179 XBT_PUBLIC gras_msg_cb_ctx_t
180 gras_msg_rpc_async_call(gras_socket_t server,
181                         double timeOut,
182                         gras_msgtype_t msgtype,
183                         void *request);
184 XBT_PUBLIC void gras_msg_rpc_async_wait(gras_msg_cb_ctx_t ctx,
185                              void *answer);
186
187 /* server side */
188 XBT_PUBLIC void gras_msg_rpcreturn(double timeOut, gras_msg_cb_ctx_t ctx,void *answer);
189
190
191 /** @} */
192
193 /** @defgroup GRAS_msg_exchangeadv Message exchange (advanced interface)
194  *  @ingroup  GRAS_msg
195  *
196  */
197 /** @{ */
198
199 /** @brief Message kind (internal enum) */
200 typedef enum {
201   e_gras_msg_kind_unknown = 0,
202
203   e_gras_msg_kind_oneway=1,    /**< good old regular messages */
204
205   e_gras_msg_kind_rpccall=2,   /**< RPC request */
206   /* HACK: e_gras_msg_kind_rpccall also designate RPC message *type* in 
207      msgtype_t, not only in msg_t*/
208   e_gras_msg_kind_rpcanswer=3, /**< RPC successful answer */
209   e_gras_msg_kind_rpcerror=4,  /**< RPC failure on server (payload=exception); should not leak to user-space */
210
211    /* future:
212        call cancel, and others
213      even after:
214        forwarding request and other application level routing stuff
215        group communication
216   */
217
218   e_gras_msg_kind_count=5 /* sentinel, dont mess with */
219 } e_gras_msg_kind_t;
220
221
222 /** @brief Message instance (internal struct) */
223 typedef struct {
224     gras_socket_t   expe;
225   e_gras_msg_kind_t kind;
226     gras_msgtype_t  type;
227     unsigned long int ID;
228     void           *payl;
229     int             payl_size;
230 } s_gras_msg_t, *gras_msg_t;
231
232 typedef int (*gras_msg_filter_t)(gras_msg_t msg,void *ctx);
233
234 XBT_PUBLIC void gras_msg_wait_ext(double           timeout,    
235                        gras_msgtype_t   msgt_want,
236                        gras_socket_t    expe_want,
237                        gras_msg_filter_t filter,
238                        void             *filter_ctx, 
239                        gras_msg_t       msg_got);
240
241 XBT_PUBLIC void gras_msg_wait_or(double         timeout,    
242                       xbt_dynar_t    msgt_want,
243                       gras_msg_cb_ctx_t *ctx,
244                       int           *msgt_got,
245                       void          *payload);
246
247
248 /* @} */
249
250 SG_END_DECL()
251
252 #endif /* GRAS_MSG_H */
253