2017-02-10 01:10:21 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include "libp2p/net/p2pnet.h"
|
2017-02-22 16:48:42 +00:00
|
|
|
#include "libp2p/peer/peerstore.h"
|
2017-02-10 01:10:21 +00:00
|
|
|
#include "ipfs/core/daemon.h"
|
2017-02-22 15:56:11 +00:00
|
|
|
#include "ipfs/core/ipfs_node.h"
|
2017-03-09 17:50:08 +00:00
|
|
|
#include "ipfs/core/bootstrap.h"
|
2017-02-22 16:48:42 +00:00
|
|
|
#include "ipfs/repo/fsrepo/fs_repo.h"
|
2017-02-10 01:10:21 +00:00
|
|
|
|
2017-02-27 17:27:40 +00:00
|
|
|
int ipfs_daemon_start(char* repo_path) {
|
2017-02-10 01:10:21 +00:00
|
|
|
int count_pths = 0;
|
|
|
|
pthread_t work_pths[MAX];
|
2017-02-22 15:56:11 +00:00
|
|
|
struct IpfsNodeListenParams listen_param;
|
2017-02-10 01:10:21 +00:00
|
|
|
|
|
|
|
fprintf(stderr, "Initializing daemon...\n");
|
|
|
|
|
2017-02-22 16:48:42 +00:00
|
|
|
// read the configuration
|
|
|
|
struct FSRepo* fs_repo;
|
2017-02-27 17:27:40 +00:00
|
|
|
if (!ipfs_repo_fsrepo_new(repo_path, NULL, &fs_repo))
|
2017-02-22 16:48:42 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
// open the repository and read the file
|
|
|
|
if (!ipfs_repo_fsrepo_open(fs_repo)) {
|
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-22 15:56:11 +00:00
|
|
|
// create a new IpfsNode
|
|
|
|
struct IpfsNode local_node;
|
|
|
|
local_node.mode = MODE_ONLINE;
|
2017-02-22 16:48:42 +00:00
|
|
|
local_node.peerstore = libp2p_peerstore_new();
|
2017-03-24 19:29:00 +00:00
|
|
|
local_node.providerstore = libp2p_providerstore_new();
|
2017-02-22 16:48:42 +00:00
|
|
|
local_node.repo = fs_repo;
|
|
|
|
local_node.identity = fs_repo->config->identity;
|
2017-02-22 15:56:11 +00:00
|
|
|
|
2017-02-10 01:10:21 +00:00
|
|
|
// Set null router param
|
|
|
|
listen_param.ipv4 = 0; // ip 0.0.0.0, all interfaces
|
|
|
|
listen_param.port = 4001;
|
2017-02-22 15:56:11 +00:00
|
|
|
listen_param.local_node = &local_node;
|
2017-02-10 01:10:21 +00:00
|
|
|
|
2017-03-09 23:03:21 +00:00
|
|
|
// Create pthread for swarm listener.
|
2017-02-10 01:10:21 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-03-09 23:03:21 +00:00
|
|
|
// create pthread for the API
|
2017-03-24 19:29:00 +00:00
|
|
|
//ipfs_bootstrap_routing(&local_node);
|
2017-03-09 23:03:21 +00:00
|
|
|
if (pthread_create(&work_pths[count_pths++], NULL, ipfs_bootstrap_routing, &local_node)) {
|
|
|
|
fprintf(stderr, "Error creating thread for routing\n");
|
2017-03-09 17:50:08 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 01:10:21 +00:00
|
|
|
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;
|
2017-02-27 17:27:40 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int ipfs_daemon (int argc, char **argv)
|
|
|
|
{
|
|
|
|
return ipfs_daemon_start(NULL);
|
2017-02-10 01:10:21 +00:00
|
|
|
}
|