From 76e6831168a25bc3aba8802f1586d0a0ab0dc4a2 Mon Sep 17 00:00:00 2001 From: Determinant Date: Thu, 13 Jun 2019 16:51:35 -0400 Subject: add del_peer --- include/salticidae/conn.h | 2 +- include/salticidae/network.h | 31 ++++++++++++++++++++++++++++--- include/salticidae/util.h | 2 +- 3 files changed, 30 insertions(+), 5 deletions(-) (limited to 'include') diff --git a/include/salticidae/conn.h b/include/salticidae/conn.h index 546df5f..62825db 100644 --- a/include/salticidae/conn.h +++ b/include/salticidae/conn.h @@ -266,7 +266,7 @@ class ConnPool { protected: conn_t _connect(const NetAddr &addr); void _listen(NetAddr listen_addr); - void recoverable_error(const std::exception_ptr err) { + void recoverable_error(const std::exception_ptr err) const { user_tcall->async_call([this, err](ThreadCall::Handle &) { if (error_cb) { try { diff --git a/include/salticidae/network.h b/include/salticidae/network.h index 14d270f..1095c89 100644 --- a/include/salticidae/network.h +++ b/include/salticidae/network.h @@ -426,6 +426,7 @@ class PeerNetwork: public MsgNetwork { ~PeerNetwork() { this->stop_workers(); } void add_peer(const NetAddr &paddr); + void del_peer(const NetAddr &paddr); bool has_peer(const NetAddr &paddr) const; const conn_t get_peer_conn(const NetAddr &paddr) const; using MsgNet::send_msg; @@ -725,6 +726,23 @@ void PeerNetwork::add_peer(const NetAddr &addr) { throw PeerNetworkError(SALTI_ERROR_PEER_ALREADY_EXISTS); id2peer.insert(std::make_pair(addr, new Peer(addr, nullptr, this->disp_ec))); start_active_conn(addr); + } catch (const PeerNetworkError &) { + this->recoverable_error(std::current_exception()); + } catch (...) { this->disp_error_cb(std::current_exception()); } + }); +} + +template +void PeerNetwork::del_peer(const NetAddr &addr) { + this->disp_tcall->async_call([this, addr](ThreadCall::Handle &) { + try { + auto it = id2peer.find(addr); + if (it == id2peer.end()) + throw PeerNetworkError(SALTI_ERROR_PEER_NOT_EXIST); + it->second->conn->disp_terminate(); + id2peer.erase(it); + } catch (const PeerNetworkError &) { + this->recoverable_error(std::current_exception()); } catch (...) { this->disp_error_cb(std::current_exception()); } }); } @@ -739,8 +757,10 @@ PeerNetwork::get_peer_conn(const NetAddr &paddr) const { try { auto it = id2peer.find(paddr); if (it == id2peer.end()) - throw PeerNetworkError(SALTI_ERROR_PEER_NOT_EXISTS); + throw PeerNetworkError(SALTI_ERROR_PEER_NOT_EXIST); conn = it->second->conn; + } catch (const PeerNetworkError &) { + this->recoverable_error(std::current_exception()); } catch (...) { err = std::current_exception(); } @@ -771,8 +791,10 @@ void PeerNetwork::_send_msg(Msg &&msg, const NetAddr &paddr) { try { auto it = id2peer.find(paddr); if (it == id2peer.end()) - throw PeerNetworkError(SALTI_ERROR_PEER_NOT_EXISTS); + throw PeerNetworkError(SALTI_ERROR_PEER_NOT_EXIST); this->_send_msg_dispatcher(msg, it->second->conn); + } catch (const PeerNetworkError &) { + this->recoverable_error(std::current_exception()); } catch (...) { this->recoverable_error(std::current_exception()); } }); } @@ -792,9 +814,11 @@ void PeerNetwork::_multicast_msg(Msg &&msg, const std::vector { auto it = id2peer.find(addr); if (it == id2peer.end()) - throw PeerNetworkError(SALTI_ERROR_PEER_NOT_EXISTS); + throw PeerNetworkError(SALTI_ERROR_PEER_NOT_EXIST); this->_send_msg_dispatcher(msg, it->second->conn); } + } catch (const PeerNetworkError &) { + this->recoverable_error(std::current_exception()); } catch (...) { this->recoverable_error(std::current_exception()); } }); } @@ -925,6 +949,7 @@ msgnetwork_config_t *peernetwork_config_as_msgnetwork_config(peernetwork_config_ peernetwork_t *peernetwork_new(const eventcontext_t *ec, const peernetwork_config_t *config); void peernetwork_free(const peernetwork_t *self); void peernetwork_add_peer(peernetwork_t *self, const netaddr_t *paddr); +void peernetwork_del_peer(peernetwork_t *self, const netaddr_t *paddr); bool peernetwork_has_peer(const peernetwork_t *self, const netaddr_t *paddr); const peernetwork_conn_t *peernetwork_get_peer_conn(const peernetwork_t *self, const netaddr_t *paddr); msgnetwork_t *peernetwork_as_msgnetwork(peernetwork_t *self); diff --git a/include/salticidae/util.h b/include/salticidae/util.h index 0ddf5be..41f681c 100644 --- a/include/salticidae/util.h +++ b/include/salticidae/util.h @@ -60,7 +60,7 @@ enum SalticidaeErrorCode { SALTI_ERROR_LISTEN, SALTI_ERROR_CONNECT, SALTI_ERROR_PEER_ALREADY_EXISTS, - SALTI_ERROR_PEER_NOT_EXISTS, + SALTI_ERROR_PEER_NOT_EXIST, SALTI_ERROR_NETADDR_INVALID, SALTI_ERROR_OPTVAL_INVALID, SALTI_ERROR_OPTNAME_ALREADY_EXISTS, -- cgit v1.2.3-70-g09d2