Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1a025195c0e6a0cc80e6a0818684e6b0e6d9291c
[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 BEGIN_DECL()
19
20 /** @addtogroup GRAS_msg
21  *  @brief Defining messages and callbacks, and exchanging messages (Communication facility) 
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
32  *  to it. If it arrives when before or after 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  */
39
40 /** @name Message declaration and retrival 
41  *  
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).
45  * 
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.
49  *  
50  *  This mecanism (stolen from the dynamic loader one) should ensure you to change the semantic of a given
51  *  message while still understanding the old one.
52  */
53 /** @{ */  
54 /** \brief Opaque type */
55 typedef struct s_gras_msgtype *gras_msgtype_t;
56
57 /** \brief declare a new message type of the given name. It only accepts the given datadesc as payload */
58 void gras_msgtype_declare  (const char           *name,
59                             gras_datadesc_type_t  payload);
60 /** \brief declare a new versionned message type of the given name and payload. */
61 void gras_msgtype_declare_v(const char           *name,
62                             short int             version,
63                             gras_datadesc_type_t  payload);
64
65 /** \brief retrive an existing message type from its name. */
66 gras_msgtype_t gras_msgtype_by_name (const char     *name);
67 /** \brief retrive an existing message type from its name and version number. */
68 gras_msgtype_t gras_msgtype_by_namev(const char     *name,
69                                      short int       version);
70 /** @} */  
71
72 /** @name Callback declaration and use
73  * 
74  * This is how to register a given function so that it gets called when a
75  * given type of message arrives.
76  * 
77  * You can register several callbacks to the same kind of messages, and
78  * they will get stacked. The lastly added callback gets the message first.
79  * If it consumes the message, it should return a true value when done. If
80  * not, it should return 0, and the message will be passed to the second
81  * callback of the stack, if any.
82  * 
83  * @{
84  */
85  
86 /** \brief Type of message callback functions. 
87  * \param msg: The message itself
88  * \return true if the message was consumed by the callback, false if the message was
89  * refused by the callback (and should be passed to the next callback of the stack for
90  * this message)
91  *
92  * Once a such a function is registered to handle messages of a given type with 
93  * \ref gras_cb_register(), it will be called each time such a message arrives.
94  *
95  * If the callback accepts the message, it should free it after use.
96  */
97 typedef int (*gras_cb_t)(gras_socket_t  expeditor,
98                          void          *payload);
99 /** \brief Bind the given callback to the given message type 
100  *
101  * Several callbacks can be attached to a given message type. The lastly added one will get the message first, and 
102  * if it returns false, the message will be passed to the second one. 
103  * And so on until one of the callbacks accepts the message.
104  */
105 void gras_cb_register  (gras_msgtype_t msgtype,
106                         gras_cb_t      cb);
107 /** \brief Unbind the given callback from the given message type */
108 void gras_cb_unregister(gras_msgtype_t msgtype,
109                         gras_cb_t      cb);
110
111 /** @} */  
112 /** @name Message exchange */
113 /** @{ */
114 /** \brief Send the data pointed by \a payload as a message of type \a msgtype to the peer \a sock */
115 xbt_error_t gras_msg_send(gras_socket_t   sock,
116                            gras_msgtype_t  msgtype,
117                            void           *payload);
118 /** \brief Waits for a message to come in over a given socket. */
119 xbt_error_t gras_msg_wait(double          timeout,    
120                            gras_msgtype_t  msgt_want,
121                            gras_socket_t  *expeditor,
122                            void           *payload);
123 xbt_error_t gras_msg_handle(double timeOut);
124
125 /*@}*/
126
127 END_DECL()
128
129 #endif /* GRAS_MSG_H */
130