mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
moved message send/recv functions to msg.c
git-svn-id: https://zerberus.eonerc.rwth-aachen.de:8443/svn/s2ss/trunk@27 8ec27952-4edc-4aab-86aa-e87bb2611832
This commit is contained in:
parent
ef7482ac8b
commit
8b1ba90c31
4 changed files with 38 additions and 18 deletions
|
@ -103,8 +103,25 @@ int msg_fscan(FILE *f, struct msg *m);
|
|||
void msg_random(struct msg *m);
|
||||
|
||||
/**
|
||||
* @brief Send a message to a node
|
||||
*
|
||||
*
|
||||
* @param m A pointer to the message
|
||||
* @param n A pointer to the node
|
||||
* @return
|
||||
* - 0 on success
|
||||
* - otherwise an error occured
|
||||
*/
|
||||
int msg_send(struct msg *m, struct node *n);
|
||||
|
||||
/**
|
||||
* @brief Receive a message from a node
|
||||
*
|
||||
* @param m A pointer to the message
|
||||
* @param n A pointer to the node
|
||||
* @return
|
||||
* - 0 on success
|
||||
* - otherwise an error occured
|
||||
*/
|
||||
int msg_recv(struct msg *m, struct node *n);
|
||||
|
||||
#endif /* _MSG_H_ */
|
||||
|
|
|
@ -67,19 +67,11 @@ struct node* node_create(const char *name, enum node_type type, const char *loca
|
|||
void node_destroy(struct node* n);
|
||||
|
||||
/**
|
||||
* Send a single message to a node
|
||||
*
|
||||
* @param sd The descriptor of the UDP socket
|
||||
* @param msg A pointer to the UDP message
|
||||
*/
|
||||
int node_send(struct node *n, struct msg *m);
|
||||
|
||||
/**
|
||||
* Receive a single message from a node
|
||||
*
|
||||
* @param sd The descriptor of the UDP socket
|
||||
* @param msg A pointer to the UDP message
|
||||
*/
|
||||
int node_recv(struct node *n, struct msg *m);
|
||||
|
||||
#endif /* _NODE_H_ */
|
||||
|
|
20
src/msg.c
20
src/msg.c
|
@ -7,8 +7,11 @@
|
|||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include "msg.h"
|
||||
#include "utils.h"
|
||||
|
||||
int msg_fprint(FILE *f, struct msg *msg)
|
||||
{
|
||||
|
@ -47,3 +50,20 @@ void msg_random(struct msg *m)
|
|||
m->sequence++;
|
||||
}
|
||||
|
||||
int msg_send(struct msg *m, struct node *n)
|
||||
{
|
||||
if (sendto(n->sd, m, m->length + 8, 0,
|
||||
(struct sockaddr *) &n->remote,
|
||||
sizeof(struct sockaddr_in)) < 0)
|
||||
perror("Failed sendto");
|
||||
|
||||
debug(10, "Message sent to node %s (%s:%u)", n->name, inet_ntoa(n->remote.sin_addr), ntohs(n->remote.sin_port));
|
||||
}
|
||||
|
||||
int msg_recv(struct msg *m, struct node *n)
|
||||
{
|
||||
if (recv(n->sd, m, sizeof(struct msg), 0) < 0)
|
||||
perror("Failed recv");
|
||||
|
||||
debug(10, "Message received from node %s", n->name);
|
||||
}
|
||||
|
|
|
@ -117,18 +117,9 @@ void node_destroy(struct node* n)
|
|||
free(n);
|
||||
}
|
||||
|
||||
int node_send(struct node *n, struct msg *m)
|
||||
{
|
||||
send(n->sd, m, sizeof(struct msg), 0);
|
||||
debug(1, "Message sent to node %s", n->name);
|
||||
msg_fprint(stdout, m);
|
||||
}
|
||||
|
||||
int node_recv(struct node *n, struct msg *m)
|
||||
{
|
||||
size_t ret = recv(n->sd, m, sizeof(struct msg), 0);
|
||||
if (ret < 0)
|
||||
error("Recv failed: %s", strerror(errno));
|
||||
|
||||
debug(1, "Message received from node %s", n->name);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue