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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
|
// **********************************************************************
//
// Copyright (c) 2003-2013 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 <Communicator.h>
#include <ImplicitContext.h>
#include <Logger.h>
#include <ObjectFactory.h>
#include <Properties.h>
#include <Proxy.h>
#include <Types.h>
#include <Util.h>
#include <Ice/Communicator.h>
#include <Ice/Initialize.h>
#include <Ice/Locator.h>
#include <Ice/ObjectFactory.h>
#include <Ice/Properties.h>
#include <Ice/Router.h>
using namespace std;
using namespace IceRuby;
static VALUE _communicatorClass;
typedef map<Ice::CommunicatorPtr, VALUE> CommunicatorMap;
static CommunicatorMap _communicatorMap;
extern "C"
void
IceRuby_Communicator_mark(Ice::CommunicatorPtr* p)
{
assert(p);
try
{
ObjectFactoryPtr pof = ObjectFactoryPtr::dynamicCast((*p)->findObjectFactory(""));
assert(pof);
pof->mark();
}
catch(const Ice::CommunicatorDestroyedException&)
{
// Ignore. This is expected.
}
}
extern "C"
void
IceRuby_Communicator_free(Ice::CommunicatorPtr* p)
{
assert(p);
delete p;
}
extern "C"
VALUE
IceRuby_initialize(int argc, VALUE* argv, VALUE self)
{
ICE_RUBY_TRY
{
volatile VALUE initDataCls = callRuby(rb_path2class, "Ice::InitializationData");
volatile VALUE args = Qnil, initData = Qnil;
if(argc == 1)
{
if(isArray(argv[0]))
{
args = argv[0];
}
else if(callRuby(rb_obj_is_instance_of, argv[0], initDataCls) == Qtrue)
{
initData = argv[0];
}
else
{
throw RubyException(rb_eTypeError, "invalid argument to Ice::initialize");
}
}
else if(argc == 2)
{
if(!isArray(argv[0]) || callRuby(rb_obj_is_instance_of, argv[1], initDataCls) == Qfalse)
{
throw RubyException(rb_eTypeError, "invalid argument to Ice::initialize");
}
args = argv[0];
initData = argv[1];
}
else if(argc > 0)
{
throw RubyException(rb_eArgError, "invalid number of arguments to Ice::initialize");
}
Ice::StringSeq seq;
if(!NIL_P(args) && !arrayToStringSeq(args, seq))
{
throw RubyException(rb_eTypeError, "invalid array argument to Ice::initialize");
}
//
// Use the with-args or the without-args version of initialize()?
//
bool hasArgs = !NIL_P(args);
Ice::InitializationData data;
if(!NIL_P(initData))
{
volatile VALUE properties = callRuby(rb_iv_get, initData, "@properties");
volatile VALUE logger = callRuby(rb_iv_get, initData, "@logger");
if(!NIL_P(properties))
{
data.properties = getProperties(properties);
}
if(!NIL_P(logger))
{
throw RubyException(rb_eArgError, "custom logger is not supported");
}
}
//
// Insert the program name (stored in the Ruby global variable $0) as the first
// element of the sequence.
//
volatile VALUE progName = callRuby(rb_gv_get, "$0");
seq.insert(seq.begin(), getString(progName));
data.compactIdResolver = new IdResolver;
if(hasArgs)
{
data.properties = Ice::createProperties(seq, data.properties);
}
else if(!data.properties)
{
data.properties = Ice::createProperties();
}
//
// Disable collocation optimization, otherwise an invocation on a
// collocated servant results in a CollocationOptimizationException
// (because Ruby uses the blobject API).
//
// TODO: Enable if a server mapping is added.
//
//data.properties->setProperty("Ice.Default.CollocationOptimization", "0");
//
// Remaining command line options are passed to the communicator
// as an argument vector in case they contain plugin properties.
//
int ac = static_cast<int>(seq.size());
char** av = new char*[ac + 1];
int i = 0;
for(Ice::StringSeq::const_iterator s = seq.begin(); s != seq.end(); ++s, ++i)
{
av[i] = strdup(s->c_str());
}
av[ac] = 0;
Ice::CommunicatorPtr communicator;
try
{
if(hasArgs)
{
communicator = Ice::initialize(ac, av, data);
}
else
{
communicator = Ice::initialize(data);
}
}
catch(...)
{
for(i = 0; i < ac + 1; ++i)
{
free(av[i]);
}
delete[] av;
throw;
}
//
// Replace the contents of the given argument list with the filtered arguments.
//
if(!NIL_P(args))
{
callRuby(rb_ary_clear, args);
//
// We start at index 1 in order to skip the element that we inserted earlier.
//
for(i = 1; i < ac; ++i)
{
volatile VALUE str = createString(av[i]);
callRuby(rb_ary_push, args, str);
}
}
for(i = 0; i < ac + 1; ++i)
{
free(av[i]);
}
delete[] av;
ObjectFactoryPtr factory = new ObjectFactory;
communicator->addObjectFactory(factory, "");
VALUE result = Data_Wrap_Struct(_communicatorClass, IceRuby_Communicator_mark,
IceRuby_Communicator_free, new Ice::CommunicatorPtr(communicator));
CommunicatorMap::iterator p = _communicatorMap.find(communicator);
if(p != _communicatorMap.end())
{
_communicatorMap.erase(p);
}
_communicatorMap.insert(CommunicatorMap::value_type(communicator, reinterpret_cast<const VALUE&>(result)));
return result;
}
ICE_RUBY_CATCH
return Qnil;
}
extern "C"
VALUE
IceRuby_Communicator_destroy(VALUE self)
{
ICE_RUBY_TRY
{
Ice::CommunicatorPtr p = getCommunicator(self);
p->destroy();
}
ICE_RUBY_CATCH
return Qnil;
}
extern "C"
VALUE
IceRuby_Communicator_shutdown(VALUE self)
{
ICE_RUBY_TRY
{
Ice::CommunicatorPtr p = getCommunicator(self);
p->shutdown();
}
ICE_RUBY_CATCH
return Qnil;
}
extern "C"
VALUE
IceRuby_Communicator_isShutdown(VALUE self)
{
ICE_RUBY_TRY
{
Ice::CommunicatorPtr p = getCommunicator(self);
return p->isShutdown() ? Qtrue : Qfalse;
}
ICE_RUBY_CATCH
return Qnil;
}
extern "C"
VALUE
IceRuby_Communicator_stringToProxy(VALUE self, VALUE str)
{
ICE_RUBY_TRY
{
Ice::CommunicatorPtr p = getCommunicator(self);
string s = getString(str);
Ice::ObjectPrx proxy = p->stringToProxy(s);
if(proxy)
{
return createProxy(proxy);
}
}
ICE_RUBY_CATCH
return Qnil;
}
extern "C"
VALUE
IceRuby_Communicator_proxyToString(VALUE self, VALUE proxy)
{
ICE_RUBY_TRY
{
Ice::CommunicatorPtr p = getCommunicator(self);
Ice::ObjectPrx prx;
if(!NIL_P(proxy))
{
if(!checkProxy(proxy))
{
throw RubyException(rb_eTypeError, "argument must be a proxy");
}
prx = getProxy(proxy);
}
string str = p->proxyToString(prx);
return createString(str);
}
ICE_RUBY_CATCH
return Qnil;
}
extern "C"
VALUE
IceRuby_Communicator_propertyToProxy(VALUE self, VALUE str)
{
ICE_RUBY_TRY
{
Ice::CommunicatorPtr p = getCommunicator(self);
string s = getString(str);
Ice::ObjectPrx proxy = p->propertyToProxy(s);
if(proxy)
{
return createProxy(proxy);
}
}
ICE_RUBY_CATCH
return Qnil;
}
extern "C"
VALUE
IceRuby_Communicator_proxyToProperty(VALUE self, VALUE obj, VALUE str)
{
ICE_RUBY_TRY
{
if(!checkProxy(obj))
{
throw RubyException(rb_eTypeError, "argument must be a proxy");
}
Ice::CommunicatorPtr p = getCommunicator(self);
Ice::ObjectPrx o = getProxy(obj);
string s = getString(str);
Ice::PropertyDict dict = p->proxyToProperty(o, s);
volatile VALUE result = callRuby(rb_hash_new);
for(Ice::PropertyDict::const_iterator q = dict.begin(); q != dict.end(); ++q)
{
volatile VALUE key = createString(q->first);
volatile VALUE value = createString(q->second);
callRuby(rb_hash_aset, result, key, value);
}
return result;
}
ICE_RUBY_CATCH
return Qnil;
}
extern "C"
VALUE
IceRuby_Communicator_stringToIdentity(VALUE self, VALUE str)
{
ICE_RUBY_TRY
{
Ice::CommunicatorPtr p = getCommunicator(self);
string s = getString(str);
Ice::Identity ident = p->stringToIdentity(s);
return createIdentity(ident);
}
ICE_RUBY_CATCH
return Qnil;
}
extern "C"
VALUE
IceRuby_Communicator_identityToString(VALUE self, VALUE id)
{
ICE_RUBY_TRY
{
Ice::CommunicatorPtr p = getCommunicator(self);
Ice::Identity ident = getIdentity(id);
string str = p->identityToString(ident);
return createString(str);
}
ICE_RUBY_CATCH
return Qnil;
}
extern "C"
VALUE
IceRuby_Communicator_addObjectFactory(VALUE self, VALUE factory, VALUE id)
{
ICE_RUBY_TRY
{
Ice::CommunicatorPtr p = getCommunicator(self);
ObjectFactoryPtr pof = ObjectFactoryPtr::dynamicCast(p->findObjectFactory(""));
assert(pof);
string idstr = getString(id);
pof->add(factory, idstr);
}
ICE_RUBY_CATCH
return Qnil;
}
extern "C"
VALUE
IceRuby_Communicator_findObjectFactory(VALUE self, VALUE id)
{
ICE_RUBY_TRY
{
Ice::CommunicatorPtr p = getCommunicator(self);
ObjectFactoryPtr pof = ObjectFactoryPtr::dynamicCast(p->findObjectFactory(""));
assert(pof);
string idstr = getString(id);
return pof->find(idstr);
}
ICE_RUBY_CATCH
return Qnil;
}
extern "C"
VALUE
IceRuby_Communicator_getImplicitContext(VALUE self)
{
ICE_RUBY_TRY
{
Ice::CommunicatorPtr p = getCommunicator(self);
Ice::ImplicitContextPtr implicitContext = p->getImplicitContext();
return createImplicitContext(implicitContext);
}
ICE_RUBY_CATCH
return Qnil;
}
extern "C"
VALUE
IceRuby_Communicator_getProperties(VALUE self)
{
ICE_RUBY_TRY
{
Ice::CommunicatorPtr p = getCommunicator(self);
Ice::PropertiesPtr props = p->getProperties();
return createProperties(props);
}
ICE_RUBY_CATCH
return Qnil;
}
extern "C"
VALUE
IceRuby_Communicator_getLogger(VALUE self)
{
ICE_RUBY_TRY
{
Ice::CommunicatorPtr p = getCommunicator(self);
Ice::LoggerPtr logger = p->getLogger();
return createLogger(logger);
}
ICE_RUBY_CATCH
return Qnil;
}
extern "C"
VALUE
IceRuby_Communicator_getDefaultRouter(VALUE self)
{
ICE_RUBY_TRY
{
Ice::CommunicatorPtr p = getCommunicator(self);
Ice::RouterPrx router = p->getDefaultRouter();
if(router)
{
volatile VALUE cls = callRuby(rb_path2class, "Ice::RouterPrx");
assert(!NIL_P(cls));
return createProxy(router, cls);
}
}
ICE_RUBY_CATCH
return Qnil;
}
extern "C"
VALUE
IceRuby_Communicator_setDefaultRouter(VALUE self, VALUE router)
{
ICE_RUBY_TRY
{
Ice::CommunicatorPtr p = getCommunicator(self);
Ice::RouterPrx proxy;
if(!NIL_P(router))
{
if(!checkProxy(router))
{
throw RubyException(rb_eTypeError, "argument must be a proxy");
}
proxy = Ice::RouterPrx::uncheckedCast(getProxy(router));
}
p->setDefaultRouter(proxy);
}
ICE_RUBY_CATCH
return Qnil;
}
extern "C"
VALUE
IceRuby_Communicator_getDefaultLocator(VALUE self)
{
ICE_RUBY_TRY
{
Ice::CommunicatorPtr p = getCommunicator(self);
Ice::LocatorPrx locator = p->getDefaultLocator();
if(locator)
{
volatile VALUE cls = callRuby(rb_path2class, "Ice::LocatorPrx");
assert(!NIL_P(cls));
return createProxy(locator, cls);
}
}
ICE_RUBY_CATCH
return Qnil;
}
extern "C"
VALUE
IceRuby_Communicator_setDefaultLocator(VALUE self, VALUE locator)
{
ICE_RUBY_TRY
{
Ice::CommunicatorPtr p = getCommunicator(self);
Ice::LocatorPrx proxy;
if(!NIL_P(locator))
{
if(!checkProxy(locator))
{
throw RubyException(rb_eTypeError, "argument must be a proxy");
}
proxy = Ice::LocatorPrx::uncheckedCast(getProxy(locator));
}
p->setDefaultLocator(proxy);
}
ICE_RUBY_CATCH
return Qnil;
}
extern "C"
VALUE
IceRuby_Communicator_flushBatchRequests(VALUE self)
{
ICE_RUBY_TRY
{
Ice::CommunicatorPtr p = getCommunicator(self);
p->flushBatchRequests();
}
ICE_RUBY_CATCH
return Qnil;
}
void
IceRuby::initCommunicator(VALUE iceModule)
{
rb_define_module_function(iceModule, "initialize", CAST_METHOD(IceRuby_initialize), -1);
_communicatorClass = rb_define_class_under(iceModule, "CommunicatorI", rb_cObject);
rb_define_method(_communicatorClass, "destroy", CAST_METHOD(IceRuby_Communicator_destroy), 0);
rb_define_method(_communicatorClass, "shutdown", CAST_METHOD(IceRuby_Communicator_shutdown), 0);
rb_define_method(_communicatorClass, "isShutdown", CAST_METHOD(IceRuby_Communicator_isShutdown), 0);
rb_define_method(_communicatorClass, "stringToProxy", CAST_METHOD(IceRuby_Communicator_stringToProxy), 1);
rb_define_method(_communicatorClass, "proxyToString", CAST_METHOD(IceRuby_Communicator_proxyToString), 1);
rb_define_method(_communicatorClass, "propertyToProxy", CAST_METHOD(IceRuby_Communicator_propertyToProxy), 1);
rb_define_method(_communicatorClass, "proxyToProperty", CAST_METHOD(IceRuby_Communicator_proxyToProperty), 2);
rb_define_method(_communicatorClass, "stringToIdentity", CAST_METHOD(IceRuby_Communicator_stringToIdentity), 1);
rb_define_method(_communicatorClass, "identityToString", CAST_METHOD(IceRuby_Communicator_identityToString), 1);
rb_define_method(_communicatorClass, "addObjectFactory", CAST_METHOD(IceRuby_Communicator_addObjectFactory), 2);
rb_define_method(_communicatorClass, "findObjectFactory", CAST_METHOD(IceRuby_Communicator_findObjectFactory), 1);
rb_define_method(_communicatorClass, "getImplicitContext", CAST_METHOD(IceRuby_Communicator_getImplicitContext), 0);
rb_define_method(_communicatorClass, "getProperties", CAST_METHOD(IceRuby_Communicator_getProperties), 0);
rb_define_method(_communicatorClass, "getLogger", CAST_METHOD(IceRuby_Communicator_getLogger), 0);
rb_define_method(_communicatorClass, "getDefaultRouter", CAST_METHOD(IceRuby_Communicator_getDefaultRouter), 0);
rb_define_method(_communicatorClass, "setDefaultRouter", CAST_METHOD(IceRuby_Communicator_setDefaultRouter), 1);
rb_define_method(_communicatorClass, "getDefaultLocator", CAST_METHOD(IceRuby_Communicator_getDefaultLocator), 0);
rb_define_method(_communicatorClass, "setDefaultLocator", CAST_METHOD(IceRuby_Communicator_setDefaultLocator), 1);
rb_define_method(_communicatorClass, "flushBatchRequests", CAST_METHOD(IceRuby_Communicator_flushBatchRequests), 0);
}
Ice::CommunicatorPtr
IceRuby::getCommunicator(VALUE v)
{
Ice::CommunicatorPtr* p = reinterpret_cast<Ice::CommunicatorPtr*>(DATA_PTR(v));
assert(p);
return *p;
}
VALUE
IceRuby::lookupCommunicator(const Ice::CommunicatorPtr& p)
{
CommunicatorMap::iterator q = _communicatorMap.find(p.get());
if(q != _communicatorMap.end())
{
return q->second;
}
return Qnil;
}
|