Hook up new pubsub

This commit is contained in:
Alex Hultman
2019-10-07 10:57:04 +02:00
parent a35d7f2ff3
commit d09f25850b
6 changed files with 39 additions and 8 deletions
+1
View File
@@ -31,6 +31,7 @@ template <bool SSL>
struct AsyncSocket {
template <bool> friend struct HttpContext;
template <bool, bool> friend struct WebSocketContext;
template <bool> friend struct WebSocketContextData;
friend struct TopicTree;
protected:
+15
View File
@@ -6,6 +6,8 @@
#include <set>
#include <chrono>
namespace uWS {
/* A Subscriber is an extension of a socket */
struct Subscriber {
/* List of all our subscriptions (subscribersNextSubscription) */
@@ -215,6 +217,10 @@ public:
/* Drain the tree by emitting what to send with every Subscriber */
void drain(/*std::function<int(Subscriber *, std::string_view)> cb*/) {
if (!numTriggeredTopics) {
return;
}
/* Up to 64 triggered Topics per batch */
std::map<uint64_t, std::string> intersectionCache;
@@ -285,6 +291,13 @@ public:
min = nextMin;
}
/* Clear messages of triggered Topics */
for (int i = 0; i < numTriggeredTopics; i++) {
triggeredTopics[i]->messages.clear();
triggeredTopics[i]->triggered = false;
}
numTriggeredTopics = 0;
}
void print(Topic *root = nullptr, int indentation = 1) {
@@ -302,3 +315,5 @@ public:
}
}
};
}
+3 -5
View File
@@ -128,7 +128,7 @@ public:
}
/* Make sure to unsubscribe from any pub/sub node at exit */
webSocketContextData->topicTree.unsubscribeAll(this);
webSocketContextData->topicTree.unsubscribeAll((Subscriber *) this);
}
/* Subscribe to a topic according to MQTT rules and syntax */
@@ -138,9 +138,7 @@ public:
);
/* Fix this up */
bool *valid = new bool;
*valid = true;
webSocketContextData->topicTree.subscribe(std::string(topic), this, valid);
webSocketContextData->topicTree.subscribe(topic, (Subscriber *) this);
}
/* Publish a message to a topic according to MQTT rules and syntax */
@@ -153,7 +151,7 @@ public:
char dst[1024];
size_t dst_length = protocol::formatMessage<true>(dst, message.data(), message.length(), OpCode::TEXT, message.length(), false);
webSocketContextData->topicTree.publish(std::string(topic), dst, dst_length);
webSocketContextData->topicTree.publish(topic, std::string_view(dst, dst_length));
}
};
+1 -1
View File
@@ -237,7 +237,7 @@ private:
}
/* Make sure to unsubscribe from any pub/sub node at exit */
webSocketContextData->topicTree.unsubscribeAll(s);
webSocketContextData->topicTree.unsubscribeAll((Subscriber *) s);
}
/* Destruct in-placed data struct */
+18 -1
View File
@@ -22,7 +22,7 @@
#include <string_view>
#include "WebSocketProtocol.h"
#include "TopicTree.h"
#include "TopicTreeDraft.h"
namespace uWS {
@@ -43,6 +43,23 @@ struct WebSocketContextData {
/* Each websocket context has a topic tree for pub/sub */
TopicTree topicTree;
WebSocketContextData() : topicTree([](Subscriber *s, std::string_view data) -> int {
//std::cout << "Skickar data: " << data << " på sub: " << s << std::endl;
auto *asyncSocket = (AsyncSocket<SSL> *) s;
asyncSocket->write(data.data(), data.length());
return 0;
}) {
Loop::get()->addPostHandler([this](Loop *loop) {
topicTree.drain();
});
}
};
}