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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
// **********************************************************************
//
// Copyright (c) 2003-2018 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.
//
// **********************************************************************
#import <Foundation/Foundation.h>
#include <Ice/Ice.h>
#include <Controller.h>
#include <TestHelper.h>
#include <dlfcn.h>
@protocol ControllerView
-(void) print:(NSString*)msg;
-(void) println:(NSString*)msg;
@end
using namespace std;
using namespace Test::Common;
namespace
{
typedef Test::TestHelper* (*CREATE_HELPER_ENTRY_POINT)();
class ControllerHelperI : public Test::ControllerHelper,
private IceUtil::Monitor<IceUtil::Mutex>,
public IceUtil::Thread
{
public:
ControllerHelperI(id<ControllerView>, const string&, const StringSeq&);
virtual ~ControllerHelperI();
virtual bool redirect() const;
virtual string getOutput() const;
virtual string loggerPrefix() const;
virtual void print(const std::string&);
virtual void serverReady();
virtual void shutdown();
virtual void join();
void waitReady(int) const;
int waitSuccess(int) const;
virtual void run();
void completed(int);
private:
id<ControllerView> _controller;
std::string _dll;
StringSeq _args;
CFBundleRef _handle;
bool _ready;
bool _completed;
int _status;
std::ostringstream _out;
IceInternal::UniquePtr<Test::TestHelper> _helper;
};
class ProcessI : public Process
{
public:
ProcessI(id<ControllerView>, ControllerHelperI*);
virtual ~ProcessI();
void waitReady(int, const Ice::Current&);
int waitSuccess(int, const Ice::Current&);
string terminate(const Ice::Current&);
private:
id<ControllerView> _controller;
IceUtil::Handle<ControllerHelperI> _helper;
};
class ProcessControllerI : public ProcessController
{
public:
ProcessControllerI(id<ControllerView>, NSString*, NSString*);
#ifdef ICE_CPP11_MAPPING
virtual shared_ptr<ProcessPrx> start(string, string, StringSeq, const Ice::Current&);
virtual string getHost(string, bool, const Ice::Current&);
#else
virtual ProcessPrx start(const string&, const string&, const StringSeq&, const Ice::Current&);
virtual string getHost(const string&, bool, const Ice::Current&);
#endif
private:
id<ControllerView> _controller;
string _ipv4;
string _ipv6;
};
class ControllerI
{
public:
ControllerI(id<ControllerView>, NSString*, NSString*);
virtual ~ControllerI();
private:
Ice::CommunicatorPtr _communicator;
};
}
ControllerHelperI::ControllerHelperI(id<ControllerView> controller, const string& dll, const StringSeq& args) :
_controller(controller),
_dll(dll),
_args(args),
_ready(false),
_completed(false),
_status(0)
{
}
ControllerHelperI::~ControllerHelperI()
{
if(_helper)
{
_helper.reset();
}
if(_handle)
{
CFBundleUnloadExecutable(_handle);
CFRelease(_handle);
}
}
void
ControllerHelperI::serverReady()
{
Lock sync(*this);
_ready = true;
notifyAll();
}
void
ControllerHelperI::shutdown()
{
Lock sync(*this);
if(_completed)
{
return;
}
if(_helper)
{
Ice::CommunicatorPtr communicator = _helper->communicator();
if(communicator)
{
communicator->shutdown();
}
}
}
bool
ControllerHelperI::redirect() const
{
return _dll.find("client") != string::npos || _dll.find("collocated") != string::npos;
}
string
ControllerHelperI::loggerPrefix() const
{
return _dll;
}
void
ControllerHelperI::print(const std::string& msg)
{
_out << msg;
}
void
ControllerHelperI::run()
{
NSString* bundlePath = [[NSBundle mainBundle] privateFrameworksPath];
bundlePath = [bundlePath stringByAppendingPathComponent:[NSString stringWithUTF8String:_dll.c_str()]];
NSURL* bundleURL = [NSURL fileURLWithPath:bundlePath];
_handle = CFBundleCreate(NULL, (CFURLRef)bundleURL);
if(!_handle)
{
print([[NSString stringWithFormat:@"Could not find bundle %@", bundlePath] UTF8String]);
completed(EXIT_FAILURE);
return;
}
CFErrorRef error = nil;
Boolean loaded = CFBundleLoadExecutableAndReturnError(_handle, &error);
if(error != nil || !loaded)
{
print([[(__bridge NSError *)error description] UTF8String]);
completed(EXIT_FAILURE);
return;
}
//
// The first CFBundleGetFunction... does not always succeed, so we make up to 5 attempts
//
void* sym = 0;
int attempts = 0;
while((sym = CFBundleGetFunctionPointerForName(_handle, CFSTR("createHelper"))) == 0 && attempts < 5)
{
attempts++;
[NSThread sleepForTimeInterval:0.2];
}
if(sym == 0)
{
NSString* err = [NSString stringWithFormat:@"Could not get function pointer createHelper from bundle %@",
bundlePath];
print([err UTF8String]);
completed(EXIT_FAILURE);
return;
}
CREATE_HELPER_ENTRY_POINT createHelper = (CREATE_HELPER_ENTRY_POINT)sym;
char** argv = new char*[_args.size() + 1];
for(unsigned int i = 0; i < _args.size(); ++i)
{
argv[i] = const_cast<char*>(_args[i].c_str());
}
argv[_args.size()] = 0;
try
{
Test::StreamHelper streamHelper(this, redirect());
_helper.reset(createHelper());
_helper->setControllerHelper(this);
_helper->run(static_cast<int>(_args.size()), argv);
completed(0);
}
catch(const std::exception& ex)
{
print("unexpected exception while running `" + _args[0] + "':\n" + ex.what());
completed(1);
}
catch(...)
{
print("unexpected unknown exception while running `" + _args[0] + "'");
completed(1);
}
delete[] argv;
}
void
ControllerHelperI::join()
{
getThreadControl().join();
}
void
ControllerHelperI::completed(int status)
{
Lock sync(*this);
_completed = true;
_status = status;
notifyAll();
}
void
ControllerHelperI::waitReady(int timeout) const
{
Lock sync(*this);
while(!_ready && !_completed)
{
if(!timedWait(IceUtil::Time::seconds(timeout)))
{
throw ProcessFailedException("timed out waiting for the process to be ready");
}
}
if(_completed && _status == EXIT_FAILURE)
{
throw ProcessFailedException(_out.str());
}
}
int
ControllerHelperI::waitSuccess(int timeout) const
{
Lock sync(*this);
while(!_completed)
{
if(!timedWait(IceUtil::Time::seconds(timeout)))
{
throw ProcessFailedException("timed out waiting for the process to succeed");
}
}
return _status;
}
string
ControllerHelperI::getOutput() const
{
assert(_completed);
return _out.str();
}
ProcessI::ProcessI(id<ControllerView> controller, ControllerHelperI* helper) :
_controller(controller),
_helper(helper)
{
}
ProcessI::~ProcessI()
{
}
void
ProcessI::waitReady(int timeout, const Ice::Current&)
{
_helper->waitReady(timeout);
}
int
ProcessI::waitSuccess(int timeout, const Ice::Current&)
{
return _helper->waitSuccess(timeout);
}
string
ProcessI::terminate(const Ice::Current& current)
{
_helper->shutdown();
current.adapter->remove(current.id);
_helper->join();
return _helper->getOutput();
}
ProcessControllerI::ProcessControllerI(id<ControllerView> controller, NSString* ipv4, NSString* ipv6) :
_controller(controller), _ipv4([ipv4 UTF8String]), _ipv6([ipv6 UTF8String])
{
}
#ifdef ICE_CPP11_MAPPING
shared_ptr<ProcessPrx>
ProcessControllerI::start(string testSuite, string exe, StringSeq args, const Ice::Current& c)
#else
ProcessPrx
ProcessControllerI::start(const string& testSuite, const string& exe, const StringSeq& args, const Ice::Current& c)
#endif
{
StringSeq newArgs = args;
std::string prefix = std::string("test/") + testSuite;
replace(prefix.begin(), prefix.end(), '/', '_');
newArgs.insert(newArgs.begin(), testSuite + ' ' + exe);
[_controller println:[NSString stringWithFormat:@"starting %s %s... ", testSuite.c_str(), exe.c_str()]];
IceUtil::Handle<ControllerHelperI> helper(new ControllerHelperI(_controller, prefix + '/' + exe + ".bundle", newArgs));
//
// Use a 768KB thread stack size for the objects test. This is necessary when running the
// test on arm64 devices with a debug Ice libraries which require lots of stack space.
//
helper->start(768 * 1024);
return ICE_UNCHECKED_CAST(ProcessPrx, c.adapter->addWithUUID(ICE_MAKE_SHARED(ProcessI, _controller, helper.get())));
}
string
#ifdef ICE_CPP11_MAPPING
ProcessControllerI::getHost(string protocol, bool ipv6, const Ice::Current& c)
#else
ProcessControllerI::getHost(const string& protocol, bool ipv6, const Ice::Current& c)
#endif
{
return ipv6 ? _ipv6 : _ipv4;
}
ControllerI::ControllerI(id<ControllerView> controller, NSString* ipv4, NSString* ipv6)
{
Ice::registerIceDiscovery();
Ice::InitializationData initData = Ice::InitializationData();
initData.properties = Ice::createProperties();
initData.properties->setProperty("Ice.ThreadPool.Server.SizeMax", "10");
initData.properties->setProperty("IceDiscovery.DomainId", "TestController");
initData.properties->setProperty("ControllerAdapter.Endpoints", "tcp");
//initData.properties->setProperty("Ice.Trace.Network", "2");
//initData.properties->setProperty("Ice.Trace.Protocol", "2");
initData.properties->setProperty("ControllerAdapter.AdapterId", Ice::generateUUID());
_communicator = Ice::initialize(initData);
Ice::ObjectAdapterPtr adapter = _communicator->createObjectAdapter("ControllerAdapter");
Ice::Identity ident;
#if TARGET_IPHONE_SIMULATOR != 0
ident.category = "iPhoneSimulator";
#else
ident.category = "iPhoneOS";
#endif
ident.name = [[[NSBundle mainBundle] bundleIdentifier] UTF8String];
adapter->add(ICE_MAKE_SHARED(ProcessControllerI, controller, ipv4, ipv6), ident);
adapter->activate();
}
ControllerI::~ControllerI()
{
_communicator->destroy();
_communicator = 0;
}
static ControllerI* controllerI = 0;
extern "C"
{
void
startController(id<ControllerView> controller, NSString* ipv4, NSString* ipv6)
{
controllerI = new ControllerI(controller, ipv4, ipv6);
}
void
stopController()
{
if(controllerI)
{
delete controllerI;
controllerI = 0;
}
}
}
|