3 /* messaging - high level communication (send/receive messages) */
4 /* module's public interface exported to end user. */
6 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */
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. */
11 #ifndef GRAS_MESSAGES_H
12 #define GRAS_MESSAGES_H
15 #include "gras/transport.h"
16 #include "gras/datadesc.h"
20 /** @addtogroup GRAS_msg
21 * @brief Defining messages and callbacks, and exchanging messages
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
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.
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.
40 /** @defgroup GRAS_msg_decl Message declaration and retrival
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.
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).
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
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
61 /** \brief Opaque type */
62 typedef struct s_gras_msgtype *gras_msgtype_t;
64 void gras_msgtype_declare (const char *name,
65 gras_datadesc_type_t payload);
66 void gras_msgtype_declare_v(const char *name,
68 gras_datadesc_type_t payload);
70 gras_msgtype_t gras_msgtype_by_name (const char *name);
71 gras_msgtype_t gras_msgtype_by_namev(const char *name, short int version);
72 gras_msgtype_t gras_msgtype_by_id(int id);
75 /** @defgroup GRAS_msg_rpcdecl RPC declaration
78 * Remote Procedure Call (RPC) are a classical mecanism to request a service
79 * from a remote host. Using this set of functions, you let GRAS doing most of
80 * the work of sending the request, wait for an answer, make sure it is the
81 * right answer from the right host and so on. Any exception raised on the
82 * server is also passed over the network to the client.
86 void gras_msgtype_declare_rpc(const char *name,
87 gras_datadesc_type_t payload_request,
88 gras_datadesc_type_t payload_answer);
90 void gras_msgtype_declare_rpc_v(const char *name,
92 gras_datadesc_type_t payload_request,
93 gras_datadesc_type_t payload_answer);
97 /** @defgroup GRAS_msg_cb Callback declaration and use
101 * This is how to register a given function so that it gets called when a
102 * given type of message arrives.
104 * You can register several callbacks to the same kind of messages, and
105 * they will get stacked. The lastly added callback gets the message first.
106 * If it consumes the message, it should return a true value when done. If
107 * not, it should return 0, and the message will be passed to the second
108 * callback of the stack, if any.
113 /** \brief Context of callbacks (opaque structure) */
114 typedef struct s_gras_msg_cb_ctx *gras_msg_cb_ctx_t;
116 gras_socket_t gras_msg_cb_ctx_from(gras_msg_cb_ctx_t ctx);
118 /** \brief Type of message callback functions.
120 * \param expeditor: a socket to contact who sent this message
121 * \param payload: the message itself
123 * \return true if the message was consumed by the callback,
124 * false if the message was refused by the callback (and should be
125 * passed to the next callback of the stack for this message)
127 * Once a such a function is registered to handle messages of a given
128 * type with \ref gras_cb_register(), it will be called each time such
129 * a message arrives (unless a gras_msg_wait() intercepts it on arrival).
131 * If the callback accepts the message, it should free it after use.
133 typedef int (*gras_msg_cb_t)(gras_msg_cb_ctx_t ctx,
136 void gras_cb_register (gras_msgtype_t msgtype, gras_msg_cb_t cb);
137 void gras_cb_unregister(gras_msgtype_t msgtype, gras_msg_cb_t cb);
141 /** @defgroup GRAS_msg_rpc RPC specific functions
147 void gras_msg_rpccall(gras_socket_t server,
149 gras_msgtype_t msgtype,
150 void *request, void *answer);
153 void gras_msg_rpcreturn(double timeOut, gras_msg_cb_ctx_t ctx,void *answer);
158 /** @defgroup GRAS_msg_exchange Message exchange
165 void gras_msg_send(gras_socket_t sock,
166 gras_msgtype_t msgtype,
168 void gras_msg_wait(double timeout,
169 gras_msgtype_t msgt_want,
170 gras_socket_t *expeditor,
172 void gras_msg_handle(double timeOut);
175 /** @defgroup GRAS_msg_exchangeadv Message exchange (advanced interface)
181 /** @brief Message kind (internal enum) */
183 e_gras_msg_kind_unknown = 0,
185 e_gras_msg_kind_oneway=1, /**< good old regular messages */
187 e_gras_msg_kind_rpccall=2, /**< RPC request */
188 /* HACK: e_gras_msg_kind_rpccall also designate RPC message *type* in
189 msgtype_t, not only in msg_t*/
190 e_gras_msg_kind_rpcanswer=3, /**< RPC successful answer */
191 e_gras_msg_kind_rpcerror=4, /**< RPC failure on server (payload=exception); should not leak to user-space */
194 call cancel, and others
196 forwarding request and other application level routing stuff
200 e_gras_msg_kind_count=5 /* sentinel, dont mess with */
204 /** @brief Message instance (internal struct) */
207 e_gras_msg_kind_t kind;
209 unsigned long int ID;
212 } s_gras_msg_t, *gras_msg_t;
214 typedef int (*gras_msg_filter_t)(gras_msg_t msg,void *ctx);
216 void gras_msg_wait_ext(double timeout,
217 gras_msgtype_t msgt_want,
218 gras_socket_t expe_want,
219 gras_msg_filter_t filter,
228 #endif /* GRAS_MSG_H */