summaryrefslogtreecommitdiff
path: root/cpp/src/IcePack/ServerAdapterI.cpp
blob: 3b14c7a25a8cb1ad77df164daea0a5295c276773 (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
// **********************************************************************
//
// Copyright (c) 2003 - 2004
// ZeroC, Inc.
// North Palm Beach, FL, USA
//
// 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 <Ice/Ice.h>
#include <IcePack/ServerAdapterI.h>
#include <IcePack/ServerFactory.h>
#include <IcePack/TraceLevels.h>
#include <IcePack/WaitQueue.h>

using namespace std;
using namespace IcePack;

namespace IcePack
{

class WaitForAdapterActivation : public WaitItem
{
public:
 
    WaitForAdapterActivation(const ServerAdapterPtr& adapter, 
			     const TraceLevelsPtr traceLevels,
			     const AMD_Adapter_getDirectProxyPtr& cb) : 
	WaitItem(adapter),
	_adapter(adapter),
	_traceLevels(traceLevels),
	_cb(cb)
    {
    }
    
    virtual void execute()
    {
	_adapter->getDirectProxy_async(_cb, false);
    }

    virtual void expired(bool destroyed)
    {
	if(_traceLevels->adapter > 1)
	{
	    Ice::Trace out(_traceLevels->logger, _traceLevels->adapterCat);
	    out << "server adapter `" << _adapter->id << "' activation timed out";
	}
	_cb->ice_response(0);
    }

private:
    
    ServerAdapterPtr _adapter;
    TraceLevelsPtr _traceLevels;
    AMD_Adapter_getDirectProxyPtr _cb;
};

}

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

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

void
IcePack::ServerAdapterI::getDirectProxy_async(const AMD_Adapter_getDirectProxyPtr& cb,
					      bool activate, 
					      const Ice::Current& current)
{
    {
	::IceUtil::Mutex::Lock sync(*this);
	if(_proxy || !activate)
	{
	    //
	    // Return the adapter direct proxy.
	    //
	    cb->ice_response(_proxy);
	    return;
	}

	if(_traceLevels->adapter > 2)
	{
	    Ice::Trace out(_traceLevels->logger, _traceLevels->adapterCat);
	    out << "waiting for activation of server adapter `" << id << "'";
	}
    }

    //
    // Try to start the server. Note that we start the server outside
    // the synchronization block since start() can block and callback
    // on this adapter (when the server is deactivating for example).
    //
    try
    {
	if(svr->start(OnDemand))
	{
	    //
	    // Now that the server is activated, wait for the adapter
	    // direct proxy to be set.
	    //
	    ::IceUtil::Mutex::Lock sync(*this);
	    if(!_proxy)
	    {
		_factory->getWaitQueue()->add(new WaitForAdapterActivation(this, _traceLevels, cb), _waitTime);
		return;
	    }
	}
    }
    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);

	Ice::ObjectNotExistException ex(__FILE__,__LINE__);
	ex.id = current.id;
	throw ex;
    }

    //
    // The server couldn't be activated, trace and return the current
    // adapter proxy.
    //
    {
	::IceUtil::Mutex::Lock sync(*this);
	if(_traceLevels->adapter > 1)
	{
	    Ice::Trace out(_traceLevels->logger, _traceLevels->adapterCat);
	    out << "server adapter `" << id << "' activation failed, couldn't start the server";
	}

	cb->ice_response(_proxy);
    }   
}

void
IcePack::ServerAdapterI::setDirectProxy(const Ice::ObjectPrx& prx, const Ice::Current&)
{
    ::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(svr->getState() == Active)
	{
	    throw AdapterActiveException();
	}
    }

    _proxy = prx;
    _notified = true;

    if(_traceLevels->adapter > 1)
    {
	Ice::Trace out(_traceLevels->logger, _traceLevels->adapterCat);
	out << "server adapter `" << id << "' " << (_proxy ? "activated" : "deactivated");
    }
    
    _factory->getWaitQueue()->notifyAllWaitingOn(this);
}

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