summaryrefslogtreecommitdiff
path: root/cpp/src/IceGrid/IceGridObserver.cpp
blob: d0679587a2e627ace00d09e6ccf9d2bc68dd1586 (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
// **********************************************************************
//
// Copyright (c) 2003-2005 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 <IceUtil/Options.h>
#include <IceUtil/Thread.h>
#include <Ice/Application.h>
#include <Ice/SliceChecksums.h>
#include <IceGrid/Observer.h>
#include <IceGrid/Admin.h>
#include <fstream>

using namespace std;
using namespace Ice;
using namespace IceGrid;

class SessionKeepAliveThread : public IceUtil::Thread, public IceUtil::Monitor<IceUtil::Mutex>
{
public:

    SessionKeepAliveThread(const Ice::LoggerPtr& logger, const IceUtil::Time& timeout, const SessionPrx& session) :
	_logger(logger),
	_session(session),
	_timeout(timeout),
	_terminated(false)
    {
    }

    virtual void
    run()
    {
	Lock sync(*this);
	while(!_terminated)
	{
	    timedWait(_timeout);
	    if(!_terminated)
	    {
		try
		{
		    _session->keepAlive();
		}
		catch(const Ice::Exception& ex)
		{
		    Ice::Warning warn(_logger);
		    warn << "SessionKeepAliveThread: " << ex;
    	    	    _terminated = true;
		}
	    }
	}
    }

    void
    terminate()
    {
	Lock sync(*this);
	_terminated = true;
	notify();
    }

private:

    const Ice::LoggerPtr _logger;
    const SessionPrx _session;
    const IceUtil::Time _timeout;
    bool _terminated;
};
typedef IceUtil::Handle<SessionKeepAliveThread> SessionKeepAliveThreadPtr;

class RegistryObserverI : public RegistryObserver
{
public:

    virtual void 
    init(int, const ApplicationDescriptorSeq&, const Ice::StringSeq& nodes, const Ice::Current&)
    {
	cout << "active nodes: ";
	for(Ice::StringSeq::const_iterator p = nodes.begin(); p != nodes.end(); ++p)
	{
	    cout << *p << " ";
	}
	cout << endl;
    }

    virtual void
    applicationAdded(int serial, const ApplicationDescriptorPtr& app, const Ice::Current&)
    {
	cout << "application `" << app->name << "' added (serial = " << serial << ")" << endl;
    }

    virtual void 
    applicationRemoved(int serial, const std::string& name, const Ice::Current&)
    {
	cout << "application `" << name << "' removed (serial = " << serial << ")" << endl;
    }

    virtual void 
    applicationUpdated(int serial, const ApplicationUpdateDescriptor& desc, const Ice::Current&)
    {
	cout << "application `" << desc.name << "' updated (serial = " << serial << ")" << endl;
    }

    virtual void 
    applicationSynced(int serial, const ApplicationDescriptorPtr& app, const Ice::Current&)
    {
	cout << "application `" << app->name << "' synced (serial = " << serial << ")" << endl;
    }

    virtual void
    nodeUp(const string& name, const Ice::Current& current)
    {
	cout << "node `" << name << "' is up" << endl;
    }

    virtual void
    nodeDown(const string& name, const Ice::Current& current)
    {
	cout << "node `" << name << "' is down" << endl;
    }
};

class NodeObserverI : public NodeObserver
{
public:

    virtual void 
    init(const NodeDynamicInfoSeq& nodes, const Ice::Current& c)
    {
	for(NodeDynamicInfoSeq::const_iterator p = nodes.begin(); p != nodes.end(); ++p)
	{
	    initNode(*p, c);
	}
    }

    virtual void 
    initNode(const NodeDynamicInfo& node, const Ice::Current&)
    {
	cout << "node `" << node.name << "' servers: ";
	for(ServerDynamicInfoSeq::const_iterator p = node.servers.begin(); p != node.servers.end(); ++p)
	{
	    cout << p->name << " ";
	}
	cout << "adapters: ";
	for(AdapterDynamicInfoSeq::const_iterator p = node.adapters.begin(); p != node.adapters.end(); ++p)
	{
	    cout << p->id << " ";
	}	
	cout << endl;
    }

    virtual void
    updateServer(const string& node, const ServerDynamicInfo& info, const Ice::Current&)
    {
	cout << "node `" << node << "' server `" << info.name << "' updated: pid = " << info.pid << " state = `";
	switch(info.state)
	{
	case Inactive:
	    cout << "Inactive";
	    break;
	case Activating:
	    cout << "Activating";
	    break;
	case Active:
	    cout << "Active";
	    break;
	case Deactivating:
	    cout << "Deactivating";
	    break;
	case Destroying:
	    cout << "Destroying";
	    break;
	case Destroyed:
	    cout << "Destroyed";
	    break;
	}
	cout << "'" << endl;
    }

    virtual void
    updateAdapter(const string& node, const AdapterDynamicInfo& info, const Ice::Current& current)
    {
	cout << "node `" << node << "' adapter `" << info.id << "' updated: ";
	cout << current.adapter->getCommunicator()->proxyToString(info.proxy);
	cout << endl;
    }
};

class Client : public Application
{
public:

    void usage();
    virtual int run(int, char*[]);
};

int
main(int argc, char* argv[])
{
    Client app;
    return app.main(argc, argv);
}

void
Client::usage()
{
    cerr << "Usage: " << appName() << " [options] [file...]\n";
    cerr <<	
	"Options:\n"
	"-h, --help           Show this message.\n"
	"-v, --version        Display the Ice version.\n"
	;
}

int
Client::run(int argc, char* argv[])
{
    IceUtil::Options opts;
    opts.addOpt("h", "help");
    opts.addOpt("v", "version");

    vector<string> args;
    try
    {
    	args = opts.parse(argc, argv);
    }
    catch(const IceUtil::Options::BadOpt& e)
    {
        cerr << e.reason << endl;
	usage();
	return EXIT_FAILURE;
    }

    if(opts.isSet("h") || opts.isSet("help"))
    {
	usage();
	return EXIT_SUCCESS;
    }
    if(opts.isSet("v") || opts.isSet("version"))
    {
	cout << ICE_STRING_VERSION << endl;
	return EXIT_SUCCESS;
    }

    SessionManagerPrx manager = SessionManagerPrx::checkedCast(communicator()->stringToProxy("IceGrid/SessionManager"));
    if(!manager)
    {
	cerr << appName() << ": no valid session manager interface" << endl;
	return EXIT_FAILURE;
    }


    AdminPrx admin = AdminPrx::checkedCast(communicator()->stringToProxy("IceGrid/Admin"));
    if(!admin)
    {
	cerr << appName() << ": no valid administrative interface" << endl;
	return EXIT_FAILURE;
    }

    Ice::SliceChecksumDict serverChecksums = admin->getSliceChecksums();
    Ice::SliceChecksumDict localChecksums = Ice::sliceChecksums();
    for(Ice::SliceChecksumDict::const_iterator q = localChecksums.begin(); q != localChecksums.end(); ++q)
    {
        Ice::SliceChecksumDict::const_iterator r = serverChecksums.find(q->first);
        if(r == serverChecksums.end())
        {
            cerr << appName() << ": server is using unknown Slice type `" << q->first << "'" << endl;
        }
        else if(q->second != r->second)
        {
            cerr << appName() << ": server is using a different Slice definition of `" << q->first << "'" << endl;
        }
    }

    SessionPrx session = manager->createLocalSession("IceGrid.Observer");

    Ice::ObjectAdapterPtr adapter = communicator()->createObjectAdapter("IceGrid.Observer");
    RegistryObserverPrx regObserver = RegistryObserverPrx::uncheckedCast(adapter->addWithUUID(new RegistryObserverI()));
    NodeObserverPrx nodeObserver = NodeObserverPrx::uncheckedCast(adapter->addWithUUID(new NodeObserverI()));
    adapter->activate();
    manager->ice_connection()->setAdapter(adapter);

    SessionKeepAliveThreadPtr keepAlive;
    keepAlive = new SessionKeepAliveThread(communicator()->getLogger(), IceUtil::Time::seconds(5), session);
    keepAlive->start();

    session->setObserversByIdentity(regObserver->ice_getIdentity(), nodeObserver->ice_getIdentity());

    communicator()->waitForShutdown();

    keepAlive->terminate();
    keepAlive->getThreadControl().join();
    keepAlive = 0;
    
    return EXIT_SUCCESS;
}