mirror of
https://github.com/QuasarApp/Heart.git
synced 2025-04-29 19:24:38 +00:00
64 lines
1.8 KiB
C++
64 lines
1.8 KiB
C++
#include "abstractnode.h"
|
|
#include "abstractnodeinfo.h"
|
|
#include "websocketcontroller.h"
|
|
#include <quasarapp.h>
|
|
|
|
namespace NetworkProtocol {
|
|
|
|
|
|
WebSocketController::WebSocketController(AbstractNode *node) {
|
|
_node = node;
|
|
assert(_node);
|
|
}
|
|
|
|
bool WebSocketController::subscribe(QSharedPointer<AbstractNodeInfo> subscriber,
|
|
const DbAddress &item) {
|
|
|
|
_subscribs[item].insert(subscriber);
|
|
_items[subscriber].insert(item);
|
|
|
|
return true;
|
|
}
|
|
|
|
void WebSocketController::unsubscribe(QSharedPointer<AbstractNodeInfo> subscriber,
|
|
const DbAddress& item) {
|
|
_subscribs[item].remove(subscriber);
|
|
_items[subscriber].remove(item);
|
|
|
|
}
|
|
|
|
const QSet<DbAddress> &WebSocketController::list(
|
|
QSharedPointer<AbstractNodeInfo> node) {
|
|
return _items[node];
|
|
}
|
|
|
|
void WebSocketController::handleItemChanged(const QWeakPointer<AbstractData> &item) {
|
|
auto obj = item.toStrongRef().dynamicCast<DBObject>();
|
|
if (obj.isNull() || !obj->isValid())
|
|
return;
|
|
|
|
foreachSubscribers(item, _subscribs.value(obj->dbAddress()));
|
|
}
|
|
|
|
void WebSocketController::foreachSubscribers(const QWeakPointer<AbstractData> &item,
|
|
const QSet<QSharedPointer<AbstractNodeInfo>> &subscribersList) {
|
|
|
|
auto ref = item.toStrongRef().dynamicCast<DBObject>();
|
|
|
|
if (ref.isNull())
|
|
return;
|
|
|
|
for (auto &&subscriber : subscribersList) {
|
|
|
|
if (!subscriber.isNull() && subscriber->isValid()) {
|
|
if (!_node->sendData(item, subscriber->id())) {
|
|
QuasarAppUtils::Params::verboseLog("Send update failed for " + subscriber->id().toString(),
|
|
QuasarAppUtils::Warning);
|
|
}
|
|
} else {
|
|
unsubscribe(subscriber, ref->dbAddress());
|
|
}
|
|
}
|
|
}
|
|
}
|