Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Connect node GRAS_msg into the doxygen module tree
[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 (Communication facility) 
22  * 
23  * <center><table><tr><td><b>Top</b>    <td> [\ref index]::[\ref GRAS_API]
24  *                <tr><td><b>Prev</b>   <td> [\ref GRAS_sock]
25  *                <tr><td><b>Next</b>   <td> [\ref GRAS_timer]
26  *                <tr><td><b>Down</b>   <td> [\ref GRAS_msg_decl]            </table></center>
27  *
28  *  There is two way to receive messages in GRAS. The first one is to
29  *  register a given function as callback to a given type of messages (see
30  *  \ref gras_cb_register and associated section). But you can also
31  *  explicitely wait for a given message with the \ref gras_msg_wait
32  *  function.
33  * 
34  *  Usually, both ways are not intended to be mixed of a given type of
35  *  messages. But if you do so, it shouldn't trigger any issue.  If the
36  *  message arrives when gras_msg_wait is blocked, then it will be routed
37  *  to it. If it arrives when before or after gras_msg_wait, it will be
38  *  passed to the callback.
39  * 
40  *  For an example of use, please refer to \ref GRAS_ex_ping.
41  */
42
43 /** @defgroup GRAS_msg_decl Message declaration and retrival 
44  *  @ingroup  GRAS_msg
45  *  
46  * <center><table><tr><td><b>Top</b>    <td> [\ref index]::[\ref GRAS_API]::[\ref GRAS_msg]
47  *                <tr><td>   Prev       <td> 
48  *                <tr><td><b>Next</b>   <td> [\ref GRAS_msg_cb]               </table></center>
49  *
50  *  GRAS messages can only accept one type of payload. If you absolutely want to declare a message
51  *  able to convey several datatypes, you can always say that it conveys a generic reference (see 
52  *  \ref gras_datadesc_ref_generic).
53  * 
54  *  In order to ease the upgrade of GRAS applications, it is possible to \e version the messages, ie 
55  *  to add a version number to the message (by default, the version is set to 0). Any messages of the 
56  *  wrong version will be ignored by the applications not providing any specific callback for them.
57  *  
58  *  This mechanism (stolen from the dynamic loader one) should ensure you to change the semantic of a given
59  *  message while still understanding the old one.
60  */
61 /** @{ */  
62 /** \brief Opaque type */
63 typedef struct s_gras_msgtype *gras_msgtype_t;
64
65   void gras_msgtype_declare  (const char           *name,
66                               gras_datadesc_type_t  payload);
67   void gras_msgtype_declare_v(const char           *name,
68                               short int             version,
69                               gras_datadesc_type_t  payload);
70
71   gras_msgtype_t gras_msgtype_by_name (const char *name);
72   gras_msgtype_t gras_msgtype_by_namev(const char *name, short int version);
73   gras_msgtype_t gras_msgtype_by_id(int id);
74
75 /** @} */  
76 /** @defgroup GRAS_msg_cb Callback declaration and use
77  *  @ingroup  GRAS_msg
78  * 
79  * <center><table><tr><td><b>Top</b>    <td> [\ref index]::[\ref GRAS_API]::[\ref GRAS_msg]
80  *                <tr><td><b>Prev</b>   <td> [\ref GRAS_msg_decl]
81  *                <tr><td><b>Next</b>   <td> [\ref GRAS_msg_exchange]       </table></center>
82  *
83  * This is how to register a given function so that it gets called when a
84  * given type of message arrives.
85  * 
86  * You can register several callbacks to the same kind of messages, and
87  * they will get stacked. The lastly added callback gets the message first.
88  * If it consumes the message, it should return a true value when done. If
89  * not, it should return 0, and the message will be passed to the second
90  * callback of the stack, if any.
91  * 
92  * @{
93  */
94  
95   /** \brief Type of message callback functions. 
96    *
97    * \param expeditor: a socket to contact who sent this message
98    * \param payload: the message itself
99    *
100    * \return true if the message was consumed by the callback, 
101    *  false if the message was refused by the callback (and should be 
102    *  passed to the next callback of the stack for this message)
103    *
104    * Once a such a function is registered to handle messages of a given
105    * type with \ref gras_cb_register(), it will be called each time such
106    * a message arrives (unless a gras_msg_wait() intercepts it on arrival).
107    *
108    * If the callback accepts the message, it should free it after use.
109    */
110   typedef int (*gras_msg_cb_t)(gras_socket_t  expeditor,
111                                void          *payload);
112
113   void gras_cb_register  (gras_msgtype_t msgtype, gras_msg_cb_t cb);
114   void gras_cb_unregister(gras_msgtype_t msgtype, gras_msg_cb_t cb);
115
116 /** @} */  
117 /** @defgroup GRAS_msg_exchange Message exchange 
118  *  @ingroup  GRAS_msg
119  *
120  * <center><table><tr><td><b>Top</b>    <td> [\ref index]::[\ref GRAS_API]::[\ref GRAS_msg]
121  *                <tr><td><b>Prev</b>   <td> [\ref GRAS_msg_cb]
122  *                <tr><td>   Next       <td>                        </table></center>
123  */
124 /** @{ */
125
126   void gras_msg_send(gras_socket_t   sock,
127                      gras_msgtype_t  msgtype,
128                      void           *payload);
129   void gras_msg_wait(double          timeout,    
130                      gras_msgtype_t  msgt_want,
131                      gras_socket_t  *expeditor,
132                      void           *payload);
133   void gras_msg_handle(double timeOut);
134
135 /* @} */
136
137 SG_END_DECL()
138
139 #endif /* GRAS_MSG_H */
140