Skip to content

Commit

Permalink
Expose relays and add connection status listener
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardobl committed Jul 15, 2024
1 parent e31a8f4 commit 74c2e87
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 15 deletions.
30 changes: 23 additions & 7 deletions examples/ESP32TestNip01Filter/ESP32TestNip01Filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void setup() {
testNIP01Filter();
}

void loop() {
void loop() {
for (nostr::NostrPool *pool : pools) {
pool->loop();
}
Expand All @@ -56,28 +56,28 @@ void loop() {
nostr::Transport *transport;

void testNIP01Filter() {
try{
try {
String relay = RELAY;
transport = nostr::esp32::ESP32Platform::getTransport();
nostr::NostrPool *pool = new nostr::NostrPool(transport);
pools.push_back(pool);
String subId = pool->subscribeMany(
{relay},
{{
{"kinds", {"1"}},
{"since", {"1626023056"}},
{"until", {"1846947856"}},
{"kinds", {"1"}},
{"since", {"1626023056"}},
{"until", {"1846947856"}},
{"limit", {"10"}},
// Filters defined in NIP01 are automatically converted to the correct type
// however this library support non-NIP01 filters as well, but you might need to
// however this library support non-NIP01 filters as well, but you might need to
// specify their type manually, if unspecified the code assumes string[]:
// eg. {"int newFilter", {"1"}}
// eg. {"int[] newFilter2", {"1", "2"}}
// eg. {"float newFilter3", {"1.1"}}
// eg. {"float[] newFilter4", {"1.1", "2.2"}}
// eg. {"string newFilter5", {"hello"}}
}},
[&](const String &subId, nostr::SignedNostrEvent *event) {
[&](const String &subId, nostr::SignedNostrEvent *event) {
JsonDocument doc;
JsonArray arr = doc["data"].to<JsonArray>();
event->toSendableEvent(arr);
Expand All @@ -86,6 +86,22 @@ void testNIP01Filter() {
Serial.println("Event received: " + json);
},
[&](const String &subId, const String &reason) { Serial.println("Subscription closed: " + reason); }, [&](const String &subId) { Serial.println("Subscription EOSE: " + subId); });

std::vector<nostr::NostrRelay *> *relays = pool->getConnectedRelays();
for (nostr::NostrRelay *relay : *relays) {
Serial.println("Registering to connection events of: " + relay->getUrl());
relay->getConnection()->addConnectionStatusListener([&](const nostr::ConnectionStatus &status) {
String sstatus="UNKNOWN";
if(status==nostr::ConnectionStatus::CONNECTED){
sstatus="CONNECTED";
}else if(status==nostr::ConnectionStatus::DISCONNECTED){
sstatus="DISCONNECTED";
}else if(status==nostr::ConnectionStatus::ERROR){
sstatus = "ERROR";
}
Serial.println("Connection status changed: " + sstatus);
});
}
} catch (const std::exception &e) {
Serial.println("Error: " + String(e.what()));
}
Expand Down
4 changes: 4 additions & 0 deletions src/NostrPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,7 @@ std::vector<NostrString> NostrPool::getRelays() {
}
return urls;
}

std::vector<NostrRelay *> *NostrPool::getConnectedRelays() {
return &this->relays;
}
21 changes: 16 additions & 5 deletions src/NostrPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,15 @@ namespace nostr {
*/
void send(NostrString message);
NostrRelay(Connection *conn, NostrString url) : conn(conn), url(url){};

Connection *getConnection() const { return conn; }
NostrString getUrl() const{
return url;
}
protected:
std::vector<NostrString> messageQueue;
Connection *conn;
NostrString url;
void processQueue();
NostrString url;
std::vector<NostrString> messageQueue;
Connection *conn;
void processQueue();
};

/**
Expand Down Expand Up @@ -138,6 +141,7 @@ namespace nostr {
/**
* Get all relays that the pool is connected to
* @return A list of relay URLs
* @deprecated Use getConnectedRelays() instead
*/
std::vector<NostrString> getRelays();

Expand All @@ -152,6 +156,13 @@ namespace nostr {
*/
void close();


/**
* Get all relays that the pool is connected to
* @return A list of relays
*/
std::vector<NostrRelay *> *getConnectedRelays();

private:
NostrNoticeCallback noticeCallback = nullptr;
long long subs = 0;
Expand Down
8 changes: 5 additions & 3 deletions src/Transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <functional>

namespace nostr {
enum class ConnectionStatus { CONNECTED, DISCONNECTED, ERROR };
class Connection {
public:
virtual void addMessageListener(std::function<void(NostrString)> listener);
Expand All @@ -13,17 +14,18 @@ class Connection {
virtual ~Connection() = default;
virtual void loop() = 0;
virtual bool isReady() = 0;
virtual void addConnectionStatusListener(std::function<void(ConnectionStatus status)> listener) {};
};
class Transport {
public:
virtual void getInvoiceFromLNAddr(NostrString addr, unsigned long long amountMSats, NostrString comment , std::function<void(NostrString)> callback) = 0;
virtual void getInvoiceFromLNAddr(NostrString addr, unsigned long long amountMSats, NostrString comment, std::function<void(NostrString)> callback) = 0;
virtual Connection *connect(NostrString url) = 0;
virtual ~Transport() = default;
Transport() = default;
virtual void disconnect(Connection *conn) = 0;
virtual bool isReady() = 0;
virtual void close() = 0;
virtual void loop(){};
virtual void loop() {};
};
}
} // namespace nostr
#endif
25 changes: 25 additions & 0 deletions src/esp32/ESP32Transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,23 @@ esp32::ESP32Connection::ESP32Connection(ESP32Transport *transport, NostrString u
switch (type) {
case WStype_DISCONNECTED:
Utils::log("ESP32Connection disconnected.");
for (auto &listener : connectionListeners) {
try {
listener(ConnectionStatus::DISCONNECTED);
} catch (std::exception &e) {
Utils::log(e.what());
}
}
break;
case WStype_CONNECTED:
Utils::log("ESP32Connection connected.");
for (auto &listener : connectionListeners) {
try {
listener(ConnectionStatus::CONNECTED);
} catch (std::exception &e) {
Utils::log(e.what());
}
}
break;
case WStype_TEXT: {
NostrString message = NostrString_fromChars((char *)payload);
Expand All @@ -157,6 +171,13 @@ esp32::ESP32Connection::ESP32Connection(ESP32Transport *transport, NostrString u
}
case WStype_ERROR:
Utils::log("ESP32Connection error.");
for (auto &listener : connectionListeners) {
try {
listener(ConnectionStatus::ERROR);
} catch (std::exception &e) {
Utils::log(e.what());
}
}
break;
default:
break;
Expand Down Expand Up @@ -200,4 +221,8 @@ esp32::ESP32Connection::~ESP32Connection() {

esp32::ESP32Transport::ESP32Transport() {}

void esp32::ESP32Connection::addConnectionStatusListener(std::function<void(ConnectionStatus status)> listener) {
connectionListeners.push_back(listener);
}

#endif
5 changes: 5 additions & 0 deletions src/esp32/ESP32Transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class ESP32Connection : public Connection {
void loop() override;
bool isReady() override;
void addMessageListener(std::function<void(NostrString)> listener) override;
void addConnectionStatusListener(std::function<void(ConnectionStatus status)> listener) override;
WebSocketsClient* getWebsocket(){
return &ws;
}
~ESP32Connection() override;

protected:
Expand All @@ -33,6 +37,7 @@ class ESP32Connection : public Connection {
ESP32Transport *transport;
WebSocketsClient ws;
std::vector<std::function<void(NostrString)>> messageListeners;
std::vector<std::function<void(ConnectionStatus status)>> connectionListeners;
};
class ESP32Transport : public Transport {
public:
Expand Down

0 comments on commit 74c2e87

Please sign in to comment.