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