Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
bd56c82bd4dd09fd2a77270551ae8e3909c1ab12
[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  */
24
25 /** @name Message declaration and retrival 
26  *  
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).
30  * 
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.
34  *  
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.
37  */
38 /** @{ */  
39 /** \brief Opaque type */
40 typedef struct s_gras_msgtype *gras_msgtype_t;
41
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,
47                             short int             version,
48                             gras_datadesc_type_t  payload);
49
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,
54                                      short int       version);
55 /** @} */  
56
57 /** @name Callback declaration and use */
58 /** @{ */
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
63  * this message)
64  *
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.
67  *
68  * If the callback accepts the message, it should free it after use.
69  */
70 typedef int (*gras_cb_t)(gras_socket_t  expeditor,
71                          void          *payload);
72 /** \brief Bind the given callback to the given message type 
73  *
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.
77  */
78 void gras_cb_register  (gras_msgtype_t msgtype,
79                         gras_cb_t      cb);
80 /** \brief Unbind the given callback from the given message type */
81 void gras_cb_unregister(gras_msgtype_t msgtype,
82                         gras_cb_t      cb);
83
84 /** @} */  
85 /** @name Message exchange */
86 /** @{ */
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,
90                            void           *payload);
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,
95                            void           *payload);
96 xbt_error_t gras_msg_handle(double timeOut);
97
98 /*@}*/
99
100 END_DECL()
101
102 #endif /* GRAS_MSG_H */
103