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
|
// **********************************************************************
//
// Copyright (c) 2003-2015 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 <Connection.h>
#include <Endpoint.h>
#include <Types.h>
#include <Util.h>
#include <Ice/Object.h>
using namespace std;
using namespace IceRuby;
static VALUE _connectionClass;
static VALUE _connectionInfoClass;
static VALUE _ipConnectionInfoClass;
static VALUE _tcpConnectionInfoClass;
static VALUE _udpConnectionInfoClass;
static VALUE _wsConnectionInfoClass;
// **********************************************************************
// Connection
// **********************************************************************
extern "C"
void
IceRuby_Connection_free(Ice::ConnectionPtr* p)
{
assert(p);
delete p;
}
VALUE
IceRuby::createConnection(const Ice::ConnectionPtr& p)
{
return Data_Wrap_Struct(_connectionClass, 0, IceRuby_Connection_free, new Ice::ConnectionPtr(p));
}
extern "C"
VALUE
IceRuby_Connection_close(VALUE self, VALUE b)
{
ICE_RUBY_TRY
{
Ice::ConnectionPtr* p = reinterpret_cast<Ice::ConnectionPtr*>(DATA_PTR(self));
assert(p);
(*p)->close(RTEST(b));
}
ICE_RUBY_CATCH
return Qnil;
}
extern "C"
VALUE
IceRuby_Connection_flushBatchRequests(VALUE self)
{
ICE_RUBY_TRY
{
Ice::ConnectionPtr* p = reinterpret_cast<Ice::ConnectionPtr*>(DATA_PTR(self));
assert(p);
(*p)->flushBatchRequests();
}
ICE_RUBY_CATCH
return Qnil;
}
extern "C"
VALUE
IceRuby_Connection_setACM(VALUE self, VALUE t, VALUE c, VALUE h)
{
ICE_RUBY_TRY
{
Ice::ConnectionPtr* p = reinterpret_cast<Ice::ConnectionPtr*>(DATA_PTR(self));
assert(p);
IceUtil::Optional<Ice::Int> timeout;
IceUtil::Optional<Ice::ACMClose> close;
IceUtil::Optional<Ice::ACMHeartbeat> heartbeat;
if(t != Unset)
{
timeout = static_cast<Ice::Int>(getInteger(t));
}
if(c != Unset)
{
volatile VALUE type = callRuby(rb_path2class, "Ice::ACMClose");
if(callRuby(rb_obj_is_instance_of, c, type) != Qtrue)
{
throw RubyException(rb_eTypeError,
"value for 'close' argument must be Unset or an enumerator of Ice.ACMClose");
}
volatile VALUE closeValue = callRuby(rb_funcall, c, rb_intern("to_i"), 0);
assert(TYPE(closeValue) == T_FIXNUM);
close = static_cast<Ice::ACMClose>(FIX2LONG(closeValue));
}
if(h != Unset)
{
volatile VALUE type = callRuby(rb_path2class, "Ice::ACMHeartbeat");
if(callRuby(rb_obj_is_instance_of, h, type) != Qtrue)
{
throw RubyException(rb_eTypeError,
"value for 'heartbeat' argument must be Unset or an enumerator of Ice.ACMHeartbeat");
}
volatile VALUE heartbeatValue = callRuby(rb_funcall, h, rb_intern("to_i"), 0);
assert(TYPE(heartbeatValue) == T_FIXNUM);
heartbeat = static_cast<Ice::ACMHeartbeat>(FIX2LONG(heartbeatValue));
}
(*p)->setACM(timeout, close, heartbeat);
}
ICE_RUBY_CATCH
return Qnil;
}
extern "C"
VALUE
IceRuby_Connection_getACM(VALUE self)
{
ICE_RUBY_TRY
{
Ice::ConnectionPtr* p = reinterpret_cast<Ice::ConnectionPtr*>(DATA_PTR(self));
assert(p);
Ice::ACM acm = (*p)->getACM();
volatile VALUE type = callRuby(rb_path2class, "Ice::ACM");
assert(type != Qnil);
volatile VALUE r = callRuby(rb_class_new_instance, 0, static_cast<VALUE*>(0), type);
assert(r != Qnil);
callRuby(rb_ivar_set, r, rb_intern("@timeout"), LONG2FIX(acm.timeout));
type = callRuby(rb_path2class, "Ice::ACMClose");
assert(type != Qnil);
volatile VALUE c = callRuby(rb_funcall, type, rb_intern("from_int"), 1, LONG2NUM(static_cast<int>(acm.close)));
callRuby(rb_ivar_set, r, rb_intern("@close"), c);
type = callRuby(rb_path2class, "Ice::ACMHeartbeat");
assert(type != Qnil);
volatile VALUE h =
callRuby(rb_funcall, type, rb_intern("from_int"), 1, LONG2NUM(static_cast<int>(acm.heartbeat)));
callRuby(rb_ivar_set, r, rb_intern("@heartbeat"), h);
return r;
}
ICE_RUBY_CATCH
return Qnil;
}
extern "C"
VALUE
IceRuby_Connection_type(VALUE self)
{
ICE_RUBY_TRY
{
Ice::ConnectionPtr* p = reinterpret_cast<Ice::ConnectionPtr*>(DATA_PTR(self));
assert(p);
string s = (*p)->type();
return createString(s);
}
ICE_RUBY_CATCH
return Qnil;
}
extern "C"
VALUE
IceRuby_Connection_timeout(VALUE self)
{
ICE_RUBY_TRY
{
Ice::ConnectionPtr* p = reinterpret_cast<Ice::ConnectionPtr*>(DATA_PTR(self));
assert(p);
Ice::Int timeout = (*p)->timeout();
return INT2FIX(timeout);
}
ICE_RUBY_CATCH
return Qnil;
}
extern "C"
VALUE
IceRuby_Connection_getInfo(VALUE self)
{
ICE_RUBY_TRY
{
Ice::ConnectionPtr* p = reinterpret_cast<Ice::ConnectionPtr*>(DATA_PTR(self));
assert(p);
Ice::ConnectionInfoPtr info = (*p)->getInfo();
return createConnectionInfo(info);
}
ICE_RUBY_CATCH
return Qnil;
}
extern "C"
VALUE
IceRuby_Connection_getEndpoint(VALUE self)
{
ICE_RUBY_TRY
{
Ice::ConnectionPtr* p = reinterpret_cast<Ice::ConnectionPtr*>(DATA_PTR(self));
assert(p);
Ice::EndpointPtr endpoint = (*p)->getEndpoint();
return createEndpoint(endpoint);
}
ICE_RUBY_CATCH
return Qnil;
}
extern "C"
VALUE
IceRuby_Connection_setBufferSize(VALUE self, VALUE r, VALUE s)
{
ICE_RUBY_TRY
{
Ice::ConnectionPtr* p = reinterpret_cast<Ice::ConnectionPtr*>(DATA_PTR(self));
assert(p);
int rcvSize = static_cast<int>(getInteger(r));
int sndSize = static_cast<int>(getInteger(s));
(*p)->setBufferSize(rcvSize, sndSize);
}
ICE_RUBY_CATCH
return Qnil;
}
extern "C"
VALUE
IceRuby_Connection_toString(VALUE self)
{
ICE_RUBY_TRY
{
Ice::ConnectionPtr* p = reinterpret_cast<Ice::ConnectionPtr*>(DATA_PTR(self));
assert(p);
string s = (*p)->toString();
return createString(s);
}
ICE_RUBY_CATCH
return Qnil;
}
extern "C"
VALUE
IceRuby_Connection_equals(VALUE self, VALUE other)
{
ICE_RUBY_TRY
{
if(NIL_P(other))
{
return Qfalse;
}
if(callRuby(rb_obj_is_kind_of, other, _connectionClass) != Qtrue)
{
throw RubyException(rb_eTypeError, "argument must be a connection");
}
Ice::ConnectionPtr* p1 = reinterpret_cast<Ice::ConnectionPtr*>(DATA_PTR(self));
Ice::ConnectionPtr* p2 = reinterpret_cast<Ice::ConnectionPtr*>(DATA_PTR(other));
return *p1 == *p2 ? Qtrue : Qfalse;
}
ICE_RUBY_CATCH
return Qnil;
}
// **********************************************************************
// ConnectionInfo
// **********************************************************************
extern "C"
void
IceRuby_ConnectionInfo_free(Ice::ConnectionInfoPtr* p)
{
assert(p);
delete p;
}
VALUE
IceRuby::createConnectionInfo(const Ice::ConnectionInfoPtr& p)
{
VALUE info;
if(Ice::TCPConnectionInfoPtr::dynamicCast(p))
{
info = Data_Wrap_Struct(_tcpConnectionInfoClass, 0, IceRuby_ConnectionInfo_free, new Ice::ConnectionInfoPtr(p));
Ice::TCPConnectionInfoPtr tcp = Ice::TCPConnectionInfoPtr::dynamicCast(p);
rb_ivar_set(info, rb_intern("@localAddress"), createString(tcp->localAddress));
rb_ivar_set(info, rb_intern("@localPort"), INT2FIX(tcp->localPort));
rb_ivar_set(info, rb_intern("@remoteAddress"), createString(tcp->remoteAddress));
rb_ivar_set(info, rb_intern("@remotePort"), INT2FIX(tcp->remotePort));
}
else if(Ice::UDPConnectionInfoPtr::dynamicCast(p))
{
info = Data_Wrap_Struct(_udpConnectionInfoClass, 0, IceRuby_ConnectionInfo_free, new Ice::ConnectionInfoPtr(p));
Ice::UDPConnectionInfoPtr udp = Ice::UDPConnectionInfoPtr::dynamicCast(p);
rb_ivar_set(info, rb_intern("@localAddress"), createString(udp->localAddress));
rb_ivar_set(info, rb_intern("@localPort"), INT2FIX(udp->localPort));
rb_ivar_set(info, rb_intern("@remoteAddress"), createString(udp->remoteAddress));
rb_ivar_set(info, rb_intern("@remotePort"), INT2FIX(udp->remotePort));
rb_ivar_set(info, rb_intern("@mcastAddress"), createString(udp->mcastAddress));
rb_ivar_set(info, rb_intern("@mcastPort"), INT2FIX(udp->mcastPort));
}
else if(Ice::WSConnectionInfoPtr::dynamicCast(p))
{
info = Data_Wrap_Struct(_wsConnectionInfoClass, 0, IceRuby_ConnectionInfo_free, new Ice::ConnectionInfoPtr(p));
Ice::WSConnectionInfoPtr ws = Ice::WSConnectionInfoPtr::dynamicCast(p);
rb_ivar_set(info, rb_intern("@localAddress"), createString(ws->localAddress));
rb_ivar_set(info, rb_intern("@localPort"), INT2FIX(ws->localPort));
rb_ivar_set(info, rb_intern("@remoteAddress"), createString(ws->remoteAddress));
rb_ivar_set(info, rb_intern("@remotePort"), INT2FIX(ws->remotePort));
volatile VALUE result = callRuby(rb_hash_new);
for(Ice::HeaderDict::const_iterator q = ws->headers.begin(); q != ws->headers.end(); ++q)
{
volatile VALUE key = createString(q->first);
volatile VALUE value = createString(q->second);
callRuby(rb_hash_aset, result, key, value);
}
rb_ivar_set(info, rb_intern("@headers"), result);
}
else if(Ice::IPConnectionInfoPtr::dynamicCast(p))
{
info = Data_Wrap_Struct(_ipConnectionInfoClass, 0, IceRuby_ConnectionInfo_free, new Ice::ConnectionInfoPtr(p));
Ice::IPConnectionInfoPtr ip = Ice::IPConnectionInfoPtr::dynamicCast(p);
rb_ivar_set(info, rb_intern("@localAddress"), createString(ip->localAddress));
rb_ivar_set(info, rb_intern("@localPort"), INT2FIX(ip->localPort));
rb_ivar_set(info, rb_intern("@remoteAddress"), createString(ip->remoteAddress));
rb_ivar_set(info, rb_intern("@remotePort"), INT2FIX(ip->remotePort));
}
else
{
info = Data_Wrap_Struct(_connectionInfoClass, 0, IceRuby_ConnectionInfo_free, new Ice::ConnectionInfoPtr(p));
}
rb_ivar_set(info, rb_intern("@incoming"), p->incoming ? Qtrue : Qfalse);
rb_ivar_set(info, rb_intern("@adapterName"), createString(p->adapterName));
rb_ivar_set(info, rb_intern("@rcvSize"), INT2FIX(p->rcvSize));
rb_ivar_set(info, rb_intern("@sndSize"), INT2FIX(p->sndSize));
return info;
}
void
IceRuby::initConnection(VALUE iceModule)
{
//
// Connection.
//
_connectionClass = rb_define_class_under(iceModule, "ConnectionI", rb_cObject);
//
// Instance methods.
//
rb_define_method(_connectionClass, "close", CAST_METHOD(IceRuby_Connection_close), 1);
rb_define_method(_connectionClass, "flushBatchRequests", CAST_METHOD(IceRuby_Connection_flushBatchRequests), 0);
rb_define_method(_connectionClass, "setACM", CAST_METHOD(IceRuby_Connection_setACM), 3);
rb_define_method(_connectionClass, "getACM", CAST_METHOD(IceRuby_Connection_getACM), 0);
rb_define_method(_connectionClass, "type", CAST_METHOD(IceRuby_Connection_type), 0);
rb_define_method(_connectionClass, "timeout", CAST_METHOD(IceRuby_Connection_timeout), 0);
rb_define_method(_connectionClass, "getInfo", CAST_METHOD(IceRuby_Connection_getInfo), 0);
rb_define_method(_connectionClass, "getEndpoint", CAST_METHOD(IceRuby_Connection_getEndpoint), 0);
rb_define_method(_connectionClass, "setBufferSize", CAST_METHOD(IceRuby_Connection_setBufferSize), 2);
rb_define_method(_connectionClass, "toString", CAST_METHOD(IceRuby_Connection_toString), 0);
rb_define_method(_connectionClass, "to_s", CAST_METHOD(IceRuby_Connection_toString), 0);
rb_define_method(_connectionClass, "inspect", CAST_METHOD(IceRuby_Connection_toString), 0);
rb_define_method(_connectionClass, "==", CAST_METHOD(IceRuby_Connection_equals), 1);
rb_define_method(_connectionClass, "eql?", CAST_METHOD(IceRuby_Connection_equals), 1);
//
// ConnectionInfo.
//
_connectionInfoClass = rb_define_class_under(iceModule, "ConnectionInfo", rb_cObject);
//
// Instance members.
//
rb_define_attr(_connectionInfoClass, "incoming", 1, 0);
rb_define_attr(_connectionInfoClass, "adapterName", 1, 0);
rb_define_attr(_connectionInfoClass, "rcvSize", 1, 0);
rb_define_attr(_connectionInfoClass, "sndSize", 1, 0);
//
// IPConnectionInfo
//
_ipConnectionInfoClass = rb_define_class_under(iceModule, "IPConnectionInfo", _connectionInfoClass);
//
// Instance members.
//
rb_define_attr(_ipConnectionInfoClass, "localAddress", 1, 0);
rb_define_attr(_ipConnectionInfoClass, "localPort", 1, 0);
rb_define_attr(_ipConnectionInfoClass, "remoteAddress", 1, 0);
rb_define_attr(_ipConnectionInfoClass, "remotePort", 1, 0);
//
// TCPConnectionInfo
//
_tcpConnectionInfoClass = rb_define_class_under(iceModule, "TCPConnectionInfo", _ipConnectionInfoClass);
//
// UDPConnectionInfo
//
_udpConnectionInfoClass = rb_define_class_under(iceModule, "UDPConnectionInfo", _ipConnectionInfoClass);
//
// Instance members.
//
rb_define_attr(_udpConnectionInfoClass, "mcastAddress", 1, 0);
rb_define_attr(_udpConnectionInfoClass, "mcastPort", 1, 0);
//
// WSConnectionInfo
//
_wsConnectionInfoClass = rb_define_class_under(iceModule, "WSConnectionInfo", _ipConnectionInfoClass);
//
// Instance members.
//
//rb_define_attr(_wsConnectionInfoClass, "headers", 1, 0);
}
|