keeping track of max socket fd

This commit is contained in:
jmjatlanta 2017-08-02 08:52:55 -05:00
parent 3137d9efa6
commit 5a91c5bbe8
3 changed files with 66 additions and 0 deletions

View file

@ -3,6 +3,7 @@
#include "multiaddr/multiaddr.h"
#include "libp2p/net/stream.h"
#include "libp2p/crypto/rsa.h"
#include "libp2p/conn/session.h"
struct Peerstore;

View file

@ -24,6 +24,7 @@ struct PeerEntry {
* improve performance, but will wait.
*/
struct Peerstore {
int max_socket_fd;
struct Libp2pLinkedList* head_entry;
struct Libp2pLinkedList* last_entry;
};
@ -87,3 +88,19 @@ struct Libp2pPeer* libp2p_peerstore_get_peer(struct Peerstore* peerstore, const
*/
struct Libp2pPeer* libp2p_peerstore_get_or_add_peer(struct Peerstore* peerstore, const struct Libp2pPeer* in);
/***
* 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);

View file

@ -52,6 +52,7 @@ struct PeerEntry* libp2p_peer_entry_copy(struct PeerEntry* in) {
struct Peerstore* libp2p_peerstore_new(const struct Libp2pPeer* local_peer) {
struct Peerstore* out = (struct Peerstore*)malloc(sizeof(struct Peerstore));
if (out != NULL) {
out->max_socket_fd = 0;
out->head_entry = NULL;
out->last_entry = NULL;
// now add this peer as the first entry
@ -138,6 +139,11 @@ int libp2p_peerstore_add_peer(struct Peerstore* peerstore, const struct Libp2pPe
if (peer_entry->peer == NULL)
return 0;
retVal = libp2p_peerstore_add_peer_entry(peerstore, peer_entry);
// recalculate max_session_fd
if (peer->sessionContext != NULL && peer->sessionContext->default_stream != NULL) {
int sd = *((int*)peer->sessionContext->default_stream->socket_descriptor);
libp2p_peerstore_update_socket_fd(peerstore, sd);
}
libp2p_logger_debug("peerstore", "Adding peer %s to peerstore was a success\n", peer->id);
}
return retVal;
@ -181,6 +187,35 @@ struct Libp2pPeer* libp2p_peerstore_get_peer(struct Peerstore* peerstore, const
return entry->peer;
}
/***
* 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) {
if (peer_id_size == 0)
return NULL;
struct PeerEntry* entry = libp2p_peerstore_get_peer_entry(peerstore, peer_id, peer_id_size);
if (entry == NULL) {
// add it
struct Libp2pPeer* temp_peer = libp2p_peer_new();
temp_peer->id_size = peer_id_size;
temp_peer->id = (char*)peer_id;
libp2p_peerstore_add_peer(peerstore, temp_peer);
libp2p_peer_free(temp_peer);
entry = libp2p_peerstore_get_peer_entry(peerstore, peer_id, peer_id_size);
}
if (entry == NULL)
return NULL;
return entry->peer;
}
/**
* 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
@ -198,3 +233,16 @@ struct Libp2pPeer* libp2p_peerstore_get_or_add_peer(struct Peerstore* peerstore,
return libp2p_peerstore_get_peer(peerstore, (unsigned char*)in->id, in->id_size);
}
/**
* Update the max socket fd
* @param peerstore the Peerstore
* @param newVal the new val (will be added if > oldVal)
* @returns the latest peerstore->max_socket_fd
*/
int libp2p_peerstore_update_socket_fd(struct Peerstore* peerstore, int newVal) {
if (newVal > peerstore->max_socket_fd)
peerstore->max_socket_fd = newVal;
return peerstore->max_socket_fd;
}