From 8b1ba90c31cfb558cd7fa4b3ba809e4a099acb9f Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Thu, 5 Jun 2014 09:34:55 +0000 Subject: [PATCH] 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 --- include/msg.h | 19 ++++++++++++++++++- include/node.h | 8 -------- src/msg.c | 20 ++++++++++++++++++++ src/node.c | 9 --------- 4 files changed, 38 insertions(+), 18 deletions(-) diff --git a/include/msg.h b/include/msg.h index ffe437a80..ff6195c95 100644 --- a/include/msg.h +++ b/include/msg.h @@ -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_ */ diff --git a/include/node.h b/include/node.h index 372b063da..d3675bfd2 100644 --- a/include/node.h +++ b/include/node.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_ */ diff --git a/src/msg.c b/src/msg.c index b71946252..b68d5018f 100644 --- a/src/msg.c +++ b/src/msg.c @@ -7,8 +7,11 @@ #include #include +#include +#include #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); +} diff --git a/src/node.c b/src/node.c index 5ba7df9d6..a8356b9fd 100644 --- a/src/node.c +++ b/src/node.c @@ -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); }