grapes / include / net_helper.h @ 0cb8677a
History | View | Annotate | Download (5.14 KB)
1 |
#ifndef NET_HELPER_H
|
---|---|
2 |
#define NET_HELPER_H
|
3 |
|
4 |
/**
|
5 |
* @file net_helper.h
|
6 |
*
|
7 |
* @brief Communication facility interface for SOM.
|
8 |
*
|
9 |
* A clean interface is provided, through which all the communication procedures needed by SOM functions
|
10 |
* are handled. This way the different SOM functionalities are not dependent on any particular
|
11 |
* library with respect of the way they may call or be called by other applicative components.
|
12 |
*/
|
13 |
|
14 |
/**
|
15 |
* Implementation dependent internal representation of a node ID.
|
16 |
*/
|
17 |
struct nodeID;
|
18 |
|
19 |
/**
|
20 |
* @brief Duplicate a nodeID.
|
21 |
* This function provides a duplicate of the given nodeID.
|
22 |
* @param[in] s A pointer to the nodeID to be duplicated.
|
23 |
* @return A pointer to the duplicate of the argument nodeID.
|
24 |
*/
|
25 |
struct nodeID *nodeid_dup(struct nodeID *s); |
26 |
|
27 |
/**
|
28 |
* @brief Test if two nodes are identical.
|
29 |
* Test if two nodeIDs can be considered identical (where the definition of identity is implementation dependent).
|
30 |
* @param[in] s1 The first nodeID to be compared.
|
31 |
* @param[in] s2 The second nodeID to be compared.
|
32 |
* @return 1 if the two nodeID are identical or 0 if they are not.
|
33 |
*/
|
34 |
int nodeid_equal(const struct nodeID *s1, const struct nodeID *s2); |
35 |
|
36 |
/**
|
37 |
* @brief Create a new nodeID.
|
38 |
* Create a new nodeID from a given IP address and port number.
|
39 |
* @param[in] IPaddr The IP address in string form to be associated to the new nodeID.
|
40 |
* @param[in] port The port to be associated to the new nodeID.
|
41 |
* @return A pointer to the new nodeID.
|
42 |
*/
|
43 |
struct nodeID *create_node(const char *IPaddr, int port); |
44 |
|
45 |
/**
|
46 |
* @brief Delete a nodeID.
|
47 |
* Delete a nodeID and free the allocated resources.
|
48 |
* @param[in] s A pointer to the nodeID to be deleted.
|
49 |
*/
|
50 |
void nodeid_free(struct nodeID *s); |
51 |
|
52 |
/**
|
53 |
* @brief Initialize all needed internal parameters.
|
54 |
* Initialize the parameters for the networking facilities and create a nodeID representing the caller.
|
55 |
* @param[in] IPaddr The IP in string form to be associated to the caller.
|
56 |
* @param[in] port The port to be associated to the caller.
|
57 |
* @return A pointer to a nodeID representing the caller, initialized with all the necessary data.
|
58 |
*/
|
59 |
struct nodeID *net_helper_init(const char *IPaddr, int port); |
60 |
|
61 |
/**
|
62 |
* @brief Map net_helper's ML callback to the given message type.
|
63 |
* To be used if ML support is needed. Register the common net_hepler callback for a msg_type.
|
64 |
* @param[in] mesType The MSG_TYPE of the message the caller is interested in.
|
65 |
*/
|
66 |
void bind_msg_type (uint8_t msgtype);
|
67 |
|
68 |
/**
|
69 |
* @brief Send data to a remote peer.
|
70 |
* This function provides a transparently handles the sending routines.
|
71 |
* @param[in] from A pointer to the nodeID representing the caller.
|
72 |
* @param[in] to A pointer to the nodeID representing the remote peer.
|
73 |
* @param[in] buffer_ptr A pointer to the buffer containing the data to be sent.
|
74 |
* @param[in] buffer_size The length of the data buffer.
|
75 |
* @return The number of bytes sent or -1 if some error occurred.
|
76 |
*/
|
77 |
int send_to_peer(const struct nodeID *from, struct nodeID *to, const uint8_t *buffer_ptr, int buffer_size); |
78 |
|
79 |
/**
|
80 |
* @brief Receive data from a remote peer.
|
81 |
* This function transparently handles the receiving routines.
|
82 |
* @param[in] local A pointer to the nodeID representing the caller.
|
83 |
* @param[out] remote The address to a pointer that has to be set to a new nodeID representing the sender peer.
|
84 |
* @param[out] buffer_ptr A pointer to the buffer containing the received data.
|
85 |
* @param[out] buffer_size The size of the data buffer.
|
86 |
* @return The number of received bytes or -1 if some error occurred.
|
87 |
*/
|
88 |
int recv_from_peer(const struct nodeID *local, struct nodeID **remote, uint8_t *buffer_ptr, int buffer_size); |
89 |
|
90 |
|
91 |
/**
|
92 |
* @brief Check for newly arrived data.
|
93 |
* Check if some data arrived for a given nodeID. It sets a timeout to return at most after a given time.
|
94 |
* @param[in] n A pointer to the nodeID representing the caller.
|
95 |
* @param[in] tout A pointer to a timer to be used to set the waiting timeout.
|
96 |
* @param[in] user_fds A "-1 terminated" array of FDs to be monitored.
|
97 |
* @return 1 if some data has arrived, 0 otherwise.
|
98 |
*/
|
99 |
int wait4data(const struct nodeID *n, struct timeval *tout, int *user_fds); |
100 |
|
101 |
/**
|
102 |
* @brief Give a string representation of a nodeID.
|
103 |
* Give a string representation of a nodeID.
|
104 |
* @param[in] s A pointer to the nodeID to be printed.
|
105 |
* @return A string representing the nodeID.
|
106 |
*/
|
107 |
const char *node_addr(const struct nodeID *s); |
108 |
|
109 |
/**
|
110 |
* @brief Create a nodeID structure from a serialized object.
|
111 |
* Read from a properly filled byte array (@see #nodeid_dump) and build a new nodeID from its serialized representation in the buffer.
|
112 |
* @param[in] b A pointer to the byte array containing the data to be used.
|
113 |
* @param[in] len The number of bytes to be read from the buffer to build the new nodeID.
|
114 |
* @return A pointer to the new nodeID.
|
115 |
*/
|
116 |
struct nodeID *nodeid_undump(const uint8_t *b, int *len); |
117 |
|
118 |
/**
|
119 |
* @brief Serialize a nodeID in a byte array.
|
120 |
* Serialize a nodeID in a byte array.
|
121 |
* @param[in] b A pointer to the byte array that will contain the nodeID serialization.
|
122 |
* @param[in] s A pointer to the nodeID to be serialized.
|
123 |
* @param[in] max_write_size A number of bytes available in b
|
124 |
* @return The number of bytes written in the buffer, or -1 if error
|
125 |
*/
|
126 |
int nodeid_dump(uint8_t *b, const struct nodeID *s, size_t max_write_size); |
127 |
|
128 |
#endif /* NET_HELPER_H */ |