summaryrefslogtreecommitdiff
path: root/cpp/src/IcePack/ServerI.cpp
blob: 5f31a22bdfc5a7c98bea23e796d07e7f235176df (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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// **********************************************************************
//
// Copyright (c) 2001
// Mutable Realms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************

#include <Ice/Ice.h>
#include <IcePack/ServerI.h>
#include <IcePack/ServerFactory.h>
#include <IcePack/TraceLevels.h>
#include <IcePack/Activator.h>

#include <IceBox/IceBox.h>
#include "../Ice/Reference.h"

using namespace std;
using namespace IcePack;

IcePack::ServerI::ServerI(const ServerFactoryPtr& factory, const TraceLevelsPtr& traceLevels, 
			  const ActivatorPtr& activator, Ice::Int waitTime) :
    _factory(factory),
    _traceLevels(traceLevels),
    _activator(activator),
    _waitTime(waitTime),
    _state(Inactive)
{
    assert(_activator);

}

IcePack::ServerI::~ServerI()
{
}

ServerDescription
IcePack::ServerI::getServerDescription(const Ice::Current&)
{
    return description; // Description is immutable.
}

bool
IcePack::ServerI::start(ServerActivation act, const Ice::Current& current)
{
    while(true)
    {
	IceUtil::Monitor< IceUtil::Mutex>::Lock sync(*this);
	switch(_state)
	{
	case Inactive:
	{
	    if(act < activation)
	    {
	        return false;
	    }

	    _state = Activating;
	    break;
	}
	case Activating:
	{
	    wait(); // TODO: Timeout?
	    continue;
	}
 	case Active:
	case Deactivating:
	{
	    return true; // Raise an exception instead?
	}
	case Destroying:
	case Destroyed:
	{
	    Ice::ObjectNotExistException ex(__FILE__,__LINE__);
	    ex.id = current.id;
	    throw ex;
	}
	}

	if(_traceLevels->server > 2)
	{
	    Ice::Trace out(_traceLevels->logger, _traceLevels->serverCat);
	    out << "changed server `" << description.name << "' state to `Activating'";
	}
	assert(_state == Activating);
	break;
    }

    try
    {
	bool active  = _activator->activate(this);
	setState(active ? Active : Inactive);
	return active;
    }
    catch (const Ice::SyscallException& ex)
    {
	Ice::Warning out(_traceLevels->logger);
	out << "activation failed for server `" << description.name << "':\n";
	out << ex;

	setState(Inactive);
	return false;
    }
}

void
IcePack::ServerI::stop(const Ice::Current& current)
{
    while(true)
    {
	IceUtil::Monitor< IceUtil::Mutex>::Lock sync(*this);
	switch(_state)
	{
	case Inactive:
	{
	    return;
	}
	case Activating:
	{
	    wait(); // TODO: Timeout?
	    continue;
	}
 	case Active:
	{	    
	    _state = Deactivating;
	    break;
	}
	case Deactivating:
	{
	    wait();
	    continue;
	}
	case Destroying:
	case Destroyed:
	{
	    Ice::ObjectNotExistException ex(__FILE__,__LINE__);
	    ex.id = current.id;
	    throw ex;
	}
	}

	if(_traceLevels->server > 2)
	{
	    Ice::Trace out(_traceLevels->logger, _traceLevels->serverCat);
	    out << "changed server `" << description.name << "' state to `Deactivating'";
	}
	assert(_state == Deactivating);
	break;
    }

    stopInternal();
}

void
IcePack::ServerI::destroy(const Ice::Current& current)
{
    bool stop = false;

    while(true)
    {
	IceUtil::Monitor< IceUtil::Mutex>::Lock sync(*this);
	switch(_state)
	{
	case Inactive:
	{
	    _state = Destroyed;
	    break;
	}
 	case Active:
	{
	    stop = true;
	    _state = Destroying;
	    break;
	}
	case Activating:
	case Deactivating:
	{
	    wait(); // TODO: Timeout?
	    continue;
	}
	case Destroying:
	case Destroyed:
	{
	    Ice::ObjectNotExistException ex(__FILE__,__LINE__);
	    ex.id = current.id;
	    throw ex;
	}
	}

	assert(_state == Destroyed || _state == Destroying);

	if(_traceLevels->server > 2)
	{
	    Ice::Trace out(_traceLevels->logger, _traceLevels->serverCat);
	    out << "changed server `" << description.name << "' state to `";
	    out << (_state == Destroyed ? "Destroyed" : "Destroying") << "'";
	}
	break;
    }

    if(stop)
    {
	stopInternal();
    }

    //
    // Destroy the server adapters.
    //
    for(ServerAdapters::const_iterator p = adapters.begin(); p != adapters.end(); ++p)
    {
	try
	{
	    (*p)->destroy();
	}
	catch(const Ice::ObjectNotExistException&)
	{
	}
    }

    _factory->destroy(this, current.id);
}

void
IcePack::ServerI::terminated(const Ice::Current&)
{
    ServerState newState;

    {
	IceUtil::Monitor< IceUtil::Mutex>::Lock sync(*this);
	switch(_state)
	{
	case Inactive:
	case Activating:
	{
	    assert(false);
	}
	case Active:
	{
	    _state = Deactivating;
	    if(_traceLevels->server > 2)
	    {
		Ice::Trace out(_traceLevels->logger, _traceLevels->serverCat);
		out << "changed server `" << description.name << "' state to `Deactivating'";
	    }
	    newState = Inactive;
	    break;
	}
	case Deactivating:
	{
	    //
	    // Deactivation was initiated by the stop method.
	    //
	    newState = Inactive;
	    break;
	}
	case Destroying:
	{
	    //
	    // Deactivation was initiated by the destroy method.
	    //
	    newState = Destroyed;
	    break;
	}
	case Destroyed:
	{
	    assert(false);	    
	}
	}

	assert(_state == Deactivating || _state == Destroying);
    }

    if(newState != Destroyed)
    {
	//
	// The server has terminated, set its adapter direct proxies to
	// null to cause the server re-activation if one of its adapter
	// direct proxy is requested.
	//
	for(ServerAdapters::iterator p = adapters.begin(); p != adapters.end(); ++p)
	{
	    try
	    {
		(*p)->setDirectProxy(0);
	    }
	    catch(const Ice::ObjectNotExistException&)
	    {
		//
		// TODO: Inconsistent database.
		//
	    }
	    catch(const Ice::LocalException& ex)
	    {
		//cerr << (*p)->__reference()->toString() << endl;
		//cerr << ex << endl;
		throw;
	    }
	}
    }

    setState(newState);
}

ServerState
IcePack::ServerI::getState(const Ice::Current&)
{
    IceUtil::Monitor< ::IceUtil::Mutex>::Lock sync(*this);
    return _state;
}

Ice::Int
IcePack::ServerI::getPid(const Ice::Current& current)
{
    return _activator->getServerPid(this);
}

void 
IcePack::ServerI::setActivationMode(ServerActivation mode, const ::Ice::Current&)
{
    IceUtil::Monitor< ::IceUtil::Mutex>::Lock sync(*this);
    activation = mode;
}

ServerActivation 
IcePack::ServerI::getActivationMode(const ::Ice::Current&)
{
    IceUtil::Monitor< ::IceUtil::Mutex>::Lock sync(*this);
    return activation;
}

void
IcePack::ServerI::stopInternal()
{
    //
    // If the server is an icebox, first try to use the IceBox service
    // manager to shutdown the server.
    //
    bool deactivate = true;
    
    //
    // TODO: use the service manager to shutdown icebox servers. The
    // following code should work once it's possible to set an
    // activation mode on a per adapter basis -- we really don't want
    // to activate the icebox in this method if it's already
    // deactivated.
    //

//     if(description.serviceManager)
//     {
// 	if(_state == Deactivating)
// 	{
// 	    try
// 	    {
// 		//
// 		// Set a timeout on the service manager proxy and try to
// 		// shutdown the icebox.
// 		//
// 		IceBox::ServiceManagerPrx serviceManager = 
// 		    IceBox::ServiceManagerPrx::uncheckedCast(description.serviceManager->ice_timeout(_waitTime));
		
// 		serviceManager->shutdown();
		
// 		deactivate = false;
// 	    }
// 	    catch(const Ice::LocalException& ex)
// 	    {
// 		if(_traceLevels->server > 1)
// 		{
// 		    Ice::Trace out(_traceLevels->logger, _traceLevels->serverCat);
// 		    out << "couldn't contact the IceBox `" << description.name << "' service manager:\n";
// 		    out << ex;
// 		}
// 	    }
// 	}
//     }

    if(deactivate)
    {
	//
	// Deactivate the server by sending a SIGTERM.
	//
	try
	{
	    _activator->deactivate(this);
	}
	catch (const Ice::SyscallException& ex)
	{
	    Ice::Warning out(_traceLevels->logger);
	    out << "deactivation failed for server `" << description.name << "':\n";
	    out << ex;
	    
	    setState(Active);
	    return;
	}
    }

    //
    // Wait for the server to be inactive (the activator monitors the
    // process and should notify us when it detects the process
    // termination by calling the terminated() method).
    //
    {
	IceUtil::Monitor< IceUtil::Mutex>::Lock sync(*this);

	ServerState oldState = _state;

	while(true)
	{
	    if(_state == Inactive || _state == Destroyed)
	    {
		//
		// State changed to inactive or destroyed, the server
		// has been correctly deactivated, we can return.
		//
		return;
	    }
	    
	    //
	    // Wait for a notification.
	    //
	    bool notify = timedWait(IceUtil::Time::seconds(_waitTime));
	    if(!notify)
	    {
		assert(oldState == _state);
		break;
	    }
	}
    }
    
    if(_traceLevels->server > 1)
    {
	Ice::Trace out(_traceLevels->logger, _traceLevels->serverCat);
	out << "gracefull server shutdown failed, killing server `" << description.name << "'";
    }
    
    //
    // The server is still not inactive, kill it.
    //
    try
    {
	_activator->kill(this);
    }
    catch (const Ice::SyscallException& ex)
    {
	Ice::Warning out(_traceLevels->logger);
	out << "deactivation failed for server `" << description.name << "':\n";
	out << ex;
	
	setState(Active);
    }
}

void
IcePack::ServerI::setState(ServerState st)
{
    IceUtil::Monitor< ::IceUtil::Mutex>::Lock sync(*this);

    _state = st;

    if(_traceLevels->server > 1)
    {
	if(_state == Active)
	{
	    Ice::Trace out(_traceLevels->logger, _traceLevels->serverCat);
	    out << "changed server `" << description.name << "' state to `Active'";
	}
	else if(_state == Inactive)
	{
	    Ice::Trace out(_traceLevels->logger, _traceLevels->serverCat);
	    out << "changed server `" << description.name << "' state to `Inactive'";
	}
	else if(_state == Destroyed)
	{
	    Ice::Trace out(_traceLevels->logger, _traceLevels->serverCat);
	    out << "changed server `" << description.name << "' state to `Destroyed'";
	}
	else if(_traceLevels->server > 2)
	{
	    if(_state == Activating)
	    {
		Ice::Trace out(_traceLevels->logger, _traceLevels->serverCat);
		out << "changed server `" << description.name << "' state to `Activating'";
	    }
	    else if(_state == Deactivating)
	    {
		Ice::Trace out(_traceLevels->logger, _traceLevels->serverCat);
		out << "changed server `" << description.name << "' state to `Deactivating'";
	    }
	}
    }

    notifyAll();
}