Implemented node removal function.
This commit is contained in:
		@@ -27,9 +27,9 @@
 | 
			
		||||
#include <sys/socket.h>
 | 
			
		||||
#include <netdb.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <strings.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
 | 
			
		||||
static struct NeoComm_directory_node *node_list = NULL;
 | 
			
		||||
@@ -240,43 +240,67 @@ unsigned int NeoComm_get_num_nodes() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int NeoComm_add_node(const struct NeoComm_address addr) {
 | 
			
		||||
	struct hostent *host = NULL;
 | 
			
		||||
	host = gethostbyname(addr.address);
 | 
			
		||||
	if(num_nodes == max_nodes)
 | 
			
		||||
	{
 | 
			
		||||
		NeoComm_error("Directory full");
 | 
			
		||||
		return 0;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	struct hostent *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)
 | 
			
		||||
	unsigned int i;
 | 
			
		||||
	for(i = 0; i < max_nodes; ++i)
 | 
			
		||||
	{
 | 
			
		||||
		if(!node_list[i].in_use)
 | 
			
		||||
			new_node = &node_list[i];
 | 
			
		||||
	}
 | 
			
		||||
	if(!new_node)
 | 
			
		||||
	{
 | 
			
		||||
		NeoComm_error("No more unused slots in directory");
 | 
			
		||||
		return 0;
 | 
			
		||||
			break;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	bzero((char*) &new_node->addr, sizeof(new_node->addr));
 | 
			
		||||
	new_node->addr.sin_family = AF_INET;
 | 
			
		||||
	bzero((char*) &node_list[i].addr, sizeof(node_list[i].addr));
 | 
			
		||||
	node_list[i].addr.sin_family = AF_INET;
 | 
			
		||||
	bcopy((char*) host->h_addr_list[0],
 | 
			
		||||
			(char*) &new_node->addr.sin_addr.s_addr,
 | 
			
		||||
			(char*) &node_list[i].addr.sin_addr.s_addr,
 | 
			
		||||
			host->h_length);
 | 
			
		||||
	new_node->addr.sin_port = htons(addr.port);
 | 
			
		||||
	node_list[i].addr.sin_port = htons(addr.port);
 | 
			
		||||
 | 
			
		||||
	if(connect(new_node->sockfd,
 | 
			
		||||
				(struct sockaddr*) &new_node->addr,
 | 
			
		||||
				sizeof(new_node->addr)) < 0)
 | 
			
		||||
	if(connect(node_list[i].sockfd,
 | 
			
		||||
				(struct sockaddr*) &node_list[i].addr,
 | 
			
		||||
				sizeof(node_list[i].addr)) < 0)
 | 
			
		||||
	{
 | 
			
		||||
		NeoComm_error("Failed to connect to new node");
 | 
			
		||||
		return 0;
 | 
			
		||||
	}
 | 
			
		||||
	new_node->in_use = 1;
 | 
			
		||||
	node_list[i].in_use = 1;
 | 
			
		||||
	++num_nodes;
 | 
			
		||||
 | 
			
		||||
	// TODO: listen to node on new thread
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void NeoComm_remove_node(const struct NeoComm_address addr) {
 | 
			
		||||
	struct hostent *host = gethostbyname(addr.address);
 | 
			
		||||
	if(!host)
 | 
			
		||||
		return;
 | 
			
		||||
 | 
			
		||||
	unsigned int i;
 | 
			
		||||
	for(i = 0; i < max_nodes; ++i)
 | 
			
		||||
	{
 | 
			
		||||
		if(strncmp((char*) host->h_addr_list[0],
 | 
			
		||||
					(char*) &node_list[i].addr.sin_addr.s_addr,
 | 
			
		||||
					host->h_length) == 0)
 | 
			
		||||
			break;
 | 
			
		||||
		if(i == max_nodes - 1)
 | 
			
		||||
			++i;
 | 
			
		||||
	}
 | 
			
		||||
	if(i == max_nodes)
 | 
			
		||||
		return;
 | 
			
		||||
 | 
			
		||||
	pthread_cancel(node_list[i].thread);
 | 
			
		||||
	close(node_list[i].sockfd);
 | 
			
		||||
	node_list[i].in_use = 0;
 | 
			
		||||
	--num_nodes;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user