2017-02-20 23:53:20 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "libp2p/utils/linked_list.h"
|
|
|
|
#include "libp2p/peer/peer.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Structures and functions to implement a storage area for peers and
|
|
|
|
* their connections and metadata
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An entry in the "database" is a PeerEntry. This contains metadata
|
|
|
|
* about the peer
|
|
|
|
*/
|
|
|
|
struct PeerEntry {
|
|
|
|
struct Libp2pPeer* peer;
|
|
|
|
// other metadata for the peer goes here
|
|
|
|
// TODO: add some type of timer to expire the record
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Contains a collection of peers and their metadata
|
|
|
|
* NOTE: this is currently a linked list. Perhaps a better algo would
|
|
|
|
* improve performance, but will wait.
|
|
|
|
*/
|
|
|
|
struct Peerstore {
|
|
|
|
struct Libp2pLinkedList* head_entry;
|
|
|
|
struct Libp2pLinkedList* last_entry;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PeerEntry* libp2p_peer_entry_new();
|
|
|
|
void libp2p_peer_entry_free(struct PeerEntry* in);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new empty peerstore
|
2017-07-31 17:49:41 +00:00
|
|
|
* @param local_peer the local peer
|
2017-02-20 23:53:20 +00:00
|
|
|
* @returns an empty peerstore or NULL on error
|
|
|
|
*/
|
2017-07-31 17:49:41 +00:00
|
|
|
struct Peerstore* libp2p_peerstore_new(const struct Libp2pPeer* local_peer);
|
2017-02-20 23:53:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Deallocate resources used by the peerstore
|
|
|
|
* @param in the struct to deallocate
|
|
|
|
* @returns true(1) on success, otherwise false(0)
|
|
|
|
*/
|
|
|
|
int libp2p_peerstore_free(struct Peerstore* in);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a Peer to the Peerstore
|
|
|
|
* @param peerstore the peerstore to add the entry to
|
|
|
|
* @param peer_entry the entry to add
|
|
|
|
* @returns true(1) on success, otherwise false
|
|
|
|
*/
|
|
|
|
int libp2p_peerstore_add_peer_entry(struct Peerstore* peerstore, struct PeerEntry* peer_entry);
|
|
|
|
|
2017-02-27 17:27:14 +00:00
|
|
|
/***
|
|
|
|
* 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
|
|
|
|
*/
|
2017-07-26 12:37:28 +00:00
|
|
|
int libp2p_peerstore_add_peer(struct Peerstore* peerstore, const struct Libp2pPeer* peer);
|
2017-02-27 17:27:14 +00:00
|
|
|
|
2017-02-20 23:53:20 +00:00
|
|
|
/**
|
|
|
|
* 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 PeerEntry struct if found, otherwise NULL
|
|
|
|
*/
|
2017-04-03 16:55:03 +00:00
|
|
|
struct PeerEntry* libp2p_peerstore_get_peer_entry(struct Peerstore* peerstore, const unsigned char* peer_id, size_t peer_id_size);
|
2017-02-27 17:27:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2017-04-03 16:55:03 +00:00
|
|
|
struct Libp2pPeer* libp2p_peerstore_get_peer(struct Peerstore* peerstore, const unsigned char* peer_id, size_t peer_id_size);
|
2017-04-20 22:55:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Look for this peer in the peerstore. If it is found, return a reference to that object.
|
|
|
|
* If it is not found, add it, and return a reference to the new copy
|
|
|
|
* @param peerstore the peerstore to search
|
|
|
|
* @param in the peer to search for
|
|
|
|
*/
|
2017-07-26 12:37:28 +00:00
|
|
|
struct Libp2pPeer* libp2p_peerstore_get_or_add_peer(struct Peerstore* peerstore, const struct Libp2pPeer* in);
|
2017-04-20 22:55:18 +00:00
|
|
|
|
2017-08-02 13:52:55 +00:00
|
|
|
/***
|
|
|
|
* Look for a peer by id. If not found, add it to the peerstore
|
|
|
|
* @param peerstore the Peerstore
|
|
|
|
* @param peer_id the peer id
|
|
|
|
* @param peer_id_size the size of peer_id
|
|
|
|
* @returns a Peer struct, or NULL if error
|
|
|
|
*/
|
|
|
|
struct Libp2pPeer* libp2p_peerstore_get_or_add_peer_by_id(struct Peerstore* peerstore, const unsigned char* peer_id, size_t peer_id_size);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the max socket fd
|
|
|
|
* @param peerstore the Peerstore
|
|
|
|
* @param newVal the new val (will be added if > oldVal)
|
|
|
|
* @returns the lastest peerstore max_socket_fd
|
|
|
|
*/
|
|
|
|
int libp2p_peerstore_update_socket_fd(struct Peerstore* peerstore, int newVal);
|