summaryrefslogtreecommitdiff
path: root/cpp/src/IcePack/ServerAdapterI.cpp
blob: 29aa1cea9c37c5fc1632ad1682a64ad116fdb0cf (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
// **********************************************************************
//
// Copyright (c) 2001
// ZeroC, Inc.
// Billerica, MA, USA
//
// All Rights Reserved.
//
// Ice is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License version 2 as published by
// the Free Software Foundation.
//
// **********************************************************************

#include <Ice/Ice.h>
#include <IcePack/ServerAdapterI.h>
#include <IcePack/ServerFactory.h>
#include <IcePack/TraceLevels.h>

using namespace std;
using namespace IcePack;

IcePack::ServerAdapterI::ServerAdapterI(const ServerFactoryPtr& factory, const TraceLevelsPtr& traceLevels, 
					Ice::Int waitTime) :
    _factory(factory),
    _traceLevels(traceLevels),
    _waitTime(waitTime)
{
}

IcePack::ServerAdapterI::~ServerAdapterI()
{
}

Ice::ObjectPrx
IcePack::ServerAdapterI::getDirectProxy(bool activate, const Ice::Current& current)
{
    ::IceUtil::Monitor< ::IceUtil::Mutex>::Lock sync(*this);

    if(!_proxy && activate)
    {
	//
	// Try to start the server and wait for the adapter proxy to
	// be changed.
	//

	if(_traceLevels->adapter > 2)
	{
	    Ice::Trace out(_traceLevels->logger, _traceLevels->adapterCat);
	    out << "waiting for activation of server adapter `" << name << "'";
	}
	
	try
	{
	    if(theServer->start(OnDemand))
	    {
		_notified = false;

		while(!_notified)
		{
		    bool notify = timedWait(IceUtil::Time::seconds(_waitTime));
		    if(!notify)
		    {
			if(_traceLevels->adapter > 1)
			{
			    Ice::Trace out(_traceLevels->logger, _traceLevels->adapterCat);
			    out << "server adapter `" << name << "' activation timed out";
			}
		    }
		}
	    }
	    else
	    {
		if(_traceLevels->adapter > 1)
		{
		    Ice::Trace out(_traceLevels->logger, _traceLevels->adapterCat);
		    out << "server adapter `" << name << "' activation failed, couldn't start the server";
		}
	    }
	}
	catch(const Ice::ObjectNotExistException&)
	{
	    //
	    // The server associated to this adapter doesn't exist
	    // anymore. Somehow the database is inconsistent if this
	    // happens. The best thing to do is to destroy the adapter
	    // and throw an ObjectNotExist exception.
	    //
	    destroy(current);
	    throw Ice::ObjectNotExistException(__FILE__, __LINE__);
	}
    }
    return _proxy;
}

void
IcePack::ServerAdapterI::setDirectProxy(const Ice::ObjectPrx& prx, const Ice::Current&)
{
    ::IceUtil::Monitor< ::IceUtil::Mutex>::Lock sync(*this);

    //
    // If the adapter proxy is not null the given proxy can only be
    // null. We don't allow to overide an existing proxy by another
    // non null proxy if the server is active.
    //
    if(prx && _proxy)
    {
	if(theServer->getState() == Active)
	{
	    throw AdapterActiveException();
	}
    }

    _proxy = prx;
    _notified = true;

    if(_traceLevels->adapter > 1)
    {
	Ice::Trace out(_traceLevels->logger, _traceLevels->adapterCat);
	out << "server adapter `" << name << "' " << (_proxy ? "activated" : "deactivated");
    }
    
    notifyAll();
}

void
IcePack::ServerAdapterI::destroy(const Ice::Current& current)
{
    _factory->destroy(this, current.id);
}