Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
shortest path algorithm already working
[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
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 to
32  *  it. If it arrives when before or after \ref 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 /** @defgroup GRAS_msg_decl Message declaration and retrival 
39  *  @ingroup  GRAS_msg
40  *  
41  *  GRAS messages can only accept one type of payload. See \ref GRAS_dd for
42  *  more information on how to describe data in GRAS.
43  *
44  *  If you absolutely want use a message able to convey several datatypes,
45  *  you can always say that it conveys a generic reference (see
46  *  \ref gras_datadesc_ref_generic).
47  * 
48  *  In order to ease the upgrade of GRAS applications, it is possible to \e version the messages, ie 
49  *  to add a version number to the message (by default, the version is set to 0). Any messages of the 
50  *  wrong version will be ignored by the applications not providing any specific callback for them.
51  *  
52  *  This mechanism (stolen from the dynamic loader one) should ensure you to change the semantic of a given
53  *  message while still understanding the old one.
54  */
55 /** @{ */  
56 /** \brief Opaque type */
57 typedef struct s_gras_msgtype *gras_msgtype_t;
58
59   void gras_msgtype_declare  (const char           *name,
60                               gras_datadesc_type_t  payload);
61   void gras_msgtype_declare_v(const char           *name,
62                               short int             version,
63                               gras_datadesc_type_t  payload);
64
65   gras_msgtype_t gras_msgtype_by_name (const char *name);
66   gras_msgtype_t gras_msgtype_by_namev(const char *name, short int version);
67   gras_msgtype_t gras_msgtype_by_id(int id);
68
69 /** @} */  
70 /** @defgroup GRAS_msg_cb Callback declaration and use
71  *  @ingroup  GRAS_msg
72  * 
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    *
88    * \param expeditor: a socket to contact who sent this message
89    * \param payload: the message itself
90    *
91    * \return true if the message was consumed by the callback, 
92    *  false if the message was refused by the callback (and should be 
93    *  passed to the next callback of the stack for this message)
94    *
95    * Once a such a function is registered to handle messages of a given
96    * type with \ref gras_cb_register(), it will be called each time such
97    * a message arrives (unless a gras_msg_wait() intercepts it on arrival).
98    *
99    * If the callback accepts the message, it should free it after use.
100    */
101   typedef int (*gras_msg_cb_t)(gras_socket_t  expeditor,
102                                void          *payload);
103
104   void gras_cb_register  (gras_msgtype_t msgtype, gras_msg_cb_t cb);
105   void gras_cb_unregister(gras_msgtype_t msgtype, gras_msg_cb_t cb);
106
107 /** @} */  
108 /** @defgroup GRAS_msg_exchange Message exchange 
109  *  @ingroup  GRAS_msg
110  *
111  */
112 /** @{ */
113
114   void gras_msg_send(gras_socket_t   sock,
115                      gras_msgtype_t  msgtype,
116                      void           *payload);
117   void gras_msg_wait(double          timeout,    
118                      gras_msgtype_t  msgt_want,
119                      gras_socket_t  *expeditor,
120                      void           *payload);
121   void gras_msg_wait_ext(double           timeout,    
122                          gras_msgtype_t   msgt_want,
123                          gras_socket_t    expe_want,
124                          int_f_pvoid_pvoid_t payl_filter,
125                          void               *filter_ctx, 
126                          gras_msgtype_t  *msgt_got,
127                          gras_socket_t   *expe_got,
128                          void            *payl_got);
129
130   void gras_msg_handle(double timeOut);
131
132 /* @} */
133
134 SG_END_DECL()
135
136 #endif /* GRAS_MSG_H */
137