From ff4be037820eaf29dd286a0525225bae493e0b4b Mon Sep 17 00:00:00 2001 From: John Jones Date: Thu, 11 May 2017 07:04:28 -0500 Subject: [PATCH] Better handling of locally stored files --- include/libp2p/peer/peerstore.h | 3 ++- include/libp2p/peer/providerstore.h | 2 +- peer/peerstore.c | 11 ++++++++++- peer/providerstore.c | 2 +- test/test_peer.h | 2 +- 5 files changed, 15 insertions(+), 5 deletions(-) diff --git a/include/libp2p/peer/peerstore.h b/include/libp2p/peer/peerstore.h index 6073486..47863ea 100644 --- a/include/libp2p/peer/peerstore.h +++ b/include/libp2p/peer/peerstore.h @@ -33,9 +33,10 @@ void libp2p_peer_entry_free(struct PeerEntry* in); /** * Creates a new empty peerstore + * @param peer_id the peer id as a null terminated string * @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 diff --git a/include/libp2p/peer/providerstore.h b/include/libp2p/peer/providerstore.h index 9ded207..e79bb66 100644 --- a/include/libp2p/peer/providerstore.h +++ b/include/libp2p/peer/providerstore.h @@ -32,6 +32,6 @@ struct ProviderStore* libp2p_providerstore_new(); */ 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); diff --git a/peer/peerstore.c b/peer/peerstore.c index 6af0b1e..f32e4e9 100644 --- a/peer/peerstore.c +++ b/peer/peerstore.c @@ -33,13 +33,22 @@ struct PeerEntry* libp2p_peer_entry_copy(struct PeerEntry* in) { /** * Creates a new empty peerstore + * @param peer_id the peer id as a null terminated string * @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)); if (out != NULL) { out->head_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; } diff --git a/peer/providerstore.c b/peer/providerstore.c index 15b9a44..fabbc19 100644 --- a/peer/providerstore.c +++ b/peer/providerstore.c @@ -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]; memcpy(hash_str, hash, hash_size); hash_str[hash_size] = 0; diff --git a/test/test_peer.h b/test/test_peer.h index 1a6ca48..31668b2 100644 --- a/test/test_peer.h +++ b/test/test_peer.h @@ -24,7 +24,7 @@ int test_peer() { * Test the peerstore */ int test_peerstore() { - struct Peerstore* peerstore = libp2p_peerstore_new(); + struct Peerstore* peerstore = libp2p_peerstore_new("Qmabcdefg"); struct PeerEntry* peer_entry = NULL; struct PeerEntry* results = NULL; int retVal = 0;