Reading replication parameters from config file

This commit is contained in:
jmjatlanta 2017-07-20 09:12:31 -05:00
parent 6e19c14bab
commit 1fe5be1c5c
10 changed files with 104 additions and 9 deletions

View file

@ -3,6 +3,6 @@
* smartly handle queues of local and remote requests.
*/
#include "ipfs/bitswap/network.h"
#include "ipfs/exchange/bitswap/network.h"

View file

@ -7,6 +7,7 @@
#include "bootstrap_peers.h"
#include "addresses.h"
#include "gateway.h"
#include "replication.h"
struct MDNS {
int enabled;
@ -44,6 +45,7 @@ struct RepoConfig {
//struct supernode_routing supernode_client_config;
//struct api api;
struct Reprovider reprovider;
struct Replication* replication;
};
/**

View file

@ -0,0 +1,20 @@
#include "libp2p/utils/vector.h"
struct Replication {
int announce_minutes;
struct Libp2pVector* nodes;
};
/***
* allocate memory and initialize the replication struct
* @param replication a pointer to the struct to be allocated
* @returns true(1) on success, false(0) otherwise
*/
int repo_config_replication_new(struct Replication** replication);
/***
* Frees memory of a replication struct
* @param replication the replication struct
* @returns true(1);
*/
int repo_config_replication_free(struct Replication* replication);

View file

@ -20,9 +20,7 @@ OBJS = main.o \
../pin/pin.o \
../repo/init.o \
../repo/fsrepo/fs_repo.o ../repo/fsrepo/jsmn.o ../repo/fsrepo/lmdb_datastore.o \
../repo/config/config.o ../repo/config/identity.o \
../repo/config/bootstrap_peers.o ../repo/config/gateway.o \
../repo/config/addresses.o ../repo/config/swarm.o ../repo/config/peer.o \
../repo/config/*.o \
../routing/*.o \
../thirdparty/ipfsaddr/ipfs_addr.o \
../unixfs/unixfs.o \

View file

@ -6,8 +6,8 @@ CFLAGS += -g3
endif
LFLAGS =
DEPS = config.h datastore.h identity.h
OBJS = config.o identity.o bootstrap_peers.o gateway.o addresses.o swarm.o peer.o
DEPS = config.h datastore.h identity.h replication.h
OBJS = config.o identity.o bootstrap_peers.o gateway.o addresses.o swarm.o peer.o replication.o
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)

View file

@ -201,6 +201,9 @@ int ipfs_repo_config_new(struct RepoConfig** config) {
if (retVal == 0)
return 0;
if (!repo_config_replication_new(&((*config)->replication)))
return 0;
return 1;
}
@ -223,6 +226,8 @@ int ipfs_repo_config_free(struct RepoConfig* config) {
repo_config_addresses_free(config->addresses);
if (config->gateway != NULL)
repo_config_gateway_free(config->gateway);
if (config->replication != NULL)
repo_config_replication_free(config->replication);
free(config);
}
return 1;

39
repo/config/replication.c Normal file
View file

@ -0,0 +1,39 @@
#include <stdlib.h>
#include "multiaddr/multiaddr.h"
#include "ipfs/repo/config/replication.h"
/***
* allocate memory and initialize the replication struct
* @param replication a pointer to the struct to be allocated
* @returns true(1) on success, false(0) otherwise
*/
int repo_config_replication_new(struct Replication** replication) {
*replication = (struct Replication*)malloc(sizeof(struct Replication));
if (*replication == NULL)
return 0;
struct Replication* out = *replication;
out->announce_minutes = 0;
out->nodes = NULL;
return 1;
}
/***
* Frees memory of a replication struct
* @param replication the replication struct
* @returns true(1);
*/
int repo_config_replication_free(struct Replication* replication) {
if (replication != NULL) {
// free the vector
if (replication->nodes != NULL) {
for(int i = 0; i < replication->nodes->total; i++) {
struct MultiAddress* currAddr = libp2p_utils_vector_get(replication->nodes, i);
multiaddress_free(currAddr);
}
libp2p_utils_vector_free(replication->nodes);
}
// free the struct
free(replication);
}
return 1;
}

View file

@ -471,6 +471,35 @@ int fs_repo_open_config(struct FSRepo* repo) {
free(val);
}
}
// replication
curr_pos = _find_token(data, tokens, num_tokens, curr_pos, "Replication");
if (curr_pos >= 0) {
// announce minutes
curr_pos++;
_get_json_int_value(data, tokens, num_tokens, curr_pos, "AnnounceMinutes", &repo->config->replication->announce_minutes);
// nodes list
int nodes_pos = _find_token(data, tokens, num_tokens, curr_pos, "Nodes");
if (nodes_pos >= 0) {
if (tokens[nodes_pos].type != JSMN_ARRAY) {
free(data);
return 0;
}
int nodes_size = tokens[nodes_pos].size;
repo->config->replication->nodes = libp2p_utils_vector_new(nodes_size);
nodes_pos++;
for(int i = 0; i < nodes_size; i++) {
char* val = NULL;
if (!_get_json_string_value(data, tokens, num_tokens, nodes_pos + i, NULL, &val))
break;
struct MultiAddress* cur = multiaddress_new_from_string(val);
if (cur == NULL)
continue;
libp2p_utils_vector_add(repo->config->replication->nodes, cur);
free(val);
}
}
}
// free the memory used reading the json file
free(data);
free(priv_key_base64);

View file

@ -6,6 +6,10 @@
#include "ipfs/repo/config/config.h"
#include "ipfs/repo/fsrepo/fs_repo.h"
/**
* The basic functions for initializing an IPFS repo
*/
/**
* Get the correct repo home directory. This first looks at the
* command line, then the IPFS_PATH environment variable,

View file

@ -20,9 +20,7 @@ OBJS = testit.o test_helper.o \
../multibase/multibase.o \
../repo/init.o \
../repo/fsrepo/fs_repo.o ../repo/fsrepo/jsmn.o ../repo/fsrepo/lmdb_datastore.o \
../repo/config/config.o ../repo/config/identity.o \
../repo/config/bootstrap_peers.o ../repo/config/gateway.o \
../repo/config/addresses.o ../repo/config/swarm.o ../repo/config/peer.o \
../repo/config/*.o \
../routing/offline.o \
../routing/online.o \
../routing/k_routing.o \