Starting to handle get_value request

yamux
John Jones 2017-02-27 12:27:14 -05:00
parent f1aaae0f17
commit c5fa775534
4 changed files with 90 additions and 0 deletions

View File

@ -1,5 +1,7 @@
#pragma once
#include "multiaddr/multiaddr.h"
enum ConnectionType {
// sender does not have a connection to the peer, and no extra information (default)
CONNECTION_TYPE_NOT_CONNECTED = 0,
@ -24,6 +26,15 @@ struct Libp2pPeer {
*/
struct Libp2pPeer* libp2p_peer_new();
/**
* Create a new peer struct with some data
* @param id the id
* @param id_size the length of the id
* @param multi_addr the MultiAddresss
* @returns the Libp2pPeer or NULL if there was a problem
*/
struct Libp2pPeer* libp2p_peer_new_from_data(const char* id, size_t id_size, const struct MultiAddress* multi_addr);
/**
* frees resources from a peer struct
* @param in the peer to free

View File

@ -52,6 +52,14 @@ int libp2p_peerstore_free(struct Peerstore* in);
*/
int libp2p_peerstore_add_peer_entry(struct Peerstore* peerstore, struct PeerEntry* peer_entry);
/***
* Add a peer to the peerstore
* @param peerstore the peerstore to add the entry to
* @param peer the peer to add (will be wrapped in PeerEntry struct)
* @returns true(1) on success, otherwise false
*/
int libp2p_peerstore_add_peer(struct Peerstore* peerstore, struct Libp2pPeer* peer);
/**
* Retrieve a peer from the peerstore based on the peer id
* @param peerstore the peerstore to search
@ -60,3 +68,12 @@ int libp2p_peerstore_add_peer_entry(struct Peerstore* peerstore, struct PeerEntr
* @returns the PeerEntry struct if found, otherwise NULL
*/
struct PeerEntry* libp2p_peerstore_get_peer_entry(struct Peerstore* peerstore, unsigned char* peer_id, size_t peer_id_size);
/**
* Retrieve a peer from the peerstore based on the peer id
* @param peerstore the peerstore to search
* @param peer_id the id to search for as a binary array
* @param peer_id_size the size of the binary array
* @returns the Libp2pPeer struct if found, otherwise NULL
*/
struct Libp2pPeer* libp2p_peerstore_get_peer(struct Peerstore* peerstore, unsigned char* peer_id, size_t peer_id_size);

View File

@ -20,6 +20,35 @@ struct Libp2pPeer* libp2p_peer_new() {
return out;
}
/**
* Create a new peer struct with some data
* @param id the id
* @param id_size the length of the id
* @param multi_addr the MultiAddresss
* @returns the Libp2pPeer or NULL if there was a problem
*/
struct Libp2pPeer* libp2p_peer_new_from_data(const char* id, size_t id_size, const struct MultiAddress* multi_addr) {
struct Libp2pPeer* out = libp2p_peer_new();
if (out != NULL) {
out->id = malloc(id_size);
strncpy(out->id, id, id_size);
out->id_size = id_size;
out->addr_head = libp2p_utils_linked_list_new();
if (out->addr_head == NULL) {
libp2p_peer_free(out);
return NULL;
}
out->addr_head->item = multiaddress_copy(multi_addr);
if (out->addr_head->item == NULL) {
libp2p_peer_free(out);
return NULL;
}
}
return out;
}
void libp2p_peer_free(struct Libp2pPeer* in) {
if (in != NULL) {
if (in->id != NULL)

View File

@ -88,6 +88,25 @@ int libp2p_peerstore_add_peer_entry(struct Peerstore* peerstore, struct PeerEntr
return 1;
}
/***
* Add a peer to the peerstore
* @param peerstore the peerstore to add the entry to
* @param peer the peer to add (will be wrapped in PeerEntry struct)
* @returns true(1) on success, otherwise false
*/
int libp2p_peerstore_add_peer(struct Peerstore* peerstore, struct Libp2pPeer* peer) {
struct PeerEntry* peer_entry = libp2p_peer_entry_new();
if (peer_entry == NULL) {
return 0;
}
peer_entry->peer = libp2p_peer_copy(peer);
if (peer_entry->peer == NULL)
return 0;
int retVal = libp2p_peerstore_add_peer_entry(peerstore, peer_entry);
libp2p_peer_entry_free(peer_entry);
return retVal;
}
/**
* Retrieve a peer from the peerstore based on the peer id
* @param peerstore the peerstore to search
@ -107,3 +126,17 @@ struct PeerEntry* libp2p_peerstore_get_peer_entry(struct Peerstore* peerstore, u
}
return NULL;
}
/**
* Retrieve a peer from the peerstore based on the peer id
* @param peerstore the peerstore to search
* @param peer_id the id to search for as a binary array
* @param peer_id_size the size of the binary array
* @returns the Peer struct if found, otherwise NULL
*/
struct Libp2pPeer* libp2p_peerstore_get_peer(struct Peerstore* peerstore, unsigned char* peer_id, size_t peer_id_size) {
struct PeerEntry* entry = libp2p_peerstore_get_peer_entry(peerstore, peer_id, peer_id_size);
if (entry == NULL)
return NULL;
return entry->peer;
}