From b766baf6f34df321e8eff9687cc2c17485da6fb4 Mon Sep 17 00:00:00 2001 From: Michael Jurkoic Date: Mon, 18 Mar 2024 20:20:36 -0500 Subject: Use smart pointers --- src/nostr_service.cpp | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'src/nostr_service.cpp') diff --git a/src/nostr_service.cpp b/src/nostr_service.cpp index 50609b4..0409a0d 100644 --- a/src/nostr_service.cpp +++ b/src/nostr_service.cpp @@ -22,27 +22,33 @@ using std::make_tuple; using std::move; using std::mutex; using std::out_of_range; +using std::shared_ptr; using std::string; using std::thread; using std::tuple; +using std::unique_ptr; using std::vector; namespace nostr { -NostrService::NostrService(plog::IAppender* appender, client::IWebSocketClient* client) - : NostrService(appender, client, {}) { }; - -NostrService::NostrService(plog::IAppender* appender, client::IWebSocketClient* client, RelayList relays) - : _defaultRelays(relays), _client(client) +NostrService::NostrService( + shared_ptr appender, + shared_ptr client) +: NostrService(appender, client, {}) { }; + +NostrService::NostrService( + shared_ptr appender, + shared_ptr client, + RelayList relays) +: _defaultRelays(relays), _client(client) { - plog::init(plog::debug, appender); + plog::init(plog::debug, appender.get()); client->start(); }; NostrService::~NostrService() { this->_client->stop(); - delete this->_client; }; RelayList NostrService::defaultRelays() const { return this->_defaultRelays; }; @@ -152,6 +158,7 @@ tuple NostrService::publishEvent(Event event) string NostrService::queryRelays(Filters filters) { return this->queryRelays(filters, [this](string subscriptionId, Event event) { + lock_guard lock(this->_propertyMutex); this->_eventIterators[subscriptionId] = this->_events[subscriptionId].begin(); this->onEvent(subscriptionId, event); }); @@ -166,6 +173,7 @@ string NostrService::queryRelays(Filters filters, function vector>> requestFutures; for (const string relay : this->_activeRelays) { + lock_guard lock(this->_propertyMutex); this->_subscriptions[relay].push_back(subscriptionId); string request = filters.serialize(subscriptionId); @@ -228,6 +236,7 @@ vector NostrService::getNewEvents(string subscriptionId) throw out_of_range("No event iterator found for subscription: " + subscriptionId); } + lock_guard lock(this->_propertyMutex); vector newEvents; vector receivedEvents = this->_events[subscriptionId]; vector::iterator eventIt = this->_eventIterators[subscriptionId]; @@ -453,6 +462,7 @@ bool NostrService::hasSubscription(string relay, string subscriptionId) void NostrService::onEvent(string subscriptionId, Event event) { + lock_guard lock(this->_propertyMutex); _events[subscriptionId].push_back(event); PLOG_INFO << "Received event for subscription: " << subscriptionId; -- cgit