Skip to content

Commit

Permalink
Ruby Proxy cleanup (#2541)
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardnormier authored Jul 23, 2024
1 parent 0e019ef commit aa0c27a
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 134 deletions.
8 changes: 0 additions & 8 deletions ruby/ruby/Ice/Proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,13 @@ module Ice
# Provide some common functionality for proxy classes
module Proxy_mixin
module ClassMethods
def inspect
::Ice::__stringify(self, self.class::ICE_TYPE)
end

def ice_staticId()
self::ICE_ID
end

def checkedCast(proxy, facetOrContext=nil, context=nil)
ice_checkedCast(proxy, self::ICE_ID, facetOrContext, context)
end

def uncheckedCast(proxy, facet=nil)
ice_uncheckedCast(proxy, facet)
end
end

def self.included(base)
Expand Down
97 changes: 45 additions & 52 deletions ruby/src/IceRuby/Proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,24 @@ IceRuby_ObjectPrx_ice_getIdentity(VALUE self)
}

extern "C" VALUE
IceRuby_ObjectPrx_ice_identity(VALUE self, VALUE id)
IceRuby_ObjectPrx_ice_identity(VALUE self, VALUE args)
{
ICE_RUBY_TRY
{
VALUE cls = Qnil;
long len = RARRAY_LEN(args);
if (len > 2 || len == 0)
{
throw RubyException(rb_eArgError, "ice_identity requires one or two arguments");
}
if (len == 2)
{
cls = rb_ary_entry(args, 1);
}

Ice::ObjectPrx p = getProxy(self);
Ice::Identity ident = getIdentity(id);
return createProxy(p->ice_identity(ident));
Ice::Identity ident = getIdentity(rb_ary_entry(args, 0));
return createProxy(p->ice_identity(ident), cls);
}
ICE_RUBY_CATCH
return Qnil;
Expand Down Expand Up @@ -243,13 +254,24 @@ IceRuby_ObjectPrx_ice_getFacet(VALUE self)
}

extern "C" VALUE
IceRuby_ObjectPrx_ice_facet(VALUE self, VALUE facet)
IceRuby_ObjectPrx_ice_facet(VALUE self, VALUE args)
{
ICE_RUBY_TRY
{
VALUE cls = Qnil;
long len = RARRAY_LEN(args);
if (len > 2 || len == 0)
{
throw RubyException(rb_eArgError, "ice_facet requires one or two arguments");
}
if (len == 2)
{
cls = rb_ary_entry(args, 1);
}

Ice::ObjectPrx p = getProxy(self);
string f = getString(facet);
return createProxy(p->ice_facet(f));
string f = getString(rb_ary_entry(args, 0));
return createProxy(p->ice_facet(f), cls);
}
ICE_RUBY_CATCH
return Qnil;
Expand Down Expand Up @@ -1010,47 +1032,6 @@ IceRuby_ObjectPrx_checkedCast(int argc, VALUE* args, VALUE /*self*/)
return Qnil;
}

extern "C" VALUE
IceRuby_ObjectPrx_uncheckedCast(int argc, VALUE* args, VALUE /*self*/)
{
ICE_RUBY_TRY
{
if (argc < 1 || argc > 2)
{
throw RubyException(rb_eArgError, "uncheckedCast requires a proxy argument and an optional facet");
}

if (NIL_P(args[0]))
{
return Qnil;
}

if (!checkProxy(args[0]))
{
throw RubyException(rb_eArgError, "uncheckedCast requires a proxy argument");
}

volatile VALUE facet = Qnil;
if (argc == 2)
{
facet = args[1];
}

Ice::ObjectPrx p = getProxy(args[0]);

if (!NIL_P(facet))
{
return createProxy(p->ice_facet(getString(facet)));
}
else
{
return createProxy(p);
}
}
ICE_RUBY_CATCH
return Qnil;
}

extern "C" VALUE
IceRuby_ObjectPrx_ice_checkedCast(VALUE self, VALUE obj, VALUE id, VALUE facetOrContext, VALUE ctx)
{
Expand Down Expand Up @@ -1104,10 +1085,23 @@ IceRuby_ObjectPrx_ice_checkedCast(VALUE self, VALUE obj, VALUE id, VALUE facetOr
}

extern "C" VALUE
IceRuby_ObjectPrx_ice_uncheckedCast(VALUE self, VALUE obj, VALUE facet)
IceRuby_ObjectPrx_uncheckedCast(VALUE self, VALUE args)
{
ICE_RUBY_TRY
{
VALUE facet = Qnil;
long len = RARRAY_LEN(args);
if (len > 2 || len == 0)
{
throw RubyException(rb_eArgError, "uncheckedCast requires one or two arguments");
}

VALUE obj = rb_ary_entry(args, 0);
if (len == 2)
{
facet = rb_ary_entry(args, 1);
}

if (NIL_P(obj))
{
return Qnil;
Expand Down Expand Up @@ -1172,11 +1166,11 @@ IceRuby::initProxy(VALUE iceModule)
rb_define_method(_proxyClass, "ice_ids", CAST_METHOD(IceRuby_ObjectPrx_ice_ids), -1);
rb_define_method(_proxyClass, "ice_id", CAST_METHOD(IceRuby_ObjectPrx_ice_id), -1);
rb_define_method(_proxyClass, "ice_getIdentity", CAST_METHOD(IceRuby_ObjectPrx_ice_getIdentity), 0);
rb_define_method(_proxyClass, "ice_identity", CAST_METHOD(IceRuby_ObjectPrx_ice_identity), 1);
rb_define_method(_proxyClass, "ice_identity", CAST_METHOD(IceRuby_ObjectPrx_ice_identity), -2);
rb_define_method(_proxyClass, "ice_getContext", CAST_METHOD(IceRuby_ObjectPrx_ice_getContext), 0);
rb_define_method(_proxyClass, "ice_context", CAST_METHOD(IceRuby_ObjectPrx_ice_context), 1);
rb_define_method(_proxyClass, "ice_getFacet", CAST_METHOD(IceRuby_ObjectPrx_ice_getFacet), 0);
rb_define_method(_proxyClass, "ice_facet", CAST_METHOD(IceRuby_ObjectPrx_ice_facet), 1);
rb_define_method(_proxyClass, "ice_facet", CAST_METHOD(IceRuby_ObjectPrx_ice_facet), -2);
rb_define_method(_proxyClass, "ice_getAdapterId", CAST_METHOD(IceRuby_ObjectPrx_ice_getAdapterId), 0);
rb_define_method(_proxyClass, "ice_adapterId", CAST_METHOD(IceRuby_ObjectPrx_ice_adapterId), 1);
rb_define_method(_proxyClass, "ice_getEndpoints", CAST_METHOD(IceRuby_ObjectPrx_ice_getEndpoints), 0);
Expand Down Expand Up @@ -1242,9 +1236,8 @@ IceRuby::initProxy(VALUE iceModule)
// Static methods.
//
rb_define_singleton_method(_proxyClass, "checkedCast", CAST_METHOD(IceRuby_ObjectPrx_checkedCast), -1);
rb_define_singleton_method(_proxyClass, "uncheckedCast", CAST_METHOD(IceRuby_ObjectPrx_uncheckedCast), -1);
rb_define_singleton_method(_proxyClass, "uncheckedCast", CAST_METHOD(IceRuby_ObjectPrx_uncheckedCast), -2);
rb_define_singleton_method(_proxyClass, "ice_checkedCast", CAST_METHOD(IceRuby_ObjectPrx_ice_checkedCast), 4);
rb_define_singleton_method(_proxyClass, "ice_uncheckedCast", CAST_METHOD(IceRuby_ObjectPrx_ice_uncheckedCast), 2);
rb_define_singleton_method(_proxyClass, "ice_staticId", CAST_METHOD(IceRuby_ObjectPrx_ice_staticId), 0);
rb_define_singleton_method(_proxyClass, "new", CAST_METHOD(IceRuby_ObjectPrx_new), 2);
}
Expand Down
9 changes: 4 additions & 5 deletions ruby/test/Ice/exceptions/AllTests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def allTests(helper, communicator)

print "testing checked cast... "
STDOUT.flush
thrower = Test::ThrowerPrx::checkedCast(base)
thrower = Test::ThrowerPrx.checkedCast(base)
test(thrower)
test(thrower == base)
puts "ok"
Expand Down Expand Up @@ -291,7 +291,7 @@ def allTests(helper, communicator)

id = Ice::stringToIdentity("does not exist")
begin
thrower2 = Test::ThrowerPrx::uncheckedCast(thrower.ice_identity(id))
thrower2 = thrower.ice_identity(id, Test::ThrowerPrx)
thrower2.throwAasA(1)
# thrower2.ice_ping()
test(false)
Expand All @@ -306,11 +306,10 @@ def allTests(helper, communicator)

print "catching facet not exist exception... "
STDOUT.flush

begin
thrower2 = Test::ThrowerPrx::uncheckedCast(thrower, "no such facet")
thrower2 = thrower.ice_facet("no such facet", Test::ThrowerPrx)
begin
thrower2.ice_ping()
thrower2.throwAasA(1)
test(false)
rescue Ice::FacetNotExistException => ex
test(ex.facet == "no such facet")
Expand Down
24 changes: 12 additions & 12 deletions ruby/test/Ice/facets/AllTests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,21 @@ def allTests(helper, communicator)

print "testing checked cast... "
STDOUT.flush
obj = Ice::ObjectPrx::checkedCast(db)
obj = Ice::ObjectPrx.checkedCast(db)
test(obj.ice_getFacet() == "")
obj = Ice::ObjectPrx::checkedCast(db, "facetABCD")
obj = Ice::ObjectPrx.checkedCast(db, "facetABCD")
test(obj.ice_getFacet() == "facetABCD")
obj2 = Ice::ObjectPrx::checkedCast(obj)
obj2 = Ice::ObjectPrx.checkedCast(obj)
test(obj2.ice_getFacet() == "facetABCD")
obj3 = Ice::ObjectPrx::checkedCast(obj, "")
obj3 = Ice::ObjectPrx.checkedCast(obj, "")
test(obj3.ice_getFacet() == "")
d = Test::DPrx::checkedCast(db)
d = Test::DPrx.checkedCast(db)
test(d.ice_getFacet() == "")
df = Test::DPrx::checkedCast(db, "facetABCD")
df = Test::DPrx.checkedCast(db, "facetABCD")
test(df.ice_getFacet() == "facetABCD")
df2 = Test::DPrx::checkedCast(df)
df2 = Test::DPrx.checkedCast(df)
test(df2.ice_getFacet() == "facetABCD")
df3 = Test::DPrx::checkedCast(df, "")
df3 = Test::DPrx.checkedCast(df, "")
test(df3.ice_getFacet() == "")
puts "ok"

Expand All @@ -88,7 +88,7 @@ def allTests(helper, communicator)

print "testing facets A, B, C, and D... "
STDOUT.flush
df = Test::DPrx::checkedCast(d, "facetABCD")
df = Test::DPrx.checkedCast(d, "facetABCD")
test(df)
test(df.callA() == "A")
test(df.callB() == "B")
Expand All @@ -98,22 +98,22 @@ def allTests(helper, communicator)

print "testing facets E and F... "
STDOUT.flush
ff = Test::FPrx::checkedCast(d, "facetEF")
ff = Test::FPrx.checkedCast(d, "facetEF")
test(ff)
test(ff.callE() == "E")
test(ff.callF() == "F")
puts "ok"

print "testing facet G... "
STDOUT.flush
gf = Test::GPrx::checkedCast(ff, "facetGH")
gf = Test::GPrx.checkedCast(ff, "facetGH")
test(gf)
test(gf.callG() == "G")
puts "ok"

print "testing whether casting preserves the facet... "
STDOUT.flush
hf = Test::HPrx::checkedCast(gf)
hf = Test::HPrx.checkedCast(gf)
test(hf)
test(hf.callG() == "G")
test(hf.callH() == "H")
Expand Down
11 changes: 5 additions & 6 deletions ruby/test/Ice/info/AllTests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,12 @@ def allTests(helper, communicator)
defaultHost = communicator.getProperties().getProperty("Ice.Default.Host")
tcpEndpoint = helper.getTestEndpoint()
udpEndpoint = helper.getTestEndpoint(protocol:"udp")
base = communicator.stringToProxy("test:#{tcpEndpoint}:#{udpEndpoint}")
testIntf = Test::TestIntfPrx::checkedCast(base)
testIntf = Test::TestIntfPrx.new(communicator, "test:#{tcpEndpoint}:#{udpEndpoint}")

print "test connection endpoint information..."
STDOUT.flush
port = helper.getTestPort()
tcpinfo = getTCPEndpointInfo(base.ice_getConnection().getEndpoint().getInfo())
tcpinfo = getTCPEndpointInfo(testIntf.ice_getConnection().getEndpoint().getInfo())
test(tcpinfo.port == port)
test(!tcpinfo.compress)
test(tcpinfo.host == defaultHost)
Expand All @@ -86,7 +85,7 @@ def allTests(helper, communicator)
port = Integer(ctx["port"])
test(port > 0)

udp = base.ice_datagram().ice_getConnection().getEndpoint().getInfo()
udp = testIntf.ice_datagram().ice_getConnection().getEndpoint().getInfo()
test(udp.port == port)
test(udp.host == defaultHost)

Expand All @@ -95,7 +94,7 @@ def allTests(helper, communicator)
print "testing connection information..."
STDOUT.flush

connection = base.ice_getConnection()
connection = testIntf.ice_getConnection()
connection.setBufferSize(1024, 2048)

info = connection.getInfo()
Expand All @@ -119,7 +118,7 @@ def allTests(helper, communicator)
test(ctx["remotePort"] == tcpinfo.localPort.to_s())
test(ctx["localPort"] == tcpinfo.remotePort.to_s())

if base.ice_getConnection().type() == "ws" || base.ice_getConnection().type() == "wss"
if testIntf.ice_getConnection().type() == "ws" || testIntf.ice_getConnection().type() == "wss"
test(info.is_a?(Ice::WSConnectionInfo))

test(info.headers["Upgrade"] == "websocket")
Expand Down
Loading

0 comments on commit aa0c27a

Please sign in to comment.