2017-07-20 14:12:31 +00:00
|
|
|
#include <stdlib.h>
|
2017-08-28 11:56:22 +00:00
|
|
|
|
2017-07-20 14:12:31 +00:00
|
|
|
#include "multiaddr/multiaddr.h"
|
2017-08-28 11:56:22 +00:00
|
|
|
#include "libp2p/utils/linked_list.h"
|
2017-08-30 16:10:14 +00:00
|
|
|
#include "libp2p/utils/logger.h"
|
2017-07-20 14:12:31 +00:00
|
|
|
#include "ipfs/repo/config/replication.h"
|
|
|
|
|
2017-08-28 11:56:22 +00:00
|
|
|
/**
|
|
|
|
* Allocate a new ReplicationPeer struct
|
|
|
|
* @returns the new struct
|
|
|
|
*/
|
|
|
|
struct ReplicationPeer* repo_config_replication_peer_new() {
|
|
|
|
struct ReplicationPeer* rp = (struct ReplicationPeer*) malloc(sizeof(struct ReplicationPeer));
|
|
|
|
if (rp != NULL) {
|
|
|
|
rp->lastConnect = 0;
|
|
|
|
rp->lastJournalTime = 0;
|
|
|
|
rp->peer = NULL;
|
|
|
|
}
|
|
|
|
return rp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Free the resources of a ReplicationPeer
|
|
|
|
* @param rp the ReplicationPeer to free
|
|
|
|
* @returns true(1)
|
|
|
|
*/
|
|
|
|
int repo_config_replication_peer_free(struct ReplicationPeer* rp) {
|
|
|
|
if (rp != NULL) {
|
|
|
|
// we allocated the peer structure, so we must remove it
|
|
|
|
libp2p_peer_free(rp->peer);
|
|
|
|
free(rp);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-07-20 14:12:31 +00:00
|
|
|
/***
|
|
|
|
* 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;
|
2017-08-28 11:56:22 +00:00
|
|
|
out->replication_peers = NULL;
|
2017-07-20 14:12:31 +00:00
|
|
|
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
|
2017-08-28 11:56:22 +00:00
|
|
|
if (replication->replication_peers != NULL) {
|
|
|
|
for(int i = 0; i < replication->replication_peers->total; i++) {
|
|
|
|
struct ReplicationPeer* currAddr = (struct ReplicationPeer*)libp2p_utils_vector_get(replication->replication_peers, i);
|
|
|
|
repo_config_replication_peer_free(currAddr);
|
2017-07-20 14:12:31 +00:00
|
|
|
}
|
2017-08-28 11:56:22 +00:00
|
|
|
libp2p_utils_vector_free(replication->replication_peers);
|
2017-07-20 14:12:31 +00:00
|
|
|
}
|
|
|
|
// free the struct
|
|
|
|
free(replication);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2017-08-28 11:56:22 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
* Given a peer, return the ReplicationPeer record
|
|
|
|
* @param replication the context
|
|
|
|
* @param key the peer to look for
|
|
|
|
* @returns the ReplicationPeer struct, or NULL if not found
|
|
|
|
*/
|
|
|
|
struct ReplicationPeer* repo_config_get_replication_peer(struct Replication* replication, struct Libp2pPeer* key) {
|
|
|
|
if (replication != NULL) {
|
|
|
|
if (replication->replication_peers != NULL) {
|
|
|
|
for(int i = 0; i < replication->replication_peers->total; i++) {
|
|
|
|
struct ReplicationPeer* confAddr = (struct ReplicationPeer*) libp2p_utils_vector_get(replication->replication_peers, i);
|
2017-08-30 16:10:14 +00:00
|
|
|
if (libp2p_peer_matches_id(confAddr->peer, (unsigned char*)key->id, key->id_size)) {
|
2017-08-28 11:56:22 +00:00
|
|
|
return confAddr;
|
2017-08-30 16:10:14 +00:00
|
|
|
}
|
2017-08-28 11:56:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Determines if this peer is one of our approved replication nodes
|
|
|
|
* @param replication the replication struct
|
|
|
|
* @param peer the peer to examine
|
|
|
|
* @returns true(1) if this peer should be kept syncronized, false(0) otherwise
|
|
|
|
*/
|
|
|
|
int repo_config_replication_approved_node(struct Replication* replication, struct Libp2pPeer* peer) {
|
|
|
|
if (replication != NULL) {
|
|
|
|
if (repo_config_get_replication_peer(replication, peer) != NULL) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine the last time a replication was successful for this peer (at least we sent it something without error)
|
|
|
|
* @param replication the replication struct
|
|
|
|
* @param peer the peer to examine
|
|
|
|
* @returns the time since the last replication, or the announce time if we have no record
|
|
|
|
*/
|
2017-08-30 16:10:14 +00:00
|
|
|
/*
|
2017-08-28 11:56:22 +00:00
|
|
|
unsigned long long repo_config_replication_last_attempt(struct Replication* replication, struct Libp2pPeer* peer) {
|
|
|
|
struct ReplicationPeer* rp = repo_config_get_replication_peer(replication, peer);
|
|
|
|
if (rp != NULL) {
|
|
|
|
return rp->lastConnect;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2017-08-30 16:10:14 +00:00
|
|
|
*/
|
2017-08-28 11:56:22 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
* Determine the last journal record time that was sent to this peer
|
|
|
|
* @param replication the replication struct
|
|
|
|
* @param peer the peer to examine
|
|
|
|
* @returns the time of the last journal entry successfully sent
|
|
|
|
*/
|
|
|
|
unsigned long long repo_config_replication_last_journal_time(struct Replication* replication, struct Libp2pPeer* peer) {
|
|
|
|
struct ReplicationPeer* rp = repo_config_get_replication_peer(replication, peer);
|
|
|
|
if (rp != NULL) {
|
|
|
|
return rp->lastJournalTime;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|