summaryrefslogtreecommitdiff
path: root/cpp/src/IcePack/Registry.cpp
blob: ee2a8b87df189f47ba1568a878f8bdf7b6bcc00c (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
// **********************************************************************
//
// Copyright (c) 2003-2004 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/UUID.h>
#include <Ice/Ice.h>
#include <Freeze/Freeze.h>

#include <IcePack/Registry.h>
#include <IcePack/AdapterI.h>
#include <IcePack/AdapterFactory.h>
#include <IcePack/ApplicationRegistryI.h>
#include <IcePack/ServerRegistryI.h>
#include <IcePack/AdapterRegistryI.h>
#include <IcePack/ObjectRegistryI.h>
#include <IcePack/NodeRegistryI.h>
#include <IcePack/LocatorI.h>
#include <IcePack/LocatorRegistryI.h>
#include <IcePack/AdminI.h>
#include <IcePack/QueryI.h>
#include <IcePack/TraceLevels.h>

#include <sys/types.h>
#include <sys/stat.h>

#ifdef _WIN32
#   include <direct.h>
#   define S_ISDIR(mode) ((mode) & _S_IFDIR)
#   define S_ISREG(mode) ((mode) & _S_IFREG)
#else
#   include <unistd.h>
#endif

using namespace std;
using namespace Ice;
using namespace IcePack;

string
intToString(int v)
{
    ostringstream os;
    os << v;
    return os.str();
}

IcePack::Registry::Registry(const CommunicatorPtr& communicator) :
    _communicator(communicator)
{
}

IcePack::Registry::~Registry()
{
}

bool
IcePack::Registry::start(bool nowarn)
{
    assert(_communicator);
    PropertiesPtr properties = _communicator->getProperties();

    //
    // Initialize the database environment.
    //
    _envName = properties->getProperty("IcePack.Registry.Data");
    if(_envName.empty())
    {
	Error out(_communicator->getLogger());
	out << "property `IcePack.Registry.Data' is not set";
	return false;
    }
    else
    {
	struct stat filestat;
	if(stat(_envName.c_str(), &filestat) != 0 || !S_ISDIR(filestat.st_mode))
	{
	    Error out(_communicator->getLogger());
	    SyscallException ex(__FILE__, __LINE__);
	    ex.error = getSystemErrno();
	    out << "property `IcePack.Registry.Data' is set to an invalid path:\n" << ex;
	    return false;
	}
    }

    //
    // Check that required properties are set and valid.
    //
    if(properties->getProperty("IcePack.Registry.Client.Endpoints").empty())
    {
	Error out(_communicator->getLogger());
	out << "property `IcePack.Registry.Client.Endpoints' is not set";
	return false;
    }

    if(properties->getProperty("IcePack.Registry.Server.Endpoints").empty())
    {
	Error out(_communicator->getLogger());
	out << "property `IcePack.Registry.Server.Endpoints' is not set";
	return false;
    }

    if(properties->getProperty("IcePack.Registry.Internal.Endpoints").empty())
    {
	Error out(_communicator->getLogger());
	out << "property `IcePack.Registry.Internal.Endpoints' is not set";
	return false;
    }

    if(!properties->getProperty("IcePack.Registry.Admin.Endpoints").empty())
    {
	if(!nowarn)
	{
	    Warning out(_communicator->getLogger());
	    out << "administrative endpoints `IcePack.Registry.Admin.Endpoints' enabled";
	}
    }

    properties->setProperty("Ice.PrintProcessId", "0");
    properties->setProperty("Ice.Warn.Leaks", "0");
    properties->setProperty("Ice.ServerIdleTime", "0");
    properties->setProperty("IcePack.Registry.Internal.AdapterId", "IcePack.Registry.Internal");

    //
    // Setup thread pool size for each thread pool.
    //
    int nThreadPool = 0;
    if(properties->getPropertyAsInt("IcePack.Registry.Client.ThreadPool.Size") == 0 &&
       properties->getPropertyAsInt("IcePack.Registry.Client.ThreadPool.SizeMax") == 0)
    {
	++nThreadPool;
    }
    if(properties->getPropertyAsInt("IcePack.Registry.Server.ThreadPool.Size") == 0 &&
       properties->getPropertyAsInt("IcePack.Registry.Server.ThreadPool.SizeMax") == 0)
    {
	++nThreadPool;
    }
    if(properties->getPropertyAsInt("IcePack.Registry.Admin.ThreadPool.Size") == 0 &&
       properties->getPropertyAsInt("IcePack.Registry.Admin.ThreadPool.SizeMax") == 0)	
    {
	++nThreadPool;
    }    

    int size = properties->getPropertyAsIntWithDefault("Ice.ThreadPool.Server.Size", 10);
    if(size < nThreadPool)
    {
	size = nThreadPool;
    }
    int sizeMax = properties->getPropertyAsIntWithDefault("Ice.ThreadPool.Server.SizeMax", size * 10);
    if(sizeMax < size)
    {
	sizeMax = size;
    }
    int sizeWarn = properties->getPropertyAsIntWithDefault("Ice.ThreadPool.Server.SizeWarn", sizeMax * 80 / 100);

    if(properties->getPropertyAsInt("IcePack.Registry.Client.ThreadPool.Size") == 0 &&
       properties->getPropertyAsInt("IcePack.Registry.Client.ThreadPool.SizeMax") == 0)
    {
	properties->setProperty("IcePack.Registry.Client.ThreadPool.Size", intToString(size / nThreadPool));
	properties->setProperty("IcePack.Registry.Client.ThreadPool.SizeMax", intToString(sizeMax / nThreadPool));
	properties->setProperty("IcePack.Registry.Client.ThreadPool.SizeWarn", intToString(sizeWarn / nThreadPool));
    }
    if(properties->getPropertyAsInt("IcePack.Registry.Server.ThreadPool.Size") == 0 &&
       properties->getPropertyAsInt("IcePack.Registry.Server.ThreadPool.SizeMax") == 0)
    {
	properties->setProperty("IcePack.Registry.Server.ThreadPool.Size", intToString(size / nThreadPool));
	properties->setProperty("IcePack.Registry.Server.ThreadPool.SizeMax", intToString(sizeMax / nThreadPool));
	properties->setProperty("IcePack.Registry.Server.ThreadPool.SizeWarn", intToString(sizeWarn / nThreadPool));
    }
    if(properties->getPropertyAsInt("IcePack.Registry.Admin.ThreadPool.Size") == 0 &&
       properties->getPropertyAsInt("IcePack.Registry.Admin.ThreadPool.SizeMax") == 0)	
    {
	properties->setProperty("IcePack.Registry.Admin.ThreadPool.Size", intToString(size / nThreadPool));
	properties->setProperty("IcePack.Registry.Admin.ThreadPool.SizeMax", intToString(sizeMax / nThreadPool));
	properties->setProperty("IcePack.Registry.Admin.ThreadPool.SizeWarn", intToString(sizeWarn / nThreadPool));
    }    

    int clientSize = properties->getPropertyAsInt("IcePack.Registry.Client.ThreadPool.Size") * 2;
    int serverSize = properties->getPropertyAsInt("IcePack.Registry.Server.ThreadPool.Size") * 2;
    properties->setProperty("IcePack.Registry.Internal.ThreadPool.Size", intToString(clientSize + serverSize));

    int clientSizeMax = properties->getPropertyAsInt("IcePack.Registry.Client.ThreadPool.SizeMax") * 2;
    if(clientSizeMax < clientSize)
    {
	clientSizeMax = clientSize;
    }
    int serverSizeMax = properties->getPropertyAsInt("IcePack.Registry.Server.ThreadPool.SizeMax") * 2;
    if(serverSizeMax < serverSize)
    {
	serverSizeMax = serverSize;
    }
    properties->setProperty("IcePack.Registry.Internal.ThreadPool.SizeMax", 
			    intToString(clientSizeMax + serverSizeMax));

    int clientSizeWarn = properties->getPropertyAsIntWithDefault("IcePack.Registry.Client.ThreadPool.SizeWarn", 
								 clientSizeMax * 80 / 100) * 2;
    int serverSizeWarn = properties->getPropertyAsIntWithDefault("IcePack.Registry.Server.ThreadPool.SizeWarn", 
								 serverSizeMax * 80 / 100) * 2;
    properties->setProperty("IcePack.Registry.Internal.ThreadPool.SizeWarn", 
			    intToString(clientSizeWarn + serverSizeWarn));

    TraceLevelsPtr traceLevels = new TraceLevels(properties, _communicator->getLogger());

    _communicator->setDefaultLocator(0);

    //
    // Create the object adapters.
    //
    ObjectAdapterPtr serverAdapter = _communicator->createObjectAdapter("IcePack.Registry.Server");
    ObjectAdapterPtr clientAdapter = _communicator->createObjectAdapter("IcePack.Registry.Client");
    ObjectAdapterPtr adminAdapter = _communicator->createObjectAdapter("IcePack.Registry.Admin");
    ObjectAdapterPtr registryAdapter = _communicator->createObjectAdapter("IcePack.Registry.Internal");

    //
    // Create the internal registries (node, server, adapter, object).
    //
    AdapterFactoryPtr adapterFactory = new AdapterFactory(registryAdapter, traceLevels, _envName);
    ObjectRegistryPtr objectRegistry = new ObjectRegistryI(_communicator, _envName, traceLevels);
    AdapterRegistryPtr adapterRegistry = new AdapterRegistryI(_communicator, _envName, traceLevels);
    ServerRegistryPtr serverRegistry = new ServerRegistryI(_communicator, _envName, traceLevels);
    ApplicationRegistryPtr appReg = new ApplicationRegistryI(_communicator, serverRegistry, _envName, traceLevels);
    NodeRegistryPtr nodeReg = new NodeRegistryI(_communicator, _envName, adapterRegistry, adapterFactory, traceLevels);

    registryAdapter->add(objectRegistry, stringToIdentity("IcePack/ObjectRegistry"));
    registryAdapter->add(adapterRegistry, stringToIdentity("IcePack/AdapterRegistry"));
    registryAdapter->add(serverRegistry, stringToIdentity("IcePack/ServerRegistry"));    
    registryAdapter->add(appReg, stringToIdentity("IcePack/ApplicationRegistry"));
    registryAdapter->add(nodeReg, stringToIdentity("IcePack/NodeRegistry"));

    //
    // Create the locator registry and locator interfaces.
    //
    bool dynamicReg = properties->getPropertyAsInt("IcePack.Registry.DynamicRegistration") > 0;
    ObjectPtr locatorRegistry = new LocatorRegistryI(adapterRegistry, serverRegistry, adapterFactory, dynamicReg);
    ObjectPrx obj = serverAdapter->add(locatorRegistry, stringToIdentity("IcePack/" + IceUtil::generateUUID()));
    LocatorRegistryPrx locatorRegistryPrx = LocatorRegistryPrx::uncheckedCast(obj->ice_collocationOptimization(false));
    ObjectPtr locator = new LocatorI(adapterRegistry, objectRegistry, locatorRegistryPrx); 
    clientAdapter->add(locator, stringToIdentity("IcePack/Locator"));

    //
    // Create the query interface and register it with the object registry.
    //
    QueryPtr query = new QueryI(_communicator, objectRegistry);
    clientAdapter->add(query, stringToIdentity("IcePack/Query"));
    ObjectPrx queryPrx = clientAdapter->createDirectProxy(stringToIdentity("IcePack/Query"));
    try
    {
	objectRegistry->remove(queryPrx->ice_getIdentity());
    }
    catch(const ObjectNotExistException&)
    {
    }	
    IcePack::ObjectDescriptor desc;
    desc.proxy = queryPrx;
    desc.type = "::IcePack::Query";	
    objectRegistry->add(desc);

    //
    // Create the admin interface and register it with the object registry.
    //
    ObjectPtr admin = new AdminI(_communicator, nodeReg, appReg, serverRegistry, adapterRegistry, objectRegistry);
    adminAdapter->add(admin, stringToIdentity("IcePack/Admin"));    
    ObjectPrx adminPrx = adminAdapter->createDirectProxy(stringToIdentity("IcePack/Admin"));
    try
    {
	objectRegistry->remove(adminPrx->ice_getIdentity());
    }
    catch(const ObjectNotExistException&)
    {
    }
    desc.proxy = adminPrx;
    desc.type = "::IcePack::Admin";	
    objectRegistry->add(desc);

    //
    // Register the IcePack.Registry.Internal adapter with the adapter registry.
    //
    try
    {
	adapterRegistry->remove("IcePack.Registry.Internal", 0);
    }
    catch(const AdapterNotExistException&)
    {
    }
    obj = registryAdapter->addWithUUID(new StandaloneAdapterI());
    registryAdapter->activate();
    AdapterPrx adapter = AdapterPrx::uncheckedCast(registryAdapter->createDirectProxy(obj->ice_getIdentity())); 
    adapterRegistry->add("IcePack.Registry.Internal", adapter);
    adapter->setDirectProxy(adapter);
    
    //
    // Setup a internal locator to be used by the IcePack registry itself. This locator is 
    // registered with the registry object adapter which is using an independant threadpool.
    //
    locator = new LocatorI(adapterRegistry, objectRegistry, locatorRegistryPrx);
    registryAdapter->add(locator, stringToIdentity("IcePack/Locator"));
    obj = registryAdapter->createDirectProxy(stringToIdentity("IcePack/Locator"));
    _communicator->setDefaultLocator(LocatorPrx::uncheckedCast(obj->ice_collocationOptimization(false)));

    //
    // We are ready to go!
    //
    serverAdapter->activate();
    clientAdapter->activate();
    if(adminAdapter)
    {
	adminAdapter->activate();
    }
    
    return true;
}