Better handling of locally stored files

This commit is contained in:
John Jones 2017-05-11 07:04:28 -05:00
parent 910e84dd5c
commit ff4be03782
5 changed files with 15 additions and 5 deletions

View file

@ -33,9 +33,10 @@ void libp2p_peer_entry_free(struct PeerEntry* in);
/** /**
* Creates a new empty peerstore * Creates a new empty peerstore
* @param peer_id the peer id as a null terminated string
* @returns an empty peerstore or NULL on error * @returns an empty peerstore or NULL on error
*/ */
struct Peerstore* libp2p_peerstore_new(); struct Peerstore* libp2p_peerstore_new(const char* peer_id);
/** /**
* Deallocate resources used by the peerstore * Deallocate resources used by the peerstore

View file

@ -32,6 +32,6 @@ struct ProviderStore* libp2p_providerstore_new();
*/ */
void libp2p_providerstore_free(struct ProviderStore* in); void libp2p_providerstore_free(struct ProviderStore* in);
int libp2p_providerstore_add(struct ProviderStore* store, unsigned char* hash, int hash_size, unsigned char* peer_id, int peer_id_size); int libp2p_providerstore_add(struct ProviderStore* store, const unsigned char* hash, int hash_size, const unsigned char* peer_id, int peer_id_size);
int libp2p_providerstore_get(struct ProviderStore* store, const unsigned char* hash, int hash_size, unsigned char** peer_id, int *peer_id_size); int libp2p_providerstore_get(struct ProviderStore* store, const unsigned char* hash, int hash_size, unsigned char** peer_id, int *peer_id_size);

View file

@ -33,13 +33,22 @@ struct PeerEntry* libp2p_peer_entry_copy(struct PeerEntry* in) {
/** /**
* Creates a new empty peerstore * Creates a new empty peerstore
* @param peer_id the peer id as a null terminated string
* @returns an empty peerstore or NULL on error * @returns an empty peerstore or NULL on error
*/ */
struct Peerstore* libp2p_peerstore_new() { struct Peerstore* libp2p_peerstore_new(const char* peer_id) {
struct Peerstore* out = (struct Peerstore*)malloc(sizeof(struct Peerstore)); struct Peerstore* out = (struct Peerstore*)malloc(sizeof(struct Peerstore));
if (out != NULL) { if (out != NULL) {
out->head_entry = NULL; out->head_entry = NULL;
out->last_entry = NULL; out->last_entry = NULL;
// now add this peer as the first entry
struct Libp2pPeer* peer = libp2p_peer_new();
peer->connection_type = CONNECTION_TYPE_NOT_CONNECTED;
peer->id_size = strlen(peer_id);
peer->id = malloc(peer->id_size);
memcpy(peer->id, peer_id, peer->id_size);
libp2p_peerstore_add_peer(out, peer);
libp2p_peer_free(peer);
} }
return out; return out;
} }

View file

@ -61,7 +61,7 @@ void libp2p_providerstore_free(struct ProviderStore* in) {
} }
} }
int libp2p_providerstore_add(struct ProviderStore* store, unsigned char* hash, int hash_size, unsigned char* peer_id, int peer_id_size) { int libp2p_providerstore_add(struct ProviderStore* store, unsigned char* hash, int hash_size, const unsigned char* peer_id, int peer_id_size) {
char hash_str[hash_size + 1]; char hash_str[hash_size + 1];
memcpy(hash_str, hash, hash_size); memcpy(hash_str, hash, hash_size);
hash_str[hash_size] = 0; hash_str[hash_size] = 0;

View file

@ -24,7 +24,7 @@ int test_peer() {
* Test the peerstore * Test the peerstore
*/ */
int test_peerstore() { int test_peerstore() {
struct Peerstore* peerstore = libp2p_peerstore_new(); struct Peerstore* peerstore = libp2p_peerstore_new("Qmabcdefg");
struct PeerEntry* peer_entry = NULL; struct PeerEntry* peer_entry = NULL;
struct PeerEntry* results = NULL; struct PeerEntry* results = NULL;
int retVal = 0; int retVal = 0;