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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
// **********************************************************************
//
// Copyright (c) 2001
// Mutable Realms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************
#include <Ice/Ice.h>
#include <Ice/Functional.h>
#include <Ice/LoggerUtil.h>
#include <IcePack/ServerManagerI.h>
#include <IcePack/AdapterManager.h>
#include <IcePack/Activator.h>
using namespace std;
using namespace Ice;
using namespace IcePack;
class ServerNameToServer
{
public:
ServerNameToServer(const ObjectAdapterPtr& adapter) :
_adapter(adapter)
{
}
ServerPrx
operator()(const string& name)
{
Identity ident;
ident.category = "server";
ident.name = name;
return ServerPrx::uncheckedCast(_adapter->createProxy(ident));
}
private:
ObjectAdapterPtr _adapter;
};
IcePack::ServerI::ServerI(const ObjectAdapterPtr& adapter, const ActivatorPrx& activator) :
_adapter(adapter),
_activator(activator)
{
}
IcePack::ServerI::~ServerI()
{
}
ServerDescription
IcePack::ServerI::getServerDescription(const Current&)
{
return _description;
}
bool
IcePack::ServerI::start(const Current&)
{
while(true)
{
IceUtil::Monitor< IceUtil::Mutex>::Lock sync(*this);
if(!_activator)
return false;
switch(_state)
{
case Inactive:
_state = Activating;
break;
case Activating:
wait(); // TODO: Timeout?
continue;
case Active:
return true; // Raise an exception instead?
case Deactivating:
wait();
continue;
case Destroyed:
throw ObjectNotExistException(__FILE__,__LINE__);
}
break;
}
try
{
bool activated = _activator->activate(ServerNameToServer(_adapter)(_description.name));
setState(activated ? Active : Inactive);
return activated;
}
catch (const SystemException& ex)
{
Warning out(_adapter->getCommunicator()->getLogger());
out << "activation failed for server `" << _description.name << "':\n";
out << ex;
setState(Inactive);
return false;
}
}
void
IcePack::ServerI::terminationCallback(const Current&)
{
//
// Callback from the activator indicating that the server
// stopped. Change state to deactivating while we mark the server
// adapters as inactive.
//
setState(Deactivating);
//
// Mark each adapter as inactive. _adapters is immutable when
// state == Deactivating.
//
for(Adapters::iterator p = _adapters.begin(); p != _adapters.end(); ++p)
{
(*p)->markAsInactive();
}
setState(Inactive);
}
ServerState
IcePack::ServerI::getState(const Current&)
{
IceUtil::Monitor< ::IceUtil::Mutex>::Lock sync(*this);
return _state;
}
void
IcePack::ServerI::setState(ServerState state)
{
IceUtil::Monitor< ::IceUtil::Mutex>::Lock sync(*this);
if(state == Destroyed && (_state == Active || _state == Deactivating))
throw ServerNotInactiveException();
_state = state;
notifyAll();
}
IcePack::ServerManagerI::ServerManagerI(const ObjectAdapterPtr& adapter,
const AdapterManagerPrx& adapterManager,
const ActivatorPrx& activator) :
_adapter(adapter),
_adapterManager(adapterManager),
_activator(activator)
{
}
IcePack::ServerManagerI::~ServerManagerI()
{
}
ServerPrx
IcePack::ServerManagerI::create(const ServerDescription& description, const Current&)
{
IceUtil::Mutex::Lock sync(*this);
ServerPrx server = ServerNameToServer(_adapter)(description.name);
try
{
server->ice_ping();
//
// The server already exists.
//
throw ServerExistsException();
}
catch (const ObjectNotExistException&)
{
}
//
// Create the server. Set its state to Activating so that we can
// safelly register the adapters without any race conditions. If a
// request comes in for an adapter we've just registerd, the
// server won't be started as long as we are not in the Inactive
// state.
//
ServerI* serverI = new ServerI(_adapter, _activator);
ServerPtr s = serverI;
serverI->_description = description;
serverI->_state = Activating;
//
// The server object might receives requests as soon as it returns
// from this call. This is the reason why we've created the server
// in the activating state -- to block any attempts to activate
// the server. The server state is set to inactive once it's fully
// created.
//
server = ServerPrx::uncheckedCast(_adapter->add(serverI, server->ice_getIdentity()));
try
{
//
// Register the server adapters to enabled automatic
// activation. If an adapter already exists, rollback the
// server creation and throw an exception.
//
for(AdapterNames::const_iterator p = description.adapters.begin(); p != description.adapters.end(); ++p)
{
AdapterDescription desc;
desc.name = (*p);
desc.server = server;
serverI->_adapters.push_back(_adapterManager->create(desc));
}
}
catch (const AdapterExistsException&)
{
//
// The adapter is already registered with a server, remove the
// server.
//
_adapter->remove(server->ice_getIdentity());
serverI->setState(Destroyed);
throw;
}
//
// Set the server state as inactive. At this point the server can
// be automatically started if a request for one of its adapter
// comes in.
//
serverI->setState(Inactive);
//
// Add this server name to our server names internal list.
//
_serverNames.insert(description.name);
return server;
}
ServerPrx
IcePack::ServerManagerI::findByName(const string& name, const Current&)
{
IceUtil::Mutex::Lock sync(*this);
ServerPrx server = ServerNameToServer(_adapter)(name);
try
{
server->ice_ping();
return server;
}
catch (const ObjectNotExistException&)
{
return 0;
}
}
void
IcePack::ServerManagerI::remove(const string& name, const Current&)
{
IceUtil::Mutex::Lock sync(*this);
ServerPrx server = ServerNameToServer(_adapter)(name);
try
{
server->ice_ping();
}
catch (const ObjectNotExistException&)
{
throw ServerNotExistException();
}
//
// Mark the server as destroyed.
//
ServerI* serverI = dynamic_cast<ServerI*>(_adapter->proxyToServant(server).get());
assert(serverI);
serverI->setState(Destroyed);
//
// Remove server adapters.
//
ServerDescription description = serverI->_description;
for(AdapterNames::iterator p = description.adapters.begin(); p != description.adapters.end(); ++p)
{
_adapterManager->remove(*p);
}
_adapter->remove(server->ice_getIdentity());
//
// Remove the server name from our internal server name set.
//
_serverNames.erase(_serverNames.find(name));
}
ServerNames
IcePack::ServerManagerI::getAll(const Current&)
{
IceUtil::Mutex::Lock sync(*this);
ServerNames names;
names.reserve(_serverNames.size());
copy(_serverNames.begin(), _serverNames.end(), back_inserter(names));
return names;
}
|