2017-04-20 22:56:03 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "ipfs/core/ipfs_node.h"
|
2017-07-27 17:05:41 +00:00
|
|
|
#include "ipfs/exchange/bitswap/bitswap.h"
|
2017-04-20 22:56:03 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
* build an online IpfsNode
|
|
|
|
* @param repo_path where the IPFS repository directory is
|
|
|
|
* @param node the completed IpfsNode struct
|
|
|
|
* @returns true(1) on success
|
|
|
|
*/
|
|
|
|
int ipfs_node_online_new(const char* repo_path, struct IpfsNode** node) {
|
|
|
|
struct FSRepo* fs_repo = NULL;
|
|
|
|
|
|
|
|
*node = (struct IpfsNode*)malloc(sizeof(struct IpfsNode));
|
|
|
|
if(*node == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
struct IpfsNode* local_node = *node;
|
2017-04-27 16:35:26 +00:00
|
|
|
local_node->identity = NULL;
|
|
|
|
local_node->peerstore = NULL;
|
|
|
|
local_node->providerstore = NULL;
|
|
|
|
local_node->repo = NULL;
|
|
|
|
local_node->routing = NULL;
|
2017-07-27 17:05:41 +00:00
|
|
|
local_node->exchange = NULL;
|
2017-04-20 22:56:03 +00:00
|
|
|
|
|
|
|
// build the struct
|
|
|
|
if (!ipfs_repo_fsrepo_new(repo_path, NULL, &fs_repo)) {
|
2017-04-27 16:35:26 +00:00
|
|
|
ipfs_node_free(local_node);
|
|
|
|
*node = NULL;
|
2017-04-20 22:56:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// open the repo
|
|
|
|
if (!ipfs_repo_fsrepo_open(fs_repo)) {
|
2017-04-27 16:35:26 +00:00
|
|
|
ipfs_node_free(local_node);
|
|
|
|
*node = NULL;
|
2017-04-20 22:56:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill in the node
|
|
|
|
local_node->repo = fs_repo;
|
|
|
|
local_node->identity = fs_repo->config->identity;
|
2017-07-31 17:50:12 +00:00
|
|
|
local_node->peerstore = libp2p_peerstore_new(local_node->identity->peer);
|
|
|
|
local_node->providerstore = libp2p_providerstore_new(fs_repo->config->datastore, local_node->identity->peer);
|
2017-07-24 19:56:30 +00:00
|
|
|
local_node->blockstore = ipfs_blockstore_new(fs_repo);
|
2017-04-20 22:56:03 +00:00
|
|
|
local_node->mode = MODE_OFFLINE;
|
2017-07-27 17:05:41 +00:00
|
|
|
local_node->routing = ipfs_routing_new_online(local_node, &fs_repo->config->identity->private_key);
|
|
|
|
local_node->exchange = ipfs_bitswap_new(local_node);
|
2017-04-20 22:56:03 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Free resources from the creation of an IpfsNode
|
|
|
|
* @param node the node to free
|
|
|
|
* @returns true(1)
|
|
|
|
*/
|
|
|
|
int ipfs_node_free(struct IpfsNode* node) {
|
|
|
|
if (node != NULL) {
|
|
|
|
if (node->providerstore != NULL)
|
|
|
|
libp2p_providerstore_free(node->providerstore);
|
|
|
|
if (node->peerstore != NULL)
|
|
|
|
libp2p_peerstore_free(node->peerstore);
|
|
|
|
if (node->repo != NULL)
|
|
|
|
ipfs_repo_fsrepo_free(node->repo);
|
|
|
|
if (node->mode == MODE_ONLINE) {
|
|
|
|
ipfs_routing_online_free(node->routing);
|
|
|
|
}
|
2017-07-26 14:48:04 +00:00
|
|
|
if (node->blockstore != NULL) {
|
|
|
|
ipfs_blockstore_free(node->blockstore);
|
|
|
|
}
|
2017-07-27 17:05:41 +00:00
|
|
|
if (node->exchange != NULL) {
|
|
|
|
node->exchange->Close(node->exchange);
|
|
|
|
}
|
2017-04-20 22:56:03 +00:00
|
|
|
free(node);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|