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
|
// **********************************************************************
//
// Copyright (c) 2003-2007 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/DisableWarnings.h>
#include <Ice/GC.h>
#include <Ice/CommunicatorI.h>
#include <Ice/PropertiesI.h>
#include <Ice/Initialize.h>
#include <Ice/LocalException.h>
#include <Ice/StreamI.h>
#include <Ice/LoggerI.h>
using namespace std;
using namespace Ice;
using namespace IceInternal;
namespace IceInternal
{
extern IceUtil::Handle<IceInternal::GC> theCollector;
}
void
Ice::collectGarbage()
{
if(theCollector)
{
theCollector->collectGarbage();
}
}
StringSeq
Ice::argsToStringSeq(int argc, char* argv[])
{
StringSeq result;
for(int i = 0; i < argc; i++)
{
result.push_back(argv[i]);
}
return result;
}
void
Ice::stringSeqToArgs(const StringSeq& args, int& argc, char* argv[])
{
//
// Shift all elements in argv which are present in args to the
// beginning of argv. We record the original value of argc so
// that we can know later if we've shifted the array.
//
const int argcOrig = argc;
int i = 0;
while(i < argc)
{
if(find(args.begin(), args.end(), argv[i]) == args.end())
{
for(int j = i; j < argc - 1; j++)
{
argv[j] = argv[j + 1];
}
--argc;
}
else
{
++i;
}
}
//
// Make sure that argv[argc] == 0, the ISO C++ standard requires this.
// We can only do this if we've shifted the array, otherwise argv[argc]
// may point to an invalid address.
//
if(argv && argcOrig != argc)
{
argv[argc] = 0;
}
}
PropertiesPtr
Ice::createProperties(const StringConverterPtr& converter)
{
return new PropertiesI(converter);
}
PropertiesPtr
Ice::createProperties(StringSeq& args, const PropertiesPtr& defaults, const StringConverterPtr& converter)
{
return new PropertiesI(args, defaults, converter);
}
PropertiesPtr
Ice::createProperties(int& argc, char* argv[], const PropertiesPtr& defaults, const StringConverterPtr& converter)
{
StringSeq args = argsToStringSeq(argc, argv);
PropertiesPtr properties = createProperties(args, defaults, converter);
stringSeqToArgs(args, argc, argv);
return properties;
}
inline void checkIceVersion(Int version)
{
#ifndef ICE_IGNORE_VERSION
# if ICE_INT_VERSION % 100 > 50
//
// Beta version: exact match required
//
if(ICE_INT_VERSION != version)
{
throw VersionMismatchException(__FILE__, __LINE__);
}
# else
//
// Major and minor version numbers must match.
//
if(ICE_INT_VERSION / 100 != version / 100)
{
throw VersionMismatchException(__FILE__, __LINE__);
}
//
// Reject beta caller
//
if(version % 100 > 50)
{
throw VersionMismatchException(__FILE__, __LINE__);
}
//
// The caller's patch level cannot be greater than library's patch level. (Patch level changes are
// backward-compatible, but not forward-compatible.)
//
if(version % 100 > ICE_INT_VERSION % 100)
{
throw VersionMismatchException(__FILE__, __LINE__);
}
# endif
#endif
}
CommunicatorPtr
Ice::initialize(int& argc, char* argv[], const InitializationData& initializationData, Int version)
{
checkIceVersion(version);
InitializationData initData = initializationData;
initData.properties = createProperties(argc, argv, initData.properties, initData.stringConverter);
CommunicatorI* communicatorI = new CommunicatorI(initData);
CommunicatorPtr result = communicatorI; // For exception safety.
communicatorI->finishSetup(argc, argv);
return result;
}
CommunicatorPtr
Ice::initialize(StringSeq& args, const InitializationData& initializationData, Int version)
{
int origArgc;
char** argv;
CommunicatorPtr communicator;
try
{
//
// Make a dummy argc/argv.
// (We can't use argsToStringSeq() because that requires an already initialized argv.)
//
int argc = args.size();
origArgc = argc;
argv = new char*[args.size() + 1];
int i;
for(i = 0; i != argc; ++i)
{
argv[i] = new char[args[i].size() + 1];
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
strcpy_s(argv[i], args[i].size() + 1, args[i].c_str());
#else
strcpy(argv[i], args[i].c_str());
#endif
}
argv[argc] = 0;
communicator = initialize(argc, argv, initializationData, version);
args = argsToStringSeq(argc, argv);
for(i = 0; i < origArgc; ++i)
{
delete[] argv[i];
}
delete[] argv;
}
catch(...)
{
for(int i = 0; i < origArgc; ++i)
{
delete[] argv[i];
}
delete[] argv;
throw;
}
return communicator;
}
CommunicatorPtr
Ice::initialize(const InitializationData& initData, Int version)
{
//
// We can't simply call the other initialize() because this one does NOT read
// the config file, while the other one always does.
//
checkIceVersion(version);
CommunicatorI* communicatorI = new CommunicatorI(initData);
CommunicatorPtr result = communicatorI; // For exception safety.
int argc = 0;
char* argv[] = { 0 };
communicatorI->finishSetup(argc, argv);
return result;
}
CommunicatorPtr
Ice::initializeWithProperties(int& argc, char* argv[], const PropertiesPtr& properties, Int version)
{
InitializationData initData;
initData.properties = properties;
return initialize(argc, argv, initData, version);
}
CommunicatorPtr
Ice::initializeWithLogger(int& argc, char* argv[], const LoggerPtr& logger, Int version)
{
InitializationData initData;
initData.logger = logger;
return initialize(argc, argv, initData, version);
}
CommunicatorPtr
Ice::initializeWithPropertiesAndLogger(int& argc, char* argv[], const PropertiesPtr& properties,
const LoggerPtr& logger, Int version)
{
InitializationData initData;
initData.properties = properties;
initData.logger = logger;
return initialize(argc, argv, initData, version);
}
InputStreamPtr
Ice::createInputStream(const CommunicatorPtr& communicator, const vector<Byte>& bytes)
{
return new InputStreamI(communicator, bytes);
}
OutputStreamPtr
Ice::createOutputStream(const CommunicatorPtr& communicator)
{
return new OutputStreamI(communicator);
}
static IceUtil::StaticMutex processLoggerMutex = ICE_STATIC_MUTEX_INITIALIZER;
static Ice::LoggerPtr processLogger;
LoggerPtr
Ice::getProcessLogger()
{
IceUtil::StaticMutex::Lock lock(processLoggerMutex);
if(processLogger == 0)
{
//
// TODO: Would be nice to be able to use process name as prefix by default.
//
processLogger = new Ice::LoggerI("");
}
return processLogger;
}
void
Ice::setProcessLogger(const LoggerPtr& logger)
{
IceUtil::StaticMutex::Lock lock(processLoggerMutex);
processLogger = logger;
}
InstancePtr
IceInternal::getInstance(const CommunicatorPtr& communicator)
{
CommunicatorI* p = dynamic_cast<CommunicatorI*>(communicator.get());
assert(p);
return p->_instance;
}
|