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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
// **********************************************************************
//
// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <IceDiscovery/LocatorI.h>
#include <IceDiscovery/LookupI.h>
#include <Ice/LocalException.h>
#include <Ice/Communicator.h>
#include <Ice/ObjectAdapter.h>
using namespace std;
using namespace Ice;
using namespace IceDiscovery;
LocatorRegistryI::LocatorRegistryI(const Ice::CommunicatorPtr& com) :
_wellKnownProxy(com->stringToProxy("p")->ice_locator(0)->ice_router(0)->ice_collocationOptimized(true))
{
}
void
LocatorRegistryI::setAdapterDirectProxy_async(const AMD_LocatorRegistry_setAdapterDirectProxyPtr& cb,
const std::string& adapterId,
const ObjectPrx& proxy,
const Current&)
{
Lock sync(*this);
if(proxy)
{
_adapters[adapterId] = proxy;
}
else
{
_adapters.erase(adapterId);
}
cb->ice_response();
}
void
LocatorRegistryI::setReplicatedAdapterDirectProxy_async(
const AMD_LocatorRegistry_setReplicatedAdapterDirectProxyPtr& cb,
const std::string& adapterId,
const std::string& replicaGroupId,
const ObjectPrx& proxy,
const Current&)
{
Lock sync(*this);
if(proxy)
{
_adapters[adapterId] = proxy;
map<string, set<string> >::iterator p = _replicaGroups.find(replicaGroupId);
if(p == _replicaGroups.end())
{
p = _replicaGroups.insert(make_pair(replicaGroupId, set<string>())).first;
}
p->second.insert(adapterId);
}
else
{
_adapters.erase(adapterId);
map<string, set<string> >::iterator p = _replicaGroups.find(replicaGroupId);
if(p != _replicaGroups.end())
{
p->second.erase(adapterId);
if(p->second.empty())
{
_replicaGroups.erase(p);
}
}
}
cb->ice_response();
}
void
LocatorRegistryI::setServerProcessProxy_async(const AMD_LocatorRegistry_setServerProcessProxyPtr& cb,
const std::string&,
const ProcessPrx&,
const Current&)
{
cb->ice_response();
}
Ice::ObjectPrx
LocatorRegistryI::findObject(const Ice::Identity& id) const
{
Lock sync(*this);
if(id.name.empty())
{
return 0;
}
Ice::ObjectPrx prx = _wellKnownProxy->ice_identity(id);
vector<string> adapterIds;
for(map<string, set<string> >::const_iterator p = _replicaGroups.begin(); p != _replicaGroups.end(); ++p)
{
try
{
prx->ice_adapterId(p->first)->ice_ping();
adapterIds.push_back(p->first);
}
catch(const Ice::Exception&)
{
// Ignore
}
}
if(adapterIds.empty())
{
for(map<string, Ice::ObjectPrx>::const_iterator p = _adapters.begin(); p != _adapters.end(); ++p)
{
try
{
prx->ice_adapterId(p->first)->ice_ping();
adapterIds.push_back(p->first);
}
catch(const Ice::Exception&)
{
// Ignore
}
}
}
if(adapterIds.empty())
{
return 0;
}
random_shuffle(adapterIds.begin(), adapterIds.end());
return prx->ice_adapterId(adapterIds[0]);
}
Ice::ObjectPrx
LocatorRegistryI::findAdapter(const string& adapterId, bool& isReplicaGroup) const
{
Lock sync(*this);
map<string, Ice::ObjectPrx>::const_iterator p = _adapters.find(adapterId);
if(p != _adapters.end())
{
isReplicaGroup = false;
return p->second;
}
map<string, set<string> >::const_iterator q = _replicaGroups.find(adapterId);
if(q != _replicaGroups.end())
{
Ice::EndpointSeq endpoints;
Ice::ObjectPrx prx;
for(set<string>::const_iterator r = q->second.begin(); r != q->second.end(); ++r)
{
map<string, Ice::ObjectPrx>::const_iterator s = _adapters.find(*r);
if(s == _adapters.end())
{
continue; // TODO: Inconsistency
}
if(!prx)
{
prx = s->second;
}
Ice::EndpointSeq endpts = s->second->ice_getEndpoints();
copy(endpts.begin(), endpts.end(), back_inserter(endpoints));
}
if(prx)
{
isReplicaGroup = true;
return prx->ice_endpoints(endpoints);
}
}
isReplicaGroup = false;
return 0;
}
LocatorI::LocatorI(const LookupIPtr& lookup, const LocatorRegistryPrx& registry) : _lookup(lookup), _registry(registry)
{
}
void
LocatorI::findObjectById_async(const AMD_Locator_findObjectByIdPtr& cb,
const Identity& id,
const Current&) const
{
_lookup->findObject(cb, id);
}
void
LocatorI::findAdapterById_async(const AMD_Locator_findAdapterByIdPtr& cb,
const std::string& adapterId,
const Current&) const
{
_lookup->findAdapter(cb, adapterId);
}
LocatorRegistryPrx
LocatorI::getRegistry(const Current&) const
{
return _registry;
}
|