blob: c9d7df6dac93d204d051c3c1b7a20f9a027e47f0 (
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
|
#include <QtCore/QDebug>
#include "saprotocol.h"
#include "sapmanager.h"
static SAPManager *manager = 0;
SAPManager::SAPManager(QObject *parent) :
QObject(parent)
{
}
SAPManager * SAPManager::instance()
{
if (!manager) {
manager = new SAPManager;
}
return manager;
}
int SAPManager::registerServiceAgent(const SAPServiceInfo &service, SAPAgent *agent)
{
const QString profile = service.profile();
const SAPServiceInfo::Role role = service.role();
QHash<QString, int> *profiles = profilesByRole(role);
if (!profiles) return -1;
if (profile.isEmpty()) return -1;
if (profiles->contains(profile)) return -1;
int agentId = findUnusedAgentId();
if (agentId < 0) return -1;
RegisteredAgent ragent;
ragent.agentId = agentId;
ragent.agent = agent;
ragent.info = service;
_agents.insert(agentId, ragent);
profiles->insert(profile, agentId);
return agentId;
}
void SAPManager::unregisterServiceAgent(int agentId)
{
if (_agents.contains(agentId)) {
const RegisteredAgent &ragent = _agents[agentId];
const QString profile = ragent.info.profile();
const SAPServiceInfo::Role role = ragent.info.role();
QHash<QString, int> *profiles = profilesByRole(role);
Q_ASSERT(profiles);
profiles->remove(profile);
_agents.remove(agentId);
}
}
void SAPManager::unregisterServiceAgent(const QString &profile, SAPServiceInfo::Role role)
{
int agentId = registeredAgentId(profile, role);
if (agentId >= 0) {
unregisterServiceAgent(agentId);
}
}
int SAPManager::registeredAgentId(const QString &profile, SAPServiceInfo::Role role)
{
QHash<QString, int> *profiles = profilesByRole(role);
if (!profiles) return -1;
return profiles->value(profile, -1);
}
bool SAPManager::isRegisteredAgent(int agentId) const
{
return _agents.contains(agentId);
}
SAPAgent * SAPManager::agent(int agentId)
{
if (!_agents.contains(agentId)) return 0;
return _agents.value(agentId).agent;
}
SAPServiceInfo SAPManager::serviceInfo(int agentId) const
{
return _agents.value(agentId).info;
}
QSet<QString> SAPManager::allProfiles()
{
return QSet<QString>::fromList(_consumerProfiles.keys())
+ QSet<QString>::fromList(_providerProfiles.keys());
}
int SAPManager::findUnusedAgentId() const
{
if (_agents.size() > 20000) {
qWarning() << "Ran out of agent ids!";
return -1;
}
int id = 1;
while (_agents.contains(id)) {
id++;
}
return id;
}
QHash<QString, int>* SAPManager::profilesByRole(SAPServiceInfo::Role role)
{
switch (role) {
case SAPServiceInfo::RoleProvider:
return &_providerProfiles;
case SAPServiceInfo::RoleConsumer:
return &_consumerProfiles;
default:
return 0;
}
}
|