diff options
-rw-r--r-- | include/nostr.hpp | 11 | ||||
-rw-r--r-- | src/nostr_service.cpp | 24 | ||||
-rw-r--r-- | test/nostr_service_test.cpp | 24 |
3 files changed, 37 insertions, 22 deletions
diff --git a/include/nostr.hpp b/include/nostr.hpp index 1a4e33c..51ea7cd 100644 --- a/include/nostr.hpp +++ b/include/nostr.hpp @@ -104,8 +104,13 @@ private: class NostrService { public: - NostrService(plog::IAppender* appender, client::IWebSocketClient* client); - NostrService(plog::IAppender* appender, client::IWebSocketClient* client, RelayList relays); + NostrService( + std::shared_ptr<plog::IAppender> appender, + std::shared_ptr<client::IWebSocketClient> client); + NostrService( + std::shared_ptr<plog::IAppender> appender, + std::shared_ptr<client::IWebSocketClient> client, + RelayList relays); ~NostrService(); RelayList defaultRelays() const; @@ -207,7 +212,7 @@ private: const int MAX_EVENTS_PER_SUBSCRIPTION = 128; ///< The WebSocket client used to communicate with relays. - client::IWebSocketClient* _client; + shared_ptr<client::IWebSocketClient> _client; ///< A mutex to protect the instance properties. std::mutex _propertyMutex; ///< The default set of Nostr relays to which the service will attempt to connect. 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<plog::IAppender> appender, + shared_ptr<client::IWebSocketClient> client) +: NostrService(appender, client, {}) { }; + +NostrService::NostrService( + shared_ptr<plog::IAppender> appender, + shared_ptr<client::IWebSocketClient> 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<RelayList, RelayList> NostrService::publishEvent(Event event) string NostrService::queryRelays(Filters filters) { return this->queryRelays(filters, [this](string subscriptionId, Event event) { + lock_guard<mutex> lock(this->_propertyMutex); this->_eventIterators[subscriptionId] = this->_events[subscriptionId].begin(); this->onEvent(subscriptionId, event); }); @@ -166,6 +173,7 @@ string NostrService::queryRelays(Filters filters, function<void(string, Event)> vector<future<tuple<string, bool>>> requestFutures; for (const string relay : this->_activeRelays) { + lock_guard<mutex> lock(this->_propertyMutex); this->_subscriptions[relay].push_back(subscriptionId); string request = filters.serialize(subscriptionId); @@ -228,6 +236,7 @@ vector<Event> NostrService::getNewEvents(string subscriptionId) throw out_of_range("No event iterator found for subscription: " + subscriptionId); } + lock_guard<mutex> lock(this->_propertyMutex); vector<Event> newEvents; vector<Event> receivedEvents = this->_events[subscriptionId]; vector<Event>::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<mutex> lock(this->_propertyMutex); _events[subscriptionId].push_back(event); PLOG_INFO << "Received event for subscription: " << subscriptionId; diff --git a/test/nostr_service_test.cpp b/test/nostr_service_test.cpp index 2dd34d2..70f4d9e 100644 --- a/test/nostr_service_test.cpp +++ b/test/nostr_service_test.cpp @@ -56,12 +56,12 @@ TEST_F(NostrServiceTest, Constructor_StartsClient) { EXPECT_CALL(*testClient, start()).Times(1); - auto nostrService = new nostr::NostrService(testAppender.get(), testClient.get()); + auto nostrService = new nostr::NostrService(testAppender, testClient); }; TEST_F(NostrServiceTest, Constructor_InitializesService_WithNoDefaultRelays) { - auto nostrService = new nostr::NostrService(testAppender.get(), testClient.get()); + auto nostrService = new nostr::NostrService(testAppender, testClient); auto defaultRelays = nostrService->defaultRelays(); auto activeRelays = nostrService->activeRelays(); @@ -71,7 +71,7 @@ TEST_F(NostrServiceTest, Constructor_InitializesService_WithNoDefaultRelays) TEST_F(NostrServiceTest, Constructor_InitializesService_WithProvidedDefaultRelays) { - auto nostrService = new nostr::NostrService(testAppender.get(), testClient.get(), defaultTestRelays); + auto nostrService = new nostr::NostrService(testAppender, testClient, defaultTestRelays); auto defaultRelays = nostrService->defaultRelays(); auto activeRelays = nostrService->activeRelays(); @@ -87,7 +87,7 @@ TEST_F(NostrServiceTest, Destructor_StopsClient) { EXPECT_CALL(*testClient, start()).Times(1); - auto nostrService = new nostr::NostrService(testAppender.get(), testClient.get()); + auto nostrService = new nostr::NostrService(testAppender, testClient); }; TEST_F(NostrServiceTest, OpenRelayConnections_OpensConnections_ToDefaultRelays) @@ -112,7 +112,7 @@ TEST_F(NostrServiceTest, OpenRelayConnections_OpensConnections_ToDefaultRelays) return status; })); - auto nostrService = new nostr::NostrService(testAppender.get(), testClient.get(), defaultTestRelays); + auto nostrService = new nostr::NostrService(testAppender, testClient, defaultTestRelays); nostrService->openRelayConnections(); auto activeRelays = nostrService->activeRelays(); @@ -147,7 +147,7 @@ TEST_F(NostrServiceTest, OpenRelayConnections_OpensConnections_ToProvidedRelays) return status; })); - auto nostrService = new nostr::NostrService(testAppender.get(), testClient.get(), defaultTestRelays); + auto nostrService = new nostr::NostrService(testAppender, testClient, defaultTestRelays); nostrService->openRelayConnections(testRelays); auto activeRelays = nostrService->activeRelays(); @@ -184,7 +184,7 @@ TEST_F(NostrServiceTest, OpenRelayConnections_AddsOpenConnections_ToActiveRelays return status; })); - auto nostrService = new nostr::NostrService(testAppender.get(), testClient.get(), defaultTestRelays); + auto nostrService = new nostr::NostrService(testAppender, testClient, defaultTestRelays); nostrService->openRelayConnections(); auto activeRelays = nostrService->activeRelays(); @@ -227,7 +227,7 @@ TEST_F(NostrServiceTest, CloseRelayConnections_ClosesConnections_ToActiveRelays) return status; })); - auto nostrService = new nostr::NostrService(testAppender.get(), testClient.get(), defaultTestRelays); + auto nostrService = new nostr::NostrService(testAppender, testClient, defaultTestRelays); nostrService->openRelayConnections(); EXPECT_CALL(*testClient, closeConnection(defaultTestRelays[0])).Times(1); @@ -262,7 +262,7 @@ TEST_F(NostrServiceTest, CloseRelayConnections_RemovesClosedConnections_FromActi return status; })); - auto nostrService = new nostr::NostrService(testAppender.get(), testClient.get(), allTestRelays); + auto nostrService = new nostr::NostrService(testAppender, testClient, allTestRelays); nostrService->openRelayConnections(); EXPECT_CALL(*testClient, closeConnection(testRelays[0])).Times(1); @@ -300,7 +300,7 @@ TEST_F(NostrServiceTest, PublishEvent_CorrectlyIndicates_AllSuccesses) return status; })); - auto nostrService = new nostr::NostrService(testAppender.get(), testClient.get(), defaultTestRelays); + auto nostrService = new nostr::NostrService(testAppender, testClient, defaultTestRelays); nostrService->openRelayConnections(); EXPECT_CALL(*testClient, send(_, _)) @@ -340,7 +340,7 @@ TEST_F(NostrServiceTest, PublishEvent_CorrectlyIndicates_AllFailures) return status; })); - auto nostrService = new nostr::NostrService(testAppender.get(), testClient.get(), defaultTestRelays); + auto nostrService = new nostr::NostrService(testAppender, testClient, defaultTestRelays); nostrService->openRelayConnections(); EXPECT_CALL(*testClient, send(_, _)) @@ -380,7 +380,7 @@ TEST_F(NostrServiceTest, PublishEvent_CorrectlyIndicates_MixedSuccessesAndFailur return status; })); - auto nostrService = new nostr::NostrService(testAppender.get(), testClient.get(), defaultTestRelays); + auto nostrService = new nostr::NostrService(testAppender, testClient, defaultTestRelays); nostrService->openRelayConnections(); EXPECT_CALL(*testClient, send(_, defaultTestRelays[0])) |