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)
25 /** @name Message declaration and retrival
27 * GRAS messages can only accept one type of payload. If you absolutely want to declare a message
28 * able to convey several datatypes, you can always say that it conveys a generic reference (see
29 * \ref gras_datadesc_ref_generic).
31 * In order to ease the upgrade of GRAS applications, it is possible to \e version the messages, ie
32 * to add a version number to the message (by default, the version is set to 0). Any messages of the
33 * wrong version will be ignored by the applications not providing any specific callback for them.
35 * This mecanism (stolen from the dynamic loader one) should ensure you to change the semantic of a given
36 * message while still understanding the old one.
39 /** \brief Opaque type */
40 typedef struct s_gras_msgtype *gras_msgtype_t;
42 /** \brief declare a new message type of the given name. It only accepts the given datadesc as payload */
43 void gras_msgtype_declare (const char *name,
44 gras_datadesc_type_t payload);
45 /** \brief declare a new versionned message type of the given name and payload. */
46 void gras_msgtype_declare_v(const char *name,
48 gras_datadesc_type_t payload);
50 /** \brief retrive an existing message type from its name. */
51 gras_msgtype_t gras_msgtype_by_name (const char *name);
52 /** \brief retrive an existing message type from its name and version number. */
53 gras_msgtype_t gras_msgtype_by_namev(const char *name,
57 /** @name Callback declaration and use */
59 /** \brief Type of message callback functions.
60 * \param msg: The message itself
61 * \return true if the message was consumed by the callback, false if the message was
62 * refused by the callback (and should be passed to the next callback of the stack for
65 * Once a such a function is registered to handle messages of a given type with
66 * \ref gras_cb_register(), it will be called each time such a message arrives.
68 * If the callback accepts the message, it should free it after use.
70 typedef int (*gras_cb_t)(gras_socket_t expeditor,
72 /** \brief Bind the given callback to the given message type
74 * Several callbacks can be attached to a given message type. The lastly added one will get the message first, and
75 * if it returns false, the message will be passed to the second one.
76 * And so on until one of the callbacks accepts the message.
78 void gras_cb_register (gras_msgtype_t msgtype,
80 /** \brief Unbind the given callback from the given message type */
81 void gras_cb_unregister(gras_msgtype_t msgtype,
85 /** @name Message exchange */
87 /** \brief Send the data pointed by \a payload as a message of type \a msgtype to the peer \a sock */
88 xbt_error_t gras_msg_send(gras_socket_t sock,
89 gras_msgtype_t msgtype,
91 /** \brief Waits for a message to come in over a given socket. */
92 xbt_error_t gras_msg_wait(double timeout,
93 gras_msgtype_t msgt_want,
94 gras_socket_t *expeditor,
96 xbt_error_t gras_msg_handle(double timeOut);
102 #endif /* GRAS_MSG_H */