summaryrefslogtreecommitdiff
path: root/capabilitypeer.cc
blob: 0290b69e397d03955e2c57ee2db4d48745f5efe3 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "sappeer.h"
#include "sapconnection.h"
#include "sapconnectionrequest.h"
#include "sapsocket.h"
#include "sapmanager.h"
#include "capabilitypeer.h"

CapabilityPeer::CapabilityPeer(SAPPeer *peer, QObject *parent) :
    QObject(parent), _peer(peer), _conn(0), _socket(0)
{
	_consumerProfiles.insert(SAProtocol::capabilityDiscoveryProfile,
	                         SAProtocol::capabilityDiscoveryAgentId);
	_providerProfiles.insert(SAProtocol::capabilityDiscoveryProfile,
	                         SAProtocol::capabilityDiscoveryAgentId);

	if (_peer->role() == SAProtocol::ClientRole) {
		_conn = _peer->createServiceConnection(SAProtocol::capabilityDiscoveryProfile,
		                                       SAProtocol::capabilityDiscoveryProfile, SAPServiceInfo::RoleConsumer);
		connect(_conn, SIGNAL(connected()), SLOT(handleConnected()));
	}
}

void CapabilityPeer::requestConnection(SAPConnectionRequest *request)
{
	_conn = request->connection();
	request->accept();
}

int CapabilityPeer::remoteAgentId(const QString &profile, SAPServiceInfo::Role role)
{
	QHash<QString, int> *profiles = profilesByRole(role);
	if (!profiles) return -1;

	return profiles->value(profile, -1);
}

SAPServiceInfo CapabilityPeer::remoteServiceInfo(int agentId) const
{
	return _remoteAgents.value(agentId).info;
}

void CapabilityPeer::handleConnected()
{
	Q_ASSERT(_conn);
	_socket = _conn->getSocket(SAProtocol::capabilityDiscoveryChannel);
	Q_ASSERT(_socket);

	connect(_socket, SIGNAL(messageReceived()), SLOT(handleMessageReceived()));

	// Send a discovery query.
	SAProtocol::CapabilityDiscoveryQuery msg;

	msg.messageType = SAProtocol::CapabilityDiscoveryMessageTypeQuery;
	msg.queryType = 2;
	msg.checksum = 1;

	// Query for all known profiles
	QSet<QString> profiles = SAPManager::instance()->allProfiles();
	profiles.remove(SAProtocol::capabilityDiscoveryProfile);
	msg.records = profiles.toList();

	qDebug() << "Quering for profiles:" << msg.records;

	if (msg.records.isEmpty()) {
		// Nothing to do
		qWarning() << "No local profiles!";
		return;
	}

	_socket->send(SAProtocol::packCapabilityDiscoveryQuery(msg));
}

void CapabilityPeer::handleMessageReceived()
{
	QByteArray data = _socket->receive();

	if (data.size() < 6) {
		qWarning() << "Invalid capability message received";
		return;
	}

	switch (data[0]) {
	case SAProtocol::CapabilityDiscoveryMessageTypeQuery: {
		SAProtocol::CapabilityDiscoveryQuery msg = SAProtocol::unpackCapabilityDiscoveryQuery(data);
		SAProtocol::CapabilityDiscoveryResponse resp;
		SAPManager *manager = SAPManager::instance();

		{
			QDebug d = qDebug();
			d << "Queried for caps:";
			foreach(const QString &cap, msg.records) {
				d << cap;
			}
		}

		qDebug() << "Got checksum" << msg.checksum;

		resp.messageType = SAProtocol::CapabilityDiscoveryMessageTypeResponse;
		resp.queryType = 3; // Why?
		resp.checksum = 1; // We will always cause a checksum fail for now.

		foreach (const QString &profile, msg.records) {
			int agentId = manager->registeredAgentId(profile, SAPServiceInfo::RoleProvider);
			if (agentId >= 0) {
				const SAPServiceInfo &info = manager->serviceInfo(agentId);
				SAProtocol::CapabilityDiscoveryProvider provider;
				provider.name = info.friendlyName();
				provider.uuid = 0;

				SAProtocol::CapabilityDiscoveryService service;
				service.aspVersion = info.version();
				service.componentId = agentId;
				service.connTimeout = info.connectionTimeout();
				service.profile = profile;
				service.role = info.role();
				provider.services.append(service);

				Q_ASSERT(service.role == SAPServiceInfo::RoleProvider);

				resp.providers.append(provider);
			}

			agentId = manager->registeredAgentId(profile, SAPServiceInfo::RoleConsumer);
			if (agentId >= 0) {
				const SAPServiceInfo &info = manager->serviceInfo(agentId);
				SAProtocol::CapabilityDiscoveryProvider provider;
				provider.name = info.friendlyName();
				provider.uuid = 0x1234;

				SAProtocol::CapabilityDiscoveryService service;
				service.aspVersion = info.version();
				service.componentId = agentId;
				service.connTimeout = info.connectionTimeout();
				service.profile = profile;
				service.role = info.role();
				provider.services.append(service);

				Q_ASSERT(service.role == SAPServiceInfo::RoleConsumer);

				resp.providers.append(provider);
			}
		}

		_socket->send(SAProtocol::packCapabilityDiscoveryResponse(resp));

		break;
	}
	case SAProtocol::CapabilityDiscoveryMessageTypeResponse: {
		SAProtocol::CapabilityDiscoveryResponse msg = SAProtocol::unpackCapabilityDiscoveryResponse(data);

		foreach (const SAProtocol::CapabilityDiscoveryProvider &provider, msg.providers) {
			foreach (const SAProtocol::CapabilityDiscoveryService &service, provider.services) {
				RemoteAgent ragent;
				ragent.agentId = service.componentId;
				ragent.info.setFriendlyName(provider.name);
				ragent.info.setProfile(service.profile);
				ragent.info.setRole(static_cast<SAPServiceInfo::Role>(service.role));
				ragent.info.setVersion(service.aspVersion);
				ragent.info.setConnectionTimeout(service.connTimeout);

				_remoteAgents.insert(ragent.agentId, ragent);

				QHash<QString, int> *profiles = profilesByRole(ragent.info.role());
				if (!profiles) continue;
				profiles->insert(ragent.info.profile(), ragent.agentId);
			}
		}

		break;
	}
	default:
		qWarning() << "Unknown message to capability socket";
		break;
	}
}

QHash<QString, int>* CapabilityPeer::profilesByRole(SAPServiceInfo::Role role)
{
	switch (role) {
	case SAPServiceInfo::RoleProvider:
		return &_providerProfiles;
	case SAPServiceInfo::RoleConsumer:
		return &_consumerProfiles;
	default:
		return 0;
	}
}