From 5a91c5bbe801ed56774611c275785ab48e2ebb45 Mon Sep 17 00:00:00 2001 From: jmjatlanta Date: Wed, 2 Aug 2017 08:52:55 -0500 Subject: [PATCH] keeping track of max socket fd --- include/libp2p/peer/peer.h | 1 + include/libp2p/peer/peerstore.h | 17 ++++++++++++ peer/peerstore.c | 48 +++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/include/libp2p/peer/peer.h b/include/libp2p/peer/peer.h index 10023da..310e0f5 100644 --- a/include/libp2p/peer/peer.h +++ b/include/libp2p/peer/peer.h @@ -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; diff --git a/include/libp2p/peer/peerstore.h b/include/libp2p/peer/peerstore.h index 96c054a..b042823 100644 --- a/include/libp2p/peer/peerstore.h +++ b/include/libp2p/peer/peerstore.h @@ -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); diff --git a/peer/peerstore.c b/peer/peerstore.c index 7f0b3d8..37f2fd1 100644 --- a/peer/peerstore.c +++ b/peer/peerstore.c @@ -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; +} +