summaryrefslogtreecommitdiff
path: root/saltoqd/toqmanager.cpp
blob: fa8f7c6a319c13742c2f3a6de025add1171f4492 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "toqmanager.h"

#include "versionmanager.h"
#include "systemmanager.h"
#include "storagemanager.h"
#include "musicmanager.h"
#include "commmanager.h"
#include "voicecallmanager.h"

ToqManager::ToqManager(const QBluetoothAddress &address, QObject *parent) :
	QObject(parent),
	_conn(new ToqConnection(address, this)),
	_versionManager(new VersionManager(this)),
	_systemManager(new SystemManager(this)),
	_storageManager(new StorageManager(this)),
	_musicManager(new MusicManager(this)),
	_commManager(new CommManager(_storageManager, this)),
	_voiceCallManager(new VoiceCallManager(this))
{
	connect(_conn, &ToqConnection::messageReceived,
			this, &ToqManager::handleToqMessage);
}

void ToqManager::setEndpointListener(ToqConnection::Endpoint ep, EndpointHandler *handler)
{
	Q_ASSERT(!_handlers.contains(ep));
	_handlers.insert(ep, handler);
}

void ToqManager::sendMessage(const ToqConnection::Message &msg)
{
	if (1) {
		QString json = QString::fromUtf8(msg.payload.toJson(QJsonDocument::Compact));
		qDebug() << "Sending message to" << ToqConnection::nameOfEndpoint(msg.destination) << "from" << ToqConnection::nameOfEndpoint(msg.destination) << "type" << msg.type << json;
	}
	_conn->sendMessage(msg);
}

void ToqManager::sendMessage(ToqConnection::Endpoint source, ToqConnection::Endpoint destination, quint16 transactionId, quint32 type, const QJsonObject &payload)
{
	QJsonDocument doc(payload);
	ToqConnection::Message msg(source, destination, transactionId, type, doc);
	sendMessage(msg);
}

quint16 ToqManager::sendMessage(ToqConnection::Endpoint source, ToqConnection::Endpoint destination, quint32 type, const QJsonObject &payload)
{
	QJsonDocument doc(payload);
	quint16 transactionId = _conn->nextTransactionId();
	ToqConnection::Message msg(source, destination, transactionId, type, doc);
	sendMessage(msg);
	return transactionId;
}

void ToqManager::sendReply(const ToqConnection::Message &msg, quint32 type, const QJsonObject &payload)
{
	ToqConnection::Message reply(msg.destination, msg.source, msg.transactionId, type, QJsonDocument(payload));
	sendMessage(reply);
}

void ToqManager::handleToqMessage(const ToqConnection::Message &msg)
{
	EndpointHandler *handler = _handlers.value(msg.destination, 0);

	if (1) {
		QString json = QString::fromUtf8(msg.payload.toJson(QJsonDocument::Compact));
		qDebug() << "Received message to" << ToqConnection::nameOfEndpoint(msg.destination) << "from" << ToqConnection::nameOfEndpoint(msg.destination) << "type" << msg.type << json;
	}

	if (handler) {
		handler->handleMessage(msg);
	} else {
		qWarning() << "No registered handler for endpoint" << ToqConnection::nameOfEndpoint(msg.destination);
	}
}