Initial implementation of core/daemon.
This commit is contained in:
parent
cd09930077
commit
de6c4b2495
6 changed files with 132 additions and 2 deletions
|
@ -2,7 +2,7 @@ CC = gcc
|
|||
CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-protobuf -Wall
|
||||
LFLAGS =
|
||||
DEPS = builder.h ipfs_node.h
|
||||
OBJS = builder.o
|
||||
OBJS = builder.o daemon.o null.o
|
||||
|
||||
%.o: %.c $(DEPS)
|
||||
$(CC) -c -o $@ $< $(CFLAGS)
|
||||
|
|
39
core/daemon.c
Normal file
39
core/daemon.c
Normal file
|
@ -0,0 +1,39 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
#include "libp2p/net/p2pnet.h"
|
||||
#include "ipfs/core/daemon.h"
|
||||
|
||||
int ipfs_daemon (int argc, char **argv)
|
||||
{
|
||||
int count_pths = 0;
|
||||
pthread_t work_pths[MAX];
|
||||
struct null_listen_params listen_param;
|
||||
|
||||
fprintf(stderr, "Initializing daemon...\n");
|
||||
|
||||
// Set null router param
|
||||
listen_param.ipv4 = 0; // ip 0.0.0.0, all interfaces
|
||||
listen_param.port = 4001;
|
||||
|
||||
// Create pthread for ipfs_null_listen.
|
||||
if (pthread_create(&work_pths[count_pths++], NULL, ipfs_null_listen, &listen_param)) {
|
||||
fprintf(stderr, "Error creating thread for ipfs_null_listen\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
fprintf(stderr, "Daemon is ready\n");
|
||||
|
||||
// Wait for pthreads to finish.
|
||||
while (count_pths) {
|
||||
if (pthread_join(work_pths[--count_pths], NULL)) {
|
||||
fprintf(stderr, "Error joining thread\n");
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
// All pthreads aborted?
|
||||
return 0;
|
||||
}
|
63
core/null.c
Normal file
63
core/null.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
#include "libp2p/net/p2pnet.h"
|
||||
#include "ipfs/core/daemon.h"
|
||||
|
||||
void *ipfs_null_connection (void *ptr)
|
||||
{
|
||||
struct null_connection_params *connection_param;
|
||||
|
||||
connection_param = (struct null_connection_params*) ptr;
|
||||
|
||||
// TODO: multistream + secio + message.
|
||||
fprintf(stderr, "Connection %d, count %d\n", connection_param->socket, *(connection_param->count));
|
||||
|
||||
close (connection_param->socket); // close socket.
|
||||
(*(connection_param->count))--; // update counter.
|
||||
free (connection_param);
|
||||
return (void*) 1;
|
||||
}
|
||||
|
||||
void *ipfs_null_listen (void *ptr)
|
||||
{
|
||||
int socketfd, s, count = 0;
|
||||
pthread_t pth_connection;
|
||||
struct null_listen_params *listen_param;
|
||||
struct null_connection_params *connection_param;
|
||||
|
||||
listen_param = (struct null_listen_params*) ptr;
|
||||
|
||||
if ((socketfd = socket_listen(socket_tcp4(), &(listen_param->ipv4), &(listen_param->port))) <= 0) {
|
||||
perror("fail to init null router.");
|
||||
exit (1);
|
||||
}
|
||||
|
||||
fprintf(stderr, "Null listening on %d\n", listen_param->port);
|
||||
|
||||
for (;;) {
|
||||
s = socket_accept4(socketfd, &(listen_param->ipv4), &(listen_param->port));
|
||||
if (count >= CONNECTIONS) { // limit reached.
|
||||
close (s);
|
||||
continue;
|
||||
}
|
||||
|
||||
count++;
|
||||
connection_param = malloc (sizeof (struct null_connection_params));
|
||||
if (connection_param) {
|
||||
connection_param->socket = s;
|
||||
connection_param->count = &count;
|
||||
// Create pthread for ipfs_null_connection.
|
||||
if (pthread_create(&pth_connection, NULL, ipfs_null_connection, connection_param)) {
|
||||
fprintf(stderr, "Error creating thread for connection %d\n", count);
|
||||
close (s);
|
||||
} else {
|
||||
pthread_detach (pth_connection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (void*) 2;
|
||||
}
|
21
include/ipfs/core/daemon.h
Normal file
21
include/ipfs/core/daemon.h
Normal file
|
@ -0,0 +1,21 @@
|
|||
#ifndef DAEMON_H
|
||||
#define DAEMON_H
|
||||
#include <stdint.h>
|
||||
|
||||
#define MAX 5
|
||||
#define CONNECTIONS 50
|
||||
|
||||
struct null_connection_params {
|
||||
int socket;
|
||||
int *count;
|
||||
};
|
||||
|
||||
struct null_listen_params {
|
||||
uint32_t ipv4;
|
||||
uint16_t port;
|
||||
};
|
||||
|
||||
void *ipfs_null_connection (void *ptr);
|
||||
void *ipfs_null_listen (void *ptr);
|
||||
int ipfs_daemon (int argc, char **argv);
|
||||
#endif // DAEMON_H
|
|
@ -7,7 +7,7 @@ OBJS = main.o \
|
|||
../cid/cid.o \
|
||||
../cmd/ipfs/init.o \
|
||||
../commands/argument.o ../commands/command_option.o ../commands/command.o ../commands/cli/parse.o \
|
||||
../core/builder.o \
|
||||
../core/builder.o ../core/daemon.o ../core/null.o \
|
||||
../datastore/ds_helper.o \
|
||||
../datastore/key.o \
|
||||
../dnslink/dnslink.o \
|
||||
|
|
|
@ -54,6 +54,7 @@ void strip_quotes(int argc, char** argv) {
|
|||
#define OBJECT_GET 3
|
||||
#define DNS 4
|
||||
#define CAT 5
|
||||
#define DAEMON 6
|
||||
|
||||
/***
|
||||
* Basic parsing of command line arguments to figure out where the user wants to go
|
||||
|
@ -78,6 +79,9 @@ int parse_arguments(int argc, char** argv) {
|
|||
if (strcmp("dns", argv[1]) == 0) {
|
||||
return DNS;
|
||||
}
|
||||
if (strcmp("daemon", argv[1]) == 0) {
|
||||
return DAEMON;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -103,5 +107,8 @@ int main(int argc, char** argv) {
|
|||
case (DNS):
|
||||
ipfs_dns(argc, argv);
|
||||
break;
|
||||
case (DAEMON):
|
||||
ipfs_daemon(argc, argv);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue