Reorganization of interfaces.
This commit is contained in:
@ -17,19 +17,49 @@
|
||||
* <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "neocomm/connection.h"
|
||||
#include "neocomm/connectivity.h"
|
||||
#include "iconnectivity.h"
|
||||
|
||||
#include "internal_error.h"
|
||||
#include "ierror.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <unistd.h>
|
||||
#include <strings.h>
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
/*
|
||||
* This is an internal OS specific 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;
|
||||
static unsigned int max_nodes;
|
||||
static unsigned int num_nodes;
|
||||
|
||||
static int run;
|
||||
static int sockfd;
|
||||
|
||||
static pthread_t accept_thread;
|
||||
|
||||
/*
|
||||
* This is a protected bind function that is not accessible as a public
|
||||
* interface.
|
||||
*/
|
||||
int NeoComm_bind(const unsigned short port) {
|
||||
int sockfd;//, newsockfd;
|
||||
struct sockaddr_in serv_addr;
|
||||
|
||||
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
@ -100,3 +130,71 @@ int NeoComm_bind(const unsigned short port) {
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void *NeoComm_connect_manager() {
|
||||
while(run)
|
||||
{
|
||||
int cli_sockfd;
|
||||
struct sockaddr_in cli_addr;
|
||||
socklen_t size_cli_addr = sizeof(cli_addr);
|
||||
cli_sockfd = accept(sockfd, (struct sockaddr*) &cli_addr,
|
||||
&size_cli_addr);
|
||||
if(cli_sockfd < 0)
|
||||
{
|
||||
NeoComm_error("Error on accept");
|
||||
continue;
|
||||
}
|
||||
// TODO: Add the new connection and have it listen on a new thread.
|
||||
}
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
||||
int NeoComm_init_directory(const unsigned int max_num_nodes,
|
||||
const unsigned short portnum) {
|
||||
if(max_num_nodes == 0)
|
||||
{
|
||||
NeoComm_error("Insufficient number of nodes");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(!NeoComm_bind(portnum))
|
||||
return 0;
|
||||
|
||||
node_list = calloc(max_num_nodes, sizeof(struct NeoComm_directory_node));
|
||||
if(!node_list)
|
||||
{
|
||||
NeoComm_error("Failed to allocate memory to directory node list");
|
||||
return 0;
|
||||
}
|
||||
|
||||
max_nodes = max_num_nodes;
|
||||
num_nodes = 0;
|
||||
|
||||
for(unsigned int i = 0; i < max_nodes; ++i)
|
||||
node_list[i].in_use = 0;
|
||||
|
||||
run = 1;
|
||||
int err = pthread_create(&accept_thread, NULL,
|
||||
NeoComm_connect_manager, NULL);
|
||||
if(err != 0)
|
||||
{
|
||||
NeoComm_error("Insufficient resources to create thread");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void NeoComm_shutdown_directory() {
|
||||
run = 0;
|
||||
void *res;
|
||||
for(unsigned int i = 0; i < num_nodes; ++i)
|
||||
{
|
||||
// in case the threads are still running, we wait
|
||||
pthread_join(node_list[i].thread, &res);
|
||||
close(node_list[i].sockfd);
|
||||
}
|
||||
pthread_join(accept_thread, &res);
|
||||
close(sockfd);
|
||||
pthread_exit(NULL);
|
||||
}
|
@ -18,7 +18,7 @@
|
||||
*/
|
||||
|
||||
#include "neocomm/error.h"
|
||||
#include "internal_error.h"
|
||||
#include "ierror.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
|
@ -19,11 +19,6 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "neocomm/nodes.h"
|
||||
int NeoComm_bind(unsigned short portnum);
|
||||
|
||||
// List of nodes
|
||||
extern struct NeoComm_node *node_list;
|
||||
// Maximum number of nodes
|
||||
extern unsigned int node_max;
|
||||
// Number of nodes in the list
|
||||
extern unsigned int node_count;
|
||||
void *NeoComm_connect_manager();
|
@ -18,13 +18,3 @@
|
||||
*/
|
||||
|
||||
#include "neocomm/neocomm.h"
|
||||
|
||||
int NeoComm_init(unsigned int max_nodes) {
|
||||
if(!NeoComm_init_nodes(max_nodes))
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void NeoComm_shutdown() {
|
||||
NeoComm_shutdown_nodes();
|
||||
}
|
||||
|
@ -19,13 +19,19 @@
|
||||
|
||||
#include "neocomm/nodes.h"
|
||||
|
||||
#include "globals.h"
|
||||
#include "internal_error.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
// List of nodes
|
||||
static struct NeoComm_node *node_list;
|
||||
// Maximum number of nodes
|
||||
static unsigned int node_max;
|
||||
// Number of nodes in the list
|
||||
static unsigned int node_count;
|
||||
|
||||
int NeoComm_init_nodes(unsigned int max_nodes) {
|
||||
if(max_nodes == 0)
|
||||
{
|
||||
|
Reference in New Issue
Block a user