summaryrefslogtreecommitdiff
path: root/cpp/src/IceGrid/InternalRegistryI.cpp
blob: 43332e40a1ff7f443d20799ee2ee6eb0a946a49d (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
// **********************************************************************
//
// Copyright (c) 2003-2006 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 <Ice/Ice.h>

#include <IceGrid/InternalRegistryI.h>
#include <IceGrid/Database.h>
#include <IceGrid/ReapThread.h>
#include <IceGrid/Topics.h>
#include <IceGrid/NodeSessionI.h>

using namespace std;
using namespace IceGrid;

namespace IceGrid
{

class NodeSessionReapable : public Reapable
{
public:

    NodeSessionReapable(const Ice::ObjectAdapterPtr& adapter, 
			const NodeSessionIPtr& session, 
			const NodeSessionPrx& proxy) : 
	_adapter(adapter),
	_session(session),
	_proxy(proxy)
    {
    }

    virtual ~NodeSessionReapable()
    {
    }
	
    virtual IceUtil::Time
    timestamp() const
    {
	return _session->timestamp();
    }

    virtual void
    destroy(bool destroy)
    {
	try
	{
	    //
	    // Invoke on the servant directly instead of the
	    // proxy. Invoking on the proxy might not always work if the
	    // communicator is being shutdown/destroyed. We have to create
	    // a fake "current" because the session destroy methods needs
	    // the adapter and object identity to unregister the servant
	    // from the adapter.
	    //
	    Ice::Current current;
	    if(!destroy)
	    {
		current.adapter = _adapter;
		current.id = _proxy->ice_getIdentity();
	    }
	    _session->destroy(current);
	}
	catch(const Ice::ObjectNotExistException&)
	{
	}
	catch(const Ice::LocalException& ex)
	{
	    Ice::Warning out(_proxy->ice_getCommunicator()->getLogger());
	    out << "unexpected exception while reaping node session:\n" << ex;
	}
    }

private:

    const Ice::ObjectAdapterPtr _adapter;
    const NodeSessionIPtr _session;
    const NodeSessionPrx _proxy;
};

}

InternalRegistryI::InternalRegistryI(const DatabasePtr& database,
				     const ReapThreadPtr& reaper, 
				     const NodeObserverPrx& observer,
				     int timeout) : 
    _database(database), 
    _nodeReaper(reaper),
    _nodeObserver(observer),
    _nodeSessionTimeout(timeout)
{
}

InternalRegistryI::~InternalRegistryI()
{
}

NodeSessionPrx
InternalRegistryI::registerNode(const std::string& name,
				const NodePrx& node, 
				const NodeInfo& info,
				const Ice::Current& current)
{
    NodeSessionIPtr session = new NodeSessionI(_database, name, node, info, _nodeObserver, _nodeSessionTimeout);
    NodeSessionPrx proxy = NodeSessionPrx::uncheckedCast(current.adapter->addWithUUID(session));
    _nodeReaper->add(new NodeSessionReapable(current.adapter, session, proxy));
    return proxy;
}