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
209
210
211
212
213
|
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
#include <IceUtil/Random.h>
#include <Ice/Communicator.h>
#include <Ice/LoggerUtil.h>
#include <Ice/LocalException.h>
#include <IceGrid/ObjectCache.h>
#include <IceGrid/NodeSessionI.h>
#include <IceGrid/ServerCache.h>
#include <IceGrid/SessionI.h>
using namespace std;
using namespace IceGrid;
namespace IceGrid
{
bool compareObjectEntryCI(const shared_ptr<ObjectEntry>& lhs, const shared_ptr<ObjectEntry>& rhs)
{
return Ice::proxyIdentityLess(lhs->getProxy(), rhs->getProxy());
}
bool compareObjectLoadCI(const pair<Ice::ObjectPrx, float>& lhs, const pair<Ice::ObjectPrx, float>& rhs)
{
return lhs.second < rhs.second;
}
};
void
ObjectCache::TypeEntry::add(const shared_ptr<ObjectEntry>& obj)
{
//
// No mutex protection here, this is called with the cache locked.
//
_objects.insert(lower_bound(_objects.begin(), _objects.end(), obj, compareObjectEntryCI), obj);
}
bool
ObjectCache::TypeEntry::remove(const shared_ptr<ObjectEntry>& obj)
{
//
// No mutex protection here, this is called with the cache locked.
//
auto q = lower_bound(_objects.begin(), _objects.end(), obj, compareObjectEntryCI);
assert(q->get() == obj.get());
_objects.erase(q);
return _objects.empty();
}
ObjectCache::ObjectCache(const shared_ptr<Ice::Communicator>& communicator) : _communicator(communicator)
{
}
void
ObjectCache::add(const ObjectInfo& info, const string& application, const string& server)
{
const Ice::Identity& id = info.proxy->ice_getIdentity();
lock_guard lock(_mutex);
if(getImpl(id))
{
Ice::Error out(_communicator->getLogger());
out << "can't add duplicate object `" << _communicator->identityToString(id) << "'";
return;
}
auto entry = make_shared<ObjectEntry>(info, application, server);
addImpl(id, entry);
map<string, TypeEntry>::iterator p = _types.find(entry->getType());
if(p == _types.end())
{
p = _types.insert(p, { entry->getType(), TypeEntry() });
}
p->second.add(entry);
if(_traceLevels && _traceLevels->object > 0)
{
Ice::Trace out(_traceLevels->logger, _traceLevels->objectCat);
out << "added object `" << _communicator->identityToString(id) << "'";
}
}
shared_ptr<ObjectEntry>
ObjectCache::get(const Ice::Identity& id) const
{
lock_guard lock(_mutex);
shared_ptr<ObjectEntry> entry = getImpl(id);
if(!entry)
{
throw ObjectNotRegisteredException(id);
}
return entry;
}
void
ObjectCache::remove(const Ice::Identity& id)
{
lock_guard lock(_mutex);
shared_ptr<ObjectEntry> entry = getImpl(id);
if(!entry)
{
Ice::Error out(_communicator->getLogger());
out << "can't remove unknown object `" << _communicator->identityToString(id) << "'";
return;
}
removeImpl(id);
map<string, TypeEntry>::iterator p = _types.find(entry->getType());
assert(p != _types.end());
if(p->second.remove(entry))
{
_types.erase(p);
}
if(_traceLevels && _traceLevels->object > 0)
{
Ice::Trace out(_traceLevels->logger, _traceLevels->objectCat);
out << "removed object `" << _communicator->identityToString(id) << "'";
}
}
vector<shared_ptr<ObjectEntry>>
ObjectCache::getObjectsByType(const string& type)
{
lock_guard lock(_mutex);
map<string, TypeEntry>::const_iterator p = _types.find(type);
if(p == _types.end())
{
return vector<shared_ptr<ObjectEntry>>();
}
return p->second.getObjects();
}
ObjectInfoSeq
ObjectCache::getAll(const string& expression)
{
lock_guard lock(_mutex);
ObjectInfoSeq infos;
for(auto p = _entries.cbegin(); p != _entries.cend(); ++p)
{
if(expression.empty() || IceUtilInternal::match(_communicator->identityToString(p->first), expression, true))
{
infos.push_back(p->second->getObjectInfo());
}
}
return infos;
}
ObjectInfoSeq
ObjectCache::getAllByType(const string& type)
{
lock_guard lock(_mutex);
ObjectInfoSeq infos;
map<string, TypeEntry>::const_iterator p = _types.find(type);
if(p == _types.end())
{
return infos;
}
const vector<shared_ptr<ObjectEntry>>& objects = p->second.getObjects();
for(vector<shared_ptr<ObjectEntry>>::const_iterator q = objects.begin(); q != objects.end(); ++q)
{
infos.push_back((*q)->getObjectInfo());
}
return infos;
}
ObjectEntry::ObjectEntry(const ObjectInfo& info, const string& application, const string& server) :
_info(info),
_application(application),
_server(server)
{
}
shared_ptr<Ice::ObjectPrx>
ObjectEntry::getProxy() const
{
return _info.proxy;
}
string
ObjectEntry::getType() const
{
return _info.type;
}
string
ObjectEntry::getApplication() const
{
return _application;
}
string
ObjectEntry::getServer() const
{
return _server;
}
const ObjectInfo&
ObjectEntry::getObjectInfo() const
{
return _info;
}
bool
ObjectEntry::canRemove()
{
return true;
}
|