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
|
// **********************************************************************
//
// Copyright (c) 2003-2005 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 <IceUtil/Functional.h>
#include <Ice/LoggerUtil.h>
#include <IceGrid/NodeCache.h>
#include <IceGrid/NodeSessionI.h>
#include <IceGrid/ServerCache.h>
using namespace std;
using namespace IceGrid;
NodeCache::NodeCache(int sessionTimeout) : _sessionTimeout(sessionTimeout)
{
}
NodeEntryPtr
NodeCache::get(const string& name, bool create) const
{
Lock sync(*this);
NodeCache& self = const_cast<NodeCache&>(*this);
NodeEntryPtr entry = self.getImpl(name, create);
if(!entry)
{
NodeNotExistException ex;
ex.name = name;
throw ex;
}
return entry;
}
NodeEntry::NodeEntry(Cache<string, NodeEntry>& cache, const std::string& name) :
_cache(cache),
_name(name)
{
}
void
NodeEntry::addDescriptor(const string& application, const NodeDescriptor& descriptor)
{
Lock sync(*this);
_descriptors.insert(make_pair(application, descriptor));
}
void
NodeEntry::removeDescriptor(const string& application)
{
bool remove = false;
{
Lock sync(*this);
_descriptors.erase(application);
remove = _servers.empty() && !_session && _descriptors.empty();
}
if(remove)
{
_cache.remove(_name);
}
}
void
NodeEntry::addServer(const ServerEntryPtr& entry)
{
Lock sync(*this);
_servers.insert(make_pair(entry->getId(), entry));
}
void
NodeEntry::removeServer(const ServerEntryPtr& entry)
{
bool remove = false;
{
Lock sync(*this);
_servers.erase(entry->getId());
remove = _servers.empty() && !_session && _descriptors.empty();
}
if(remove)
{
_cache.remove(_name);
}
}
void
NodeEntry::setSession(const NodeSessionIPtr& session)
{
bool remove = false;
{
Lock sync(*this);
if(session && _session)
{
throw NodeActiveException();
}
_session = session;
remove = _servers.empty() && !_session && _descriptors.empty();
}
if(remove)
{
_cache.remove(_name);
}
if(session)
{
if(_cache.getTraceLevels() && _cache.getTraceLevels()->node > 0)
{
Ice::Trace out(_cache.getTraceLevels()->logger, _cache.getTraceLevels()->nodeCat);
out << "node `" << _name << "' up";
}
ServerEntrySeq entries;
{
Lock sync(*this);
for(map<string, ServerEntryPtr>::const_iterator q = _servers.begin() ; q != _servers.end(); ++q)
{
entries.push_back(q->second);
}
}
for_each(entries.begin(), entries.end(), IceUtil::voidMemFun(&ServerEntry::sync));
}
else
{
if(_cache.getTraceLevels() && _cache.getTraceLevels()->node > 0)
{
Ice::Trace out(_cache.getTraceLevels()->logger, _cache.getTraceLevels()->nodeCat);
out << "node `" << _name << "' down";
}
}
}
NodePrx
NodeEntry::getProxy() const
{
Lock sync(*this);
if(!_session)
{
throw NodeUnreachableException(_name, "node is not registered");
}
return _session->getNode();
}
Ice::StringSeq
NodeEntry::getServers() const
{
Lock sync(*this);
Ice::StringSeq names;
for(map<string, ServerEntryPtr>::const_iterator p = _servers.begin(); p != _servers.end(); ++p)
{
names.push_back(p->second->getId());
}
return names;
}
LoadInfo
NodeEntry::getLoadInfoAndLoadFactor(const string& application, float& loadFactor) const
{
Lock sync(*this);
if(!_session)
{
throw NodeUnreachableException(_name, "node is not registered");
}
map<string, NodeDescriptor>::const_iterator p = _descriptors.find(application);
loadFactor = -1.0f;
if(p != _descriptors.end())
{
if(!p->second.loadFactor.empty())
{
istringstream is(p->second.loadFactor);
is >> loadFactor;
}
}
return _session->getLoadInfo();
}
bool
NodeEntry::canRemove()
{
Lock sync(*this);
return !_session && _servers.empty();
}
|