From afe5f6ae4e8bea72692388194b5be54528c49d86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Sat, 5 Aug 2017 15:41:01 -0500 Subject: [PATCH] Adding implementation for adding new node. --- include/neocomm/connectivity.h | 5 +--- src/connectivity.c | 50 ++++++++++++++++++++++++---------- src/iconnectivity.h | 16 +++++++++++ 3 files changed, 52 insertions(+), 19 deletions(-) diff --git a/include/neocomm/connectivity.h b/include/neocomm/connectivity.h index 5ee4efc..d150ff7 100644 --- a/include/neocomm/connectivity.h +++ b/include/neocomm/connectivity.h @@ -104,11 +104,8 @@ int NeoComm_add_node(const struct NeoComm_address addr); * @brief Disconnect and remove a node from the directory. * * @param addr The address of the node to remove. - * - * @return If the operation failed then a 0 will be returned and the error can - * be read from the NeoComm_get_last_error function, else 1 is returned. */ -int NeoComm_remove_node(const struct NeoComm_address addr); +void NeoComm_remove_node(const struct NeoComm_address addr); /** * @brief Retrieve information on a specific node. diff --git a/src/connectivity.c b/src/connectivity.c index bd7c703..4529742 100644 --- a/src/connectivity.c +++ b/src/connectivity.c @@ -25,27 +25,13 @@ #include #include #include -#include +#include #include #include #include -#include #include -/* - * This is an internal structure for nodes. - */ -struct NeoComm_directory_node { - int in_use; - struct sockaddr_in addr; - int sockfd; - pthread_t thread; - int open_directory; - unsigned int connections; - unsigned int max_connections; -}; - static struct NeoComm_directory_node *node_list = NULL; static unsigned int max_nodes; static unsigned int num_nodes; @@ -245,3 +231,37 @@ unsigned int NeoComm_get_directory_size() { unsigned int NeoComm_get_num_nodes() { return num_nodes; } + +int NeoComm_add_node(const struct NeoComm_address addr) { + struct hostent *host; + host = gethostbyname(addr.address); + if(!host) + { + NeoComm_error("Error getting host"); + return 0; + } + + struct NeoComm_directory_node *new_node; + for(unsigned int i = 0; i < max_nodes; ++i) + { + if(!node_list[i].in_use) + new_node = &node_list[i]; + } + + bzero((char*) &new_node->addr, sizeof(new_node->addr)); + new_node->addr.sin_family = AF_INET; + bcopy((char*) host->h_addr, + (char*) &new_node->addr.sin_addr.s_addr, + host->h_length); + new_node->addr.sin_port = htons(addr.port); + + if(connect(new_node->sockfd, (struct sockaddr*) &new_node->addr, + sizeof(new_node->addr)) < 0) + { + NeoComm_error("Failed to connect to new node"); + return 0; + } + new_node->in_use = 1; + + // TODO: listen to node on new thread +} diff --git a/src/iconnectivity.h b/src/iconnectivity.h index 4f813b4..ba54b52 100644 --- a/src/iconnectivity.h +++ b/src/iconnectivity.h @@ -19,6 +19,22 @@ #pragma once +#include +#include + +/* + * This is an internal structure for nodes. + */ +struct NeoComm_directory_node { + int in_use; + struct sockaddr_in addr; + int sockfd; + pthread_t thread; + int open_directory; + unsigned int connections; + unsigned int max_connections; +}; + int NeoComm_bind(unsigned short portnum); void *NeoComm_connect_manager();