Nostrduino
Loading...
Searching...
No Matches
NostrPool.h
Go to the documentation of this file.
1#ifndef _NOSTR_POOL_H
2#define _NOSTR_POOL_H
3#include <Arduino.h>
4#include <NostrTransport.h>
5#include <functional>
6#include <initializer_list>
7#include <map>
8#include <mutex>
9#include <vector>
10#include "NostrString.h"
11#include "NostrLimits.h"
12#include "ArduinoJson.h"
13#include "NostrEvent.h"
14#include "NostrString.h"
15
16
17namespace nostr {
18 class NostrRelay;
19
21 std::function<void(const NostrString &, const NostrString &)>;
23 std::function<void(NostrRelay *, const NostrString &)>;
24 using NostrEOSECallback = std::function<void(const NostrString &)>;
25
32 std::function<void(const NostrString &, SignedNostrEvent *event)>;
34 std::function<void(const NostrString &, bool, const NostrString& )>;
35 using NostrSeenCallback = std::function<bool(const SignedNostrEvent &)>;
37 bool active = true;
38 bool eose = false;
39 bool closed = false;
40 bool disconnected = false;
41 };
42
44 public:
45 bool eose = false;
50
51 private:
52 friend class NostrPool;
53 NostrString request;
54 std::map<NostrString, NostrRelaySubscriptionState> relayStates;
55 };
56
62
63 class NostrRelay {
64 friend class NostrPool;
65
66 public:
73 Connection *getConnection() const { return conn; }
75 return url;
76 }
77 protected:
80 std::vector<NostrString> messageQueue;
83 bool connectedOnce = false;
85 void processQueue();
86 };
87
92 class NostrPool {
93
94 public:
101 NostrPool(Transport *transport, int eventStatusTimeoutSeconds = 60 * 10, const NostrLimits &limits = NostrLimits()) {
102 this->transport = transport;
103 this->eventStatusTimeoutSeconds = eventStatusTimeoutSeconds;
104 this->limits = limits;
105 };
106 ~NostrPool();
107
108 NostrPool(const NostrPool &) = delete;
109 NostrPool &operator=(const NostrPool &) = delete;
110 NostrPool(NostrPool &&) = delete;
112
123 std::initializer_list<NostrString> urls,
124 std::initializer_list<
125 std::map<NostrString, std::initializer_list<NostrString>>>
126 filters,
127 NostrEventCallback eventCallback = nullptr,
128 NostrCloseCallback closeCallback = nullptr,
129 NostrEOSECallback eoseCallback = nullptr
130
131 );
133 const std::vector<NostrString> &urls,
134 std::initializer_list<
135 std::map<NostrString, std::initializer_list<NostrString>>>
136 filters,
137 NostrEventCallback eventCallback = nullptr,
138 NostrCloseCallback closeCallback = nullptr,
139 NostrEOSECallback eoseCallback = nullptr);
140
150 NostrString subscribeMany(std::initializer_list<NostrString> urls, JsonArray filters,
151 NostrEventCallback eventCallback = nullptr, NostrCloseCallback closeCallback = nullptr, NostrEOSECallback eoseCallback = nullptr
152
153 );
154 NostrString subscribeMany(const std::vector<NostrString> &urls, JsonArray filters,
155 NostrEventCallback eventCallback = nullptr,
156 NostrCloseCallback closeCallback = nullptr,
157 NostrEOSECallback eoseCallback = nullptr);
164 bool publish(std::initializer_list<NostrString> rs,
166 NostrEventStatusCallback statusCallback = nullptr);
167 bool publish(const std::vector<NostrString> &rs, SignedNostrEvent *event,
168 NostrEventStatusCallback statusCallback = nullptr);
169
173 void loop();
174
179 void setVerifyIncomingEvents(bool verify) { verifyIncomingEvents = verify; }
180
182 bool getVerifyIncomingEvents() const { return verifyIncomingEvents; }
183
193
198 void closeSubscription(NostrString subId);
199
203 const NostrSubscription *getSubscription(const NostrString &subId) const;
204
208 bool hasSubscription(const NostrString &subId) const;
209
216
222 std::vector<NostrString> getRelays();
223
229
233 void close();
234
235
240 const std::vector<NostrRelay *> *getConnectedRelays() const;
241
242 private:
243 class DispatchGuard {
244 public:
245 explicit DispatchGuard(NostrPool *pool) : pool(pool) { ++pool->dispatchDepth; }
246 ~DispatchGuard() {
247 if (--pool->dispatchDepth == 0) pool->flushDeferredMutations();
248 }
249
250 private:
251 NostrPool *pool;
252 };
253
254 NostrNoticeCallback noticeCallback = nullptr;
255 long long subs = 0;
256 std::map<NostrString, NostrSubscription> subscriptions;
257 std::vector<NostrRelay *> relays;
258 void onEvent(NostrRelay *relay, NostrString message);
259 void recomputeSubscriptionEose(const NostrString &subId);
260 int eventStatusTimeoutSeconds = 60 * 10;
261 std::vector<EventStatusCallbackEntry> eventStatusCallbackEntries;
262 Transport *transport;
263 NostrLimits limits;
264 bool verifyIncomingEvents = false;
265 NostrSeenCallback seen = [](const SignedNostrEvent &) { return false; };
266 std::mutex seenMutex;
267 unsigned int dispatchDepth = 0;
268 bool deferredClose = false;
269 std::vector<NostrString> deferredRelayDisconnects;
270 void disconnectRelayNow(const NostrString &url);
271 void performClose();
272 void flushDeferredMutations();
273 };
274} // namespace nostr
275#endif
#define NostrString
Definition NostrString.h:10
Definition NostrTransport.h:10
Definition Nip47.h:44
Definition NostrPool.h:92
NostrPool(Transport *transport, int eventStatusTimeoutSeconds=60 *10, const NostrLimits &limits=NostrLimits())
Definition NostrPool.h:101
void setVerifyIncomingEvents(bool verify)
Definition NostrPool.h:179
bool publish(std::initializer_list< NostrString > rs, SignedNostrEvent *event, NostrEventStatusCallback statusCallback=nullptr)
Definition NostrPool.cpp:467
const std::vector< NostrRelay * > * getConnectedRelays() const
Definition NostrPool.cpp:527
NostrString subscribeMany(std::initializer_list< NostrString > urls, std::initializer_list< std::map< NostrString, std::initializer_list< NostrString > > > filters, NostrEventCallback eventCallback=nullptr, NostrCloseCallback closeCallback=nullptr, NostrEOSECallback eoseCallback=nullptr)
Definition NostrPool.cpp:171
NostrPool & operator=(NostrPool &&)=delete
void disconnectRelay(NostrString url)
Definition NostrPool.cpp:402
std::vector< NostrString > getRelays()
Definition NostrPool.cpp:519
bool getVerifyIncomingEvents() const
Definition NostrPool.h:182
void setSeenTracker(NostrSeenCallback callback)
Definition NostrPool.cpp:142
void close()
Definition NostrPool.cpp:436
NostrPool(const NostrPool &)=delete
NostrRelay * ensureRelay(NostrString url)
Definition NostrPool.cpp:348
void loop()
Definition NostrPool.cpp:495
void closeSubscription(NostrString subId)
Definition NostrPool.cpp:320
NostrPool(NostrPool &&)=delete
NostrPool & operator=(const NostrPool &)=delete
~NostrPool()
Definition NostrPool.cpp:7
bool hasSubscription(const NostrString &subId) const
Definition NostrPool.cpp:344
const NostrSubscription * getSubscription(const NostrString &subId) const
Definition NostrPool.cpp:339
Definition NostrPool.h:63
bool send(NostrString message)
Definition NostrPool.cpp:11
std::vector< NostrString > messageQueue
Definition NostrPool.h:80
NostrLimits limits
Definition NostrPool.h:82
NostrRelay(Connection *conn, NostrString url, const NostrLimits &limits)
Definition NostrPool.h:71
NostrString url
Definition NostrPool.h:79
bool restoreSubscriptions
Definition NostrPool.h:84
size_t queuedMessageBytes
Definition NostrPool.h:81
void processQueue()
Definition NostrPool.cpp:25
NostrString getUrl() const
Definition NostrPool.h:74
Connection * getConnection() const
Definition NostrPool.h:73
Connection * conn
Definition NostrPool.h:78
bool connectedOnce
Definition NostrPool.h:83
Definition NostrPool.h:43
NostrCloseCallback closeCallback
Definition NostrPool.h:46
NostrEOSECallback eoseCallback
Definition NostrPool.h:47
NostrEventStatusCallback statusCallback
Definition NostrPool.h:49
bool eose
Definition NostrPool.h:45
NostrEventCallback eventCallback
Definition NostrPool.h:48
Definition NostrEvent.h:130
Definition NostrTransport.h:21
Definition CryptoLock.h:6
std::function< bool(const SignedNostrEvent &)> NostrSeenCallback
Definition NostrPool.h:35
struct nostr::s_EventStatusCallbackEntry EventStatusCallbackEntry
std::function< void(const NostrString &, const NostrString &)> NostrCloseCallback
Definition NostrPool.h:21
std::function< void(const NostrString &)> NostrEOSECallback
Definition NostrPool.h:24
std::function< void(const NostrString &, bool, const NostrString &)> NostrEventStatusCallback
Definition NostrPool.h:34
std::function< void(NostrRelay *, const NostrString &)> NostrNoticeCallback
Definition NostrPool.h:23
std::function< void(const NostrString &, SignedNostrEvent *event)> NostrEventCallback
Definition NostrPool.h:32
Definition NostrLimits.h:9
Definition NostrPool.h:36
bool closed
Definition NostrPool.h:39
bool active
Definition NostrPool.h:37
bool disconnected
Definition NostrPool.h:40
bool eose
Definition NostrPool.h:38
Definition NostrPool.h:57
long long timestampSeconds
Definition NostrPool.h:59
NostrEventStatusCallback statusCallback
Definition NostrPool.h:58
NostrString eventId
Definition NostrPool.h:60