1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4
5 #include <Endpoint.h>
6 #include <Util.h>
7 #include <Ice/Object.h>
8 #include <IceSSL/EndpointInfo.h>
9
10 using namespace std;
11 using namespace IceRuby;
12
13 static VALUE _endpointClass;
14
15 static VALUE _endpointInfoClass;
16 static VALUE _ipEndpointInfoClass;
17 static VALUE _tcpEndpointInfoClass;
18 static VALUE _udpEndpointInfoClass;
19 static VALUE _wsEndpointInfoClass;
20 static VALUE _opaqueEndpointInfoClass;
21 static VALUE _sslEndpointInfoClass;
22
23 // Endpoint
24
25 extern "C"
26 void
IceRuby_Endpoint_free(Ice::EndpointPtr * p)27 IceRuby_Endpoint_free(Ice::EndpointPtr* p)
28 {
29 assert(p);
30 delete p;
31 }
32
33 VALUE
createEndpoint(const Ice::EndpointPtr & p)34 IceRuby::createEndpoint(const Ice::EndpointPtr& p)
35 {
36 return Data_Wrap_Struct(_endpointClass, 0, IceRuby_Endpoint_free, new Ice::EndpointPtr(p));
37 }
38
39 extern "C"
40 VALUE
IceRuby_Endpoint_toString(VALUE self)41 IceRuby_Endpoint_toString(VALUE self)
42 {
43 ICE_RUBY_TRY
44 {
45 Ice::EndpointPtr* p = reinterpret_cast<Ice::EndpointPtr*>(DATA_PTR(self));
46 assert(p);
47
48 string s = (*p)->toString();
49 return createString(s);
50 }
51 ICE_RUBY_CATCH
52 return Qnil;
53 }
54
55 extern "C"
56 VALUE
IceRuby_Endpoint_getInfo(VALUE self)57 IceRuby_Endpoint_getInfo(VALUE self)
58 {
59 ICE_RUBY_TRY
60 {
61 Ice::EndpointPtr* p = reinterpret_cast<Ice::EndpointPtr*>(DATA_PTR(self));
62 assert(p);
63
64 Ice::EndpointInfoPtr info = (*p)->getInfo();
65 return createEndpointInfo(info);
66 }
67 ICE_RUBY_CATCH
68 return Qnil;
69 }
70
71 extern "C"
72 VALUE
IceRuby_Endpoint_cmp(VALUE self,VALUE other)73 IceRuby_Endpoint_cmp(VALUE self, VALUE other)
74 {
75 ICE_RUBY_TRY
76 {
77 if(NIL_P(other))
78 {
79 return INT2NUM(1);
80 }
81 if(!checkEndpoint(other))
82 {
83 throw RubyException(rb_eTypeError, "argument must be a endpoint");
84 }
85 Ice::EndpointPtr p1 = Ice::EndpointPtr(*reinterpret_cast<Ice::EndpointPtr*>(DATA_PTR(self)));
86 Ice::EndpointPtr p2 = Ice::EndpointPtr(*reinterpret_cast<Ice::EndpointPtr*>(DATA_PTR(other)));
87 if(p1 < p2)
88 {
89 return INT2NUM(-1);
90 }
91 else if(p1 == p2)
92 {
93 return INT2NUM(0);
94 }
95 else
96 {
97 return INT2NUM(1);
98 }
99 }
100 ICE_RUBY_CATCH
101 return Qnil;
102 }
103
104 extern "C"
105 VALUE
IceRuby_Endpoint_equals(VALUE self,VALUE other)106 IceRuby_Endpoint_equals(VALUE self, VALUE other)
107 {
108 return IceRuby_Endpoint_cmp(self, other) == INT2NUM(0) ? Qtrue : Qfalse;
109 }
110
111 // EndpointInfo
112
113 extern "C"
114 void
IceRuby_EndpointInfo_free(Ice::EndpointPtr * p)115 IceRuby_EndpointInfo_free(Ice::EndpointPtr* p)
116 {
117 assert(p);
118 delete p;
119 }
120
121 VALUE
createEndpointInfo(const Ice::EndpointInfoPtr & p)122 IceRuby::createEndpointInfo(const Ice::EndpointInfoPtr& p)
123 {
124 if(!p)
125 {
126 return Qnil;
127 }
128
129 VALUE info;
130 if(Ice::WSEndpointInfoPtr::dynamicCast(p))
131 {
132 info = Data_Wrap_Struct(_wsEndpointInfoClass, 0, IceRuby_EndpointInfo_free, new Ice::EndpointInfoPtr(p));
133
134 Ice::WSEndpointInfoPtr ws = Ice::WSEndpointInfoPtr::dynamicCast(p);
135 rb_ivar_set(info, rb_intern("@resource"), createString(ws->resource));
136 }
137 else if(Ice::TCPEndpointInfoPtr::dynamicCast(p))
138 {
139 info = Data_Wrap_Struct(_tcpEndpointInfoClass, 0, IceRuby_EndpointInfo_free, new Ice::EndpointInfoPtr(p));
140 }
141 else if(Ice::UDPEndpointInfoPtr::dynamicCast(p))
142 {
143 info = Data_Wrap_Struct(_udpEndpointInfoClass, 0, IceRuby_EndpointInfo_free, new Ice::EndpointInfoPtr(p));
144
145 Ice::UDPEndpointInfoPtr udp = Ice::UDPEndpointInfoPtr::dynamicCast(p);
146 rb_ivar_set(info, rb_intern("@mcastInterface"), createString(udp->mcastInterface));
147 rb_ivar_set(info, rb_intern("@mcastTtl"), INT2FIX(udp->mcastTtl));
148 }
149 else if(Ice::OpaqueEndpointInfoPtr::dynamicCast(p))
150 {
151 info = Data_Wrap_Struct(_opaqueEndpointInfoClass, 0, IceRuby_EndpointInfo_free, new Ice::EndpointInfoPtr(p));
152
153 Ice::OpaqueEndpointInfoPtr opaque = Ice::OpaqueEndpointInfoPtr::dynamicCast(p);
154 Ice::ByteSeq b = opaque->rawBytes;
155 volatile VALUE v = callRuby(rb_str_new, reinterpret_cast<const char*>(&b[0]), static_cast<long>(b.size()));
156 rb_ivar_set(info, rb_intern("@rawBytes"), v);
157 rb_ivar_set(info, rb_intern("@rawEncoding"), createEncodingVersion(opaque->rawEncoding));
158 }
159 else if(IceSSL::EndpointInfoPtr::dynamicCast(p))
160 {
161 info = Data_Wrap_Struct(_sslEndpointInfoClass, 0, IceRuby_EndpointInfo_free, new Ice::EndpointInfoPtr(p));
162 }
163 else if(Ice::IPEndpointInfoPtr::dynamicCast(p))
164 {
165 info = Data_Wrap_Struct(_ipEndpointInfoClass, 0, IceRuby_EndpointInfo_free, new Ice::EndpointInfoPtr(p));
166 }
167 else
168 {
169 info = Data_Wrap_Struct(_endpointInfoClass, 0, IceRuby_EndpointInfo_free, new Ice::EndpointInfoPtr(p));
170 }
171
172 if(Ice::IPEndpointInfoPtr::dynamicCast(p))
173 {
174 Ice::IPEndpointInfoPtr ip = Ice::IPEndpointInfoPtr::dynamicCast(p);
175 rb_ivar_set(info, rb_intern("@host"), createString(ip->host));
176 rb_ivar_set(info, rb_intern("@port"), INT2FIX(ip->port));
177 rb_ivar_set(info, rb_intern("@sourceAddress"), createString(ip->sourceAddress));
178 }
179
180 rb_ivar_set(info, rb_intern("@underlying"), createEndpointInfo(p->underlying));
181 rb_ivar_set(info, rb_intern("@timeout"), INT2FIX(p->timeout));
182 rb_ivar_set(info, rb_intern("@compress"), p->compress ? Qtrue : Qfalse);
183 return info;
184 }
185
186 extern "C"
187 VALUE
IceRuby_EndpointInfo_type(VALUE self)188 IceRuby_EndpointInfo_type(VALUE self)
189 {
190 ICE_RUBY_TRY
191 {
192 Ice::EndpointInfoPtr* p = reinterpret_cast<Ice::EndpointInfoPtr*>(DATA_PTR(self));
193 assert(p);
194
195 Ice::Short type = (*p)->type();
196 return INT2FIX(type);
197 }
198 ICE_RUBY_CATCH
199 return Qnil;
200 }
201
202 extern "C"
203 VALUE
IceRuby_EndpointInfo_datagram(VALUE self)204 IceRuby_EndpointInfo_datagram(VALUE self)
205 {
206 ICE_RUBY_TRY
207 {
208 Ice::EndpointInfoPtr* p = reinterpret_cast<Ice::EndpointInfoPtr*>(DATA_PTR(self));
209 assert(p);
210
211 bool result = (*p)->datagram();
212 return result ? Qtrue : Qfalse;
213 }
214 ICE_RUBY_CATCH
215 return Qnil;
216 }
217
218 extern "C"
219 VALUE
IceRuby_EndpointInfo_secure(VALUE self)220 IceRuby_EndpointInfo_secure(VALUE self)
221 {
222 ICE_RUBY_TRY
223 {
224 Ice::EndpointInfoPtr* p = reinterpret_cast<Ice::EndpointInfoPtr*>(DATA_PTR(self));
225 assert(p);
226
227 bool result = (*p)->secure();
228 return result ? Qtrue : Qfalse;
229 }
230 ICE_RUBY_CATCH
231 return Qnil;
232 }
233
234 void
initEndpoint(VALUE iceModule)235 IceRuby::initEndpoint(VALUE iceModule)
236 {
237 //
238 // Endpoint.
239 //
240 _endpointClass = rb_define_class_under(iceModule, "Endpoint", rb_cObject);
241
242 //
243 // Instance methods.
244 //
245 rb_define_method(_endpointClass, "toString", CAST_METHOD(IceRuby_Endpoint_toString), 0);
246 rb_define_method(_endpointClass, "getInfo", CAST_METHOD(IceRuby_Endpoint_getInfo), 0);
247 rb_define_method(_endpointClass, "to_s", CAST_METHOD(IceRuby_Endpoint_toString), 0);
248 rb_define_method(_endpointClass, "inspect", CAST_METHOD(IceRuby_Endpoint_toString), 0);
249 rb_define_method(_endpointClass, "<=>", CAST_METHOD(IceRuby_Endpoint_cmp), 1);
250 rb_define_method(_endpointClass, "==", CAST_METHOD(IceRuby_Endpoint_equals), 1);
251 rb_define_method(_endpointClass, "eql?", CAST_METHOD(IceRuby_Endpoint_equals), 1);
252
253 //
254 // EndpointInfo.
255 //
256 _endpointInfoClass = rb_define_class_under(iceModule, "EndpointInfo", rb_cObject);
257
258 //
259 // Instance methods.
260 //
261 rb_define_method(_endpointInfoClass, "type", CAST_METHOD(IceRuby_EndpointInfo_type), 0);
262 rb_define_method(_endpointInfoClass, "datagram", CAST_METHOD(IceRuby_EndpointInfo_datagram), 0);
263 rb_define_method(_endpointInfoClass, "secure", CAST_METHOD(IceRuby_EndpointInfo_secure), 0);
264
265 //
266 // Instance members.
267 //
268 rb_define_attr(_endpointInfoClass, "protocol", 1, 0);
269 rb_define_attr(_endpointInfoClass, "encoding", 1, 0);
270 rb_define_attr(_endpointInfoClass, "timeout", 1, 0);
271 rb_define_attr(_endpointInfoClass, "compress", 1, 0);
272
273 //
274 // IPEndpointInfo
275 //
276 _ipEndpointInfoClass = rb_define_class_under(iceModule, "IPEndpointInfo", _endpointInfoClass);
277
278 //
279 // Instance members.
280 //
281 rb_define_attr(_ipEndpointInfoClass, "host", 1, 0);
282 rb_define_attr(_ipEndpointInfoClass, "port", 1, 0);
283 rb_define_attr(_ipEndpointInfoClass, "sourceAddress", 1, 0);
284
285 //
286 // TCPEndpointInfo
287 //
288 _tcpEndpointInfoClass = rb_define_class_under(iceModule, "TCPEndpointInfo", _ipEndpointInfoClass);
289
290 //
291 // UDPEndpointInfo
292 //
293 _udpEndpointInfoClass = rb_define_class_under(iceModule, "UDPEndpointInfo", _ipEndpointInfoClass);
294
295 //
296 // Instance members.
297 //
298 rb_define_attr(_udpEndpointInfoClass, "mcastInterface", 1, 0);
299 rb_define_attr(_udpEndpointInfoClass, "mcastTtl", 1, 0);
300
301 //
302 // WSEndpointInfo
303 //
304 _wsEndpointInfoClass = rb_define_class_under(iceModule, "WSEndpointInfo", _endpointInfoClass);
305
306 //
307 // Instance members.
308 //
309 rb_define_attr(_wsEndpointInfoClass, "resource", 1, 0);
310
311 //
312 // OpaqueEndpointInfo
313 //
314 _opaqueEndpointInfoClass = rb_define_class_under(iceModule, "OpaqueEndpointInfo", _endpointInfoClass);
315
316 //
317 // Instance members.
318 //
319 rb_define_attr(_opaqueEndpointInfoClass, "rawBytes", 1, 0);
320 rb_define_attr(_opaqueEndpointInfoClass, "rawEncoding", 1, 0);
321
322 //
323 // SSLEndpointInfo
324 //
325 _sslEndpointInfoClass = rb_define_class_under(iceModule, "SSLEndpointInfo", _endpointInfoClass);
326 }
327
328 bool
checkEndpoint(VALUE v)329 IceRuby::checkEndpoint(VALUE v)
330 {
331 return callRuby(rb_obj_is_kind_of, v, _endpointClass) == Qtrue;
332 }
333