Statistics
| Branch: | Revision:

grapes / include / net_helper.h @ ef4061e4

History | View | Annotate | Download (5.9 KB)

1
#ifndef NET_HELPER_H
2
#define NET_HELPER_H
3

    
4
#include <sys/time.h>
5

    
6
/**
7
* @file net_helper.h
8
*
9
* @brief Communication facility interface for SOM.
10
*
11
* A clean interface is provided, through which all the communication procedures needed by SOM functions
12
* are handled. This way the different SOM functionalities are not dependent on any particular
13
* library with respect of the way they may call or be called by other applicative components.
14
*/
15

    
16
/**
17
* Implementation dependent internal representation of a node ID.
18
*/
19
struct nodeID;
20

    
21
/**
22
* @brief Duplicate a nodeID.
23
* This function provides a duplicate of the given nodeID.
24
* @param[in] s A pointer to the nodeID to be duplicated.
25
* @return A pointer to the duplicate of the argument nodeID.
26
*/
27
struct nodeID *nodeid_dup(struct nodeID *s);
28

    
29
/**
30
* @brief Test if two nodes are identical.
31
* Test if two nodeIDs can be considered identical (where the definition of identity is implementation dependent).
32
* @param[in] s1 The first nodeID to be compared.
33
* @param[in] s2 The second nodeID to be compared.
34
* @return 1 if the two nodeID are identical or 0 if they are not.
35
*/
36
int nodeid_equal(const struct nodeID *s1, const struct nodeID *s2);
37

    
38
/**
39
* @brief Compare two nodes and give some consistent ordering.
40
* This ordering  should only be used for keeping lists ordered, it has no other meaning.
41
* @param[in] s1 The first nodeID to be compared.
42
* @param[in] s2 The second nodeID to be compared.
43
* @return -1, 0 or 1, depending on the relation between  s1 and s2.
44
*/
45
int nodeid_cmp(const struct nodeID *s1, const struct nodeID *s2);
46

    
47
/**
48
* @brief Create a new nodeID.
49
* Create a new nodeID from a given IP address and port number.
50
* @param[in] IPaddr The IP address in string form to be associated to the new nodeID.
51
* @param[in] port The port to be associated to the new nodeID.
52
* @return A pointer to the new nodeID.
53
*/
54
struct nodeID *create_node(const char *IPaddr, int port);
55

    
56
/**
57
* @brief Delete a nodeID.
58
* Delete a nodeID and free the allocated resources.
59
* @param[in] s A pointer to the nodeID to be deleted.
60
*/
61
void nodeid_free(struct nodeID *s);
62

    
63
/**
64
* @brief Initialize all needed internal parameters.
65
* Initialize the parameters for the networking facilities and create a nodeID representing the caller.
66
* @param[in] IPaddr The IP in string form to be associated to the caller.
67
* @param[in] port The port to be associated to the caller.
68
* @param[in] config Additional configuration options.
69
* @return A pointer to a nodeID representing the caller, initialized with all the necessary data.
70
*/
71
struct nodeID *net_helper_init(const char *IPaddr, int port,const char *config);
72

    
73
/**
74
* @brief Map net_helper's ML callback to the given message type.
75
* To be used if ML support is needed. Register the common net_hepler callback for a msg_type.
76
* @param[in] mesType The MSG_TYPE of the message the caller is interested in.
77
*/
78
void bind_msg_type (uint8_t msgtype);
79

    
80
/**
81
* @brief Send data to a remote peer.
82
* This function provides a transparently handles the sending routines.
83
* @param[in] from A pointer to the nodeID representing the caller.
84
* @param[in] to A pointer to the nodeID representing the remote peer.
85
* @param[in] buffer_ptr A pointer to the buffer containing the data to be sent.
86
* @param[in] buffer_size The length of the data buffer.
87
* @return The number of bytes sent or -1 if some error occurred.
88
*/
89
int send_to_peer(const struct nodeID *from, struct nodeID *to, const uint8_t *buffer_ptr, int buffer_size);
90

    
91
/**
92
* @brief Receive data from a remote peer.
93
* This function transparently handles the receiving routines.
94
* @param[in] local A pointer to the nodeID representing the caller.
95
* @param[out] remote The address to a pointer that has to be set to a new nodeID representing the sender peer.
96
* @param[out] buffer_ptr A pointer to the buffer containing the received data.
97
* @param[out] buffer_size The size of the data buffer.
98
* @return The number of received bytes or -1 if some error occurred.
99
*/
100
int recv_from_peer(const struct nodeID *local, struct nodeID **remote, uint8_t *buffer_ptr, int buffer_size);
101

    
102

    
103
/**
104
* @brief Check for newly arrived data.
105
* Check if some data arrived for a given nodeID. It sets a timeout to return at most after a given time.
106
* @param[in] n A pointer to the nodeID representing the caller.
107
* @param[in] tout A pointer to a timer to be used to set the waiting timeout.
108
* @param[in] user_fds A "-1 terminated" array of FDs to be monitored.
109
* @return 1 if some data has arrived, 0 otherwise.
110
*/
111
int wait4data(const struct nodeID *n, struct timeval *tout, int *user_fds);
112

    
113
/**
114
* @brief Give a string representation of a nodeID.
115
* Give a string representation of a nodeID.
116
* @param[in] s A pointer to the nodeID to be printed.
117
* @return A string representing the nodeID.
118
*/
119
const char *node_addr(const struct nodeID *s);
120

    
121
/**
122
* @brief Create a nodeID structure from a serialized object.
123
* Read from a properly filled byte array (@see #nodeid_dump) and build a new nodeID from its serialized representation in the buffer.
124
* @param[in] b A pointer to the byte array containing the data to be used.
125
* @param[in] len The number of bytes to be read from the buffer to build the new nodeID.
126
* @return A pointer to the new nodeID.
127
*/
128
struct nodeID *nodeid_undump(const uint8_t *b, int *len);
129

    
130
/**
131
* @brief Serialize a nodeID in a byte array.
132
* Serialize a nodeID in a byte array.
133
* @param[in] b A pointer to the byte array that will contain the nodeID serialization.
134
* @param[in] s A pointer to the nodeID to be serialized.
135
* @param[in] max_write_size A number of bytes available in b
136
* @return The number of bytes written in the buffer, or -1 if error
137
*/
138
int nodeid_dump(uint8_t *b, const struct nodeID *s, size_t max_write_size);
139

    
140
/**
141
* @brief Give a string representation of the public IP belonging to the nodeID.
142
* Serialize the public IP address of a given node and return it.
143
* @param[in] s A pointer to the nodeID.
144
* @return the publicly accessible IP address of the node.
145
*/
146
const char *node_ip(const struct nodeID *s);
147

    
148
#endif /* NET_HELPER_H */