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 (Communication facility)
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
32 * to it. If it arrives when before or after gras_msg_wait, it will be
33 * passed to the callback.
35 * For an example of use, please refer to \ref GRAS_ex_ping.
40 /** @name 1. Message declaration and retrival
42 * GRAS messages can only accept one type of payload. If you absolutely want to declare a message
43 * able to convey several datatypes, you can always say that it conveys a generic reference (see
44 * \ref gras_datadesc_ref_generic).
46 * In order to ease the upgrade of GRAS applications, it is possible to \e version the messages, ie
47 * to add a version number to the message (by default, the version is set to 0). Any messages of the
48 * wrong version will be ignored by the applications not providing any specific callback for them.
50 * This mechanism (stolen from the dynamic loader one) should ensure you to change the semantic of a given
51 * message while still understanding the old one.
54 /** \brief Opaque type */
55 typedef struct s_gras_msgtype *gras_msgtype_t;
57 void gras_msgtype_declare (const char *name,
58 gras_datadesc_type_t payload);
59 void gras_msgtype_declare_v(const char *name,
61 gras_datadesc_type_t payload);
63 gras_msgtype_t gras_msgtype_by_name (const char *name);
64 gras_msgtype_t gras_msgtype_by_namev(const char *name, short int version);
67 /** @name 2. Callback declaration and use
69 * This is how to register a given function so that it gets called when a
70 * given type of message arrives.
72 * You can register several callbacks to the same kind of messages, and
73 * they will get stacked. The lastly added callback gets the message first.
74 * If it consumes the message, it should return a true value when done. If
75 * not, it should return 0, and the message will be passed to the second
76 * callback of the stack, if any.
81 /** \brief Type of message callback functions.
83 * \param expeditor: a socket to contact who sent this message
84 * \param payload: the message itself
86 * \return true if the message was consumed by the callback,
87 * false if the message was refused by the callback (and should be
88 * passed to the next callback of the stack for this message)
90 * Once a such a function is registered to handle messages of a given
91 * type with \ref gras_cb_register(), it will be called each time such
92 * a message arrives (unless a gras_msg_wait() intercepts it on arrival).
94 * If the callback accepts the message, it should free it after use.
96 typedef int (*gras_msg_cb_t)(gras_socket_t expeditor,
99 void gras_cb_register (gras_msgtype_t msgtype, gras_msg_cb_t cb);
100 void gras_cb_unregister(gras_msgtype_t msgtype, gras_msg_cb_t cb);
103 /** @name 3. Message exchange */
106 void gras_msg_send(gras_socket_t sock,
107 gras_msgtype_t msgtype,
109 void gras_msg_wait(double timeout,
110 gras_msgtype_t msgt_want,
111 gras_socket_t *expeditor,
113 void gras_msg_handle(double timeOut);
119 #endif /* GRAS_MSG_H */