Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
293de522bfd1a1382d6adac350aa92e79f6c5404
[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.
36  */
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   void gras_msgtype_declare  (const char           *name,
63                               gras_datadesc_type_t  payload);
64   void gras_msgtype_declare_v(const char           *name,
65                               short int             version,
66                               gras_datadesc_type_t  payload);
67
68   gras_msgtype_t gras_msgtype_by_name (const char *name);
69   gras_msgtype_t gras_msgtype_by_namev(const char *name, short int version);
70   gras_msgtype_t gras_msgtype_by_id(int id);
71
72 /** @} */  
73 /** @defgroup GRAS_msg_rpcdecl RPC declaration
74  *  @ingroup  GRAS_msg
75  *
76  * Remote Procedure Call (RPC) are a classical mecanism to request a service
77  * from a remote host. Using this set of functions, you let GRAS doing most of
78  * the work of sending the request, wait for an answer, make sure it is the
79  * right answer from the right host and so on.  Any exception raised on the
80  * server is also passed over the network to the client.
81  */
82 /** @{ */
83
84 void gras_msgtype_declare_rpc(const char           *name,
85                               gras_datadesc_type_t  payload_request,
86                               gras_datadesc_type_t  payload_answer);
87
88 void gras_msgtype_declare_rpc_v(const char           *name,
89                                 short int             version,
90                                 gras_datadesc_type_t  payload_request,
91                                 gras_datadesc_type_t  payload_answer);
92
93
94 /** @} */  
95 /** @defgroup GRAS_msg_cb Callback declaration and use
96  *  @ingroup  GRAS_msg
97  * 
98  *
99  * This is how to register a given function so that it gets called when a
100  * given type of message arrives.
101  * 
102  * You can register several callbacks to the same kind of messages, and
103  * they will get stacked. The lastly added callback gets the message first.
104  * If it consumes the message, it should return a true value when done. If
105  * not, it should return 0, and the message will be passed to the second
106  * callback of the stack, if any.
107  * 
108  * @{
109  */
110  
111   /** \brief Context of callbacks (opaque structure) */
112   typedef struct s_gras_msg_cb_ctx *gras_msg_cb_ctx_t;
113
114 gras_socket_t gras_msg_cb_ctx_from(gras_msg_cb_ctx_t ctx);
115
116   /** \brief Type of message callback functions. 
117    *
118    * \param expeditor: a socket to contact who sent this message
119    * \param payload: the message itself
120    *
121    * \return true if the message was consumed by the callback, 
122    *  false if the message was refused by the callback (and should be 
123    *  passed to the next callback of the stack for this message)
124    *
125    * Once a such a function is registered to handle messages of a given
126    * type with \ref gras_cb_register(), it will be called each time such
127    * a message arrives (unless a gras_msg_wait() intercepts it on arrival).
128    *
129    * If the callback accepts the message, it should free it after use.
130    */
131   typedef int (*gras_msg_cb_t)(gras_msg_cb_ctx_t  ctx,
132                                void             *payload);
133
134   void gras_cb_register  (gras_msgtype_t msgtype, gras_msg_cb_t cb);
135   void gras_cb_unregister(gras_msgtype_t msgtype, gras_msg_cb_t cb);
136
137 /** @} */  
138
139 /** @defgroup GRAS_msg_rpc RPC specific functions
140  *  @ingroup  GRAS_msg
141  */
142 /** @{ */
143
144 /* client side */
145 void gras_msg_rpccall(gras_socket_t server,
146                       double timeOut,
147                       gras_msgtype_t msgtype,
148                       void *request, void *answer);
149
150 /* server side */
151 void gras_msg_rpcreturn(double timeOut, gras_msg_cb_ctx_t ctx,void *answer);
152
153
154 /** @} */
155
156 /** @defgroup GRAS_msg_exchange Message exchange 
157  *  @ingroup  GRAS_msg
158  *
159  */
160 /** @{ */
161
162
163   void gras_msg_send(gras_socket_t   sock,
164                      gras_msgtype_t  msgtype,
165                      void           *payload);
166   void gras_msg_wait(double          timeout,    
167                      gras_msgtype_t  msgt_want,
168                      gras_socket_t  *expeditor,
169                      void           *payload);
170   void gras_msg_handle(double timeOut);
171
172 /** @} */
173 /** @defgroup GRAS_msg_exchangeadv Message exchange (advanced interface)
174  *  @ingroup  GRAS_msg
175  *
176  */
177 /** @{ */
178
179 /** @brief Message kind (internal enum) */
180 typedef enum {
181   e_gras_msg_kind_unknown = 0,
182
183   e_gras_msg_kind_oneway=1,    /**< good old regular messages */
184
185   e_gras_msg_kind_rpccall=2,   /**< RPC request */
186   /* HACK: e_gras_msg_kind_rpccall also designate RPC message *type* in 
187      msgtype_t, not only in msg_t*/
188   e_gras_msg_kind_rpcanswer=3, /**< RPC successful answer */
189   e_gras_msg_kind_rpcerror=4,  /**< RPC failure on server (payload=exception); should not leak to user-space */
190
191    /* future:
192        call cancel, and others
193      even after:
194        forwarding request and other application level routing stuff
195        group communication
196   */
197
198   e_gras_msg_kind_count=5 /* sentinel, dont mess with */
199 } e_gras_msg_kind_t;
200
201
202 /** @brief Message instance (internal struct) */
203 typedef struct {
204     gras_socket_t   expe;
205   e_gras_msg_kind_t kind;
206     gras_msgtype_t  type;
207     unsigned long int ID;
208     void           *payl;
209     int             payl_size;
210 } s_gras_msg_t, *gras_msg_t;
211
212 typedef int (*gras_msg_filter_t)(gras_msg_t msg,void *ctx);
213
214   void gras_msg_wait_ext(double           timeout,    
215                          gras_msgtype_t   msgt_want,
216                          gras_socket_t    expe_want,
217                          gras_msg_filter_t filter,
218                          void             *filter_ctx, 
219                          gras_msg_t       msg_got);
220
221
222 /* @} */
223
224 SG_END_DECL()
225
226 #endif /* GRAS_MSG_H */
227