Adding implementation for adding new node.

This commit is contained in:
Nicolás Ortega Froysa 2017-08-05 15:41:01 -05:00
parent 03156e45de
commit afe5f6ae4e
No known key found for this signature in database
GPG Key ID: FEC70E3BAE2E69BF
3 changed files with 52 additions and 19 deletions

View File

@ -104,11 +104,8 @@ int NeoComm_add_node(const struct NeoComm_address addr);
* @brief Disconnect and remove a node from the directory. * @brief Disconnect and remove a node from the directory.
* *
* @param addr The address of the node to remove. * @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. * @brief Retrieve information on a specific node.

View File

@ -25,27 +25,13 @@
#include <errno.h> #include <errno.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netdb.h>
#include <unistd.h> #include <unistd.h>
#include <strings.h> #include <strings.h>
#include <stdio.h> #include <stdio.h>
#include <pthread.h>
#include <stdlib.h> #include <stdlib.h>
/*
* 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 struct NeoComm_directory_node *node_list = NULL;
static unsigned int max_nodes; static unsigned int max_nodes;
static unsigned int num_nodes; static unsigned int num_nodes;
@ -245,3 +231,37 @@ unsigned int NeoComm_get_directory_size() {
unsigned int NeoComm_get_num_nodes() { unsigned int NeoComm_get_num_nodes() {
return 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
}

View File

@ -19,6 +19,22 @@
#pragma once #pragma once
#include <netinet/in.h>
#include <pthread.h>
/*
* 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); int NeoComm_bind(unsigned short portnum);
void *NeoComm_connect_manager(); void *NeoComm_connect_manager();