Hook up new pubsub
This commit is contained in:
@@ -31,6 +31,7 @@ template <bool SSL>
|
|||||||
struct AsyncSocket {
|
struct AsyncSocket {
|
||||||
template <bool> friend struct HttpContext;
|
template <bool> friend struct HttpContext;
|
||||||
template <bool, bool> friend struct WebSocketContext;
|
template <bool, bool> friend struct WebSocketContext;
|
||||||
|
template <bool> friend struct WebSocketContextData;
|
||||||
friend struct TopicTree;
|
friend struct TopicTree;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|||||||
@@ -6,6 +6,8 @@
|
|||||||
#include <set>
|
#include <set>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
||||||
|
namespace uWS {
|
||||||
|
|
||||||
/* A Subscriber is an extension of a socket */
|
/* A Subscriber is an extension of a socket */
|
||||||
struct Subscriber {
|
struct Subscriber {
|
||||||
/* List of all our subscriptions (subscribersNextSubscription) */
|
/* List of all our subscriptions (subscribersNextSubscription) */
|
||||||
@@ -215,6 +217,10 @@ public:
|
|||||||
/* Drain the tree by emitting what to send with every Subscriber */
|
/* Drain the tree by emitting what to send with every Subscriber */
|
||||||
void drain(/*std::function<int(Subscriber *, std::string_view)> cb*/) {
|
void drain(/*std::function<int(Subscriber *, std::string_view)> cb*/) {
|
||||||
|
|
||||||
|
if (!numTriggeredTopics) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
/* Up to 64 triggered Topics per batch */
|
/* Up to 64 triggered Topics per batch */
|
||||||
std::map<uint64_t, std::string> intersectionCache;
|
std::map<uint64_t, std::string> intersectionCache;
|
||||||
|
|
||||||
@@ -285,6 +291,13 @@ public:
|
|||||||
|
|
||||||
min = nextMin;
|
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) {
|
void print(Topic *root = nullptr, int indentation = 1) {
|
||||||
@@ -302,3 +315,5 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
}
|
||||||
+3
-5
@@ -128,7 +128,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Make sure to unsubscribe from any pub/sub node at exit */
|
/* 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 */
|
/* Subscribe to a topic according to MQTT rules and syntax */
|
||||||
@@ -138,9 +138,7 @@ public:
|
|||||||
);
|
);
|
||||||
|
|
||||||
/* Fix this up */
|
/* Fix this up */
|
||||||
bool *valid = new bool;
|
webSocketContextData->topicTree.subscribe(topic, (Subscriber *) this);
|
||||||
*valid = true;
|
|
||||||
webSocketContextData->topicTree.subscribe(std::string(topic), this, valid);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Publish a message to a topic according to MQTT rules and syntax */
|
/* Publish a message to a topic according to MQTT rules and syntax */
|
||||||
@@ -153,7 +151,7 @@ public:
|
|||||||
char dst[1024];
|
char dst[1024];
|
||||||
size_t dst_length = protocol::formatMessage<true>(dst, message.data(), message.length(), OpCode::TEXT, message.length(), false);
|
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));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -237,7 +237,7 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Make sure to unsubscribe from any pub/sub node at exit */
|
/* 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 */
|
/* Destruct in-placed data struct */
|
||||||
|
|||||||
@@ -22,7 +22,7 @@
|
|||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
|
||||||
#include "WebSocketProtocol.h"
|
#include "WebSocketProtocol.h"
|
||||||
#include "TopicTree.h"
|
#include "TopicTreeDraft.h"
|
||||||
|
|
||||||
namespace uWS {
|
namespace uWS {
|
||||||
|
|
||||||
@@ -43,6 +43,23 @@ struct WebSocketContextData {
|
|||||||
|
|
||||||
/* Each websocket context has a topic tree for pub/sub */
|
/* Each websocket context has a topic tree for pub/sub */
|
||||||
TopicTree topicTree;
|
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();
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
Submodule uSockets updated: e2c093cb78...12a235bcfe
Reference in New Issue
Block a user