Skip to content

Commit

Permalink
New makeProxy factory function in Swift (#2275)
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardnormier authored Jun 7, 2024
1 parent efb2642 commit 5d5b650
Show file tree
Hide file tree
Showing 16 changed files with 127 additions and 99 deletions.
18 changes: 18 additions & 0 deletions cpp/src/slice2swift/Gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,24 @@ Gen::ProxyVisitor::visitInterfaceDefStart(const InterfaceDefPtr& p)

out << eb;

//
// makeProxy
//
out << sp;
out << nl << "/// Makes a new proxy from a communicator and a proxy string.";
out << nl << "///";
out << nl << "/// - Parameters:";
out << nl << "/// - communicator: The communicator of the new proxy.";
out << nl << "/// - proxyString: The proxy string to parse.";
out << nl << "/// - type: The type of the new proxy.";
out << nl << "/// - Throws: `Ice.ProxyParseException` if the proxy string is invalid.";
out << nl << "/// - Returns: A new proxy with the requested type.";
out << nl << "public func makeProxy(communicator: Ice.Communicator, proxyString: String, type: " << prx
<< ".Protocol) throws -> " << prx;
out << sb;
out << nl << "try communicator.makeProxyImpl(proxyString) as " << prxI;
out << eb;

//
// checkedCast
//
Expand Down
6 changes: 3 additions & 3 deletions cpp/test/Ice/binding/AllTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ allTests(Test::TestHelper* helper)
int i;

//
// Ensure that endpoints are tried in order by deactiving the adapters
// Ensure that endpoints are tried in order by deactivating the adapters
// one after the other.
//
for (i = 0; i < nRetry && test->getAdapterName() == "Adapter31"; i++)
Expand Down Expand Up @@ -616,7 +616,7 @@ allTests(Test::TestHelper* helper)
int i;

//
// Ensure that endpoints are tried in order by deactiving the adapters
// Ensure that endpoints are tried in order by deactivating the adapters
// one after the other.
//
for (i = 0; i < nRetry && test->getAdapterName() == "Adapter61"; i++)
Expand Down Expand Up @@ -707,7 +707,7 @@ allTests(Test::TestHelper* helper)
int i;

//
// Ensure that endpoints are tried in order by deactiving the adapters
// Ensure that endpoints are tried in order by deactivating the adapters
// one after the other.
//
for (i = 0; i < nRetry && getAdapterNameWithAMI(test) == "AdapterAMI61"; i++)
Expand Down
6 changes: 3 additions & 3 deletions csharp/test/Ice/binding/AllTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public static async Task allTests(global::Test.TestHelper helper)
int i;

//
// Ensure that endpoints are tried in order by deactiving the adapters
// Ensure that endpoints are tried in order by deactivating the adapters
// one after the other.
//
for (i = 0; i < nRetry && obj.getAdapterName() == "Adapter31"; i++) ;
Expand Down Expand Up @@ -585,7 +585,7 @@ public static async Task allTests(global::Test.TestHelper helper)
int i;

//
// Ensure that endpoints are tried in order by deactiving the adapters
// Ensure that endpoints are tried in order by deactivating the adapters
// one after the other.
//
for (i = 0; i < nRetry && obj.getAdapterName() == "Adapter61"; i++) ;
Expand Down Expand Up @@ -648,7 +648,7 @@ public static async Task allTests(global::Test.TestHelper helper)
int i;

//
// Ensure that endpoints are tried in order by deactiving the adapters
// Ensure that endpoints are tried in order by deactivating the adapters
// one after the other.
//
for (i = 0; i < nRetry && (await obj.getAdapterNameAsync()) == "AdapterAMI61"; i++) ;
Expand Down
6 changes: 3 additions & 3 deletions java/test/src/main/java/test/Ice/binding/AllTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ public static void allTests(test.TestHelper helper) {
int i;

//
// Ensure that endpoints are tried in order by deactiving the adapters
// Ensure that endpoints are tried in order by deactivating the adapters
// one after the other.
//
for (i = 0; i < nRetry && test.getAdapterName().equals("Adapter31"); i++)
Expand Down Expand Up @@ -566,7 +566,7 @@ public static void allTests(test.TestHelper helper) {
int i;

//
// Ensure that endpoints are tried in order by deactiving the adapters
// Ensure that endpoints are tried in order by deactivating the adapters
// one after the other.
//
for (i = 0; i < nRetry && test.getAdapterName().equals("Adapter61"); i++)
Expand Down Expand Up @@ -637,7 +637,7 @@ public static void allTests(test.TestHelper helper) {
int i;

//
// Ensure that endpoints are tried in order by deactiving the adapters
// Ensure that endpoints are tried in order by deactivating the adapters
// one after the other.
//
for (i = 0; i < nRetry && getAdapterNameWithAMI(test).equals("AdapterAMI61"); i++)
Expand Down
4 changes: 2 additions & 2 deletions js/test/Ice/binding/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@
const nRetry = 3;

//
// Ensure that endpoints are tried in order by deactiving the adapters
// Ensure that endpoints are tried in order by deactivating the adapters
// one after the other.
//
/* eslint-disable curly */
Expand Down Expand Up @@ -523,7 +523,7 @@
let i;

//
// Ensure that endpoints are tried in order by deactiving the adapters
// Ensure that endpoints are tried in order by deactivating the adapters
// one after the other.
//

Expand Down
4 changes: 2 additions & 2 deletions js/test/typescript/Ice/binding/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ export class Client extends TestHelper
const nRetry = 3;

//
// Ensure that endpoints are tried in order by deactiving the adapters
// Ensure that endpoints are tried in order by deactivating the adapters
// one after the other.
//
/* eslint-disable curly */
Expand Down Expand Up @@ -505,7 +505,7 @@ export class Client extends TestHelper
let i;

//
// Ensure that endpoints are tried in order by deactiving the adapters
// Ensure that endpoints are tried in order by deactivating the adapters
// one after the other.
//

Expand Down
6 changes: 3 additions & 3 deletions matlab/test/Ice/binding/AllTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ function allTests(helper)
assert(test.ice_getEndpointSelection() == Ice.EndpointSelectionType.Ordered);

%
% Ensure that endpoints are tried in order by deactiving the adapters
% Ensure that endpoints are tried in order by deactivating the adapters
% one after the other.
%
nRetry = 5;
Expand Down Expand Up @@ -581,7 +581,7 @@ function allTests(helper)
assert(~test.ice_isConnectionCached());

%
% Ensure that endpoints are tried in order by deactiving the adapters
% Ensure that endpoints are tried in order by deactivating the adapters
% one after the other.
%
nRetry = 5;
Expand Down Expand Up @@ -666,7 +666,7 @@ function allTests(helper)
assert(~test.ice_isConnectionCached());

%
% Ensure that endpoints are tried in order by deactiving the adapters
% Ensure that endpoints are tried in order by deactivating the adapters
% one after the other.
%
nRetry = 5;
Expand Down
4 changes: 2 additions & 2 deletions php/test/Ice/binding/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ function allTests($helper)
$nRetry = 5;

//
// Ensure that endpoints are tried in order by deactiving the adapters
// Ensure that endpoints are tried in order by deactivating the adapters
// one after the other.
//
for($i = 0; $i < $nRetry && $test->getAdapterName() == "Adapter31"; $i++);
Expand Down Expand Up @@ -358,7 +358,7 @@ function allTests($helper)
$nRetry = 5;

//
// Ensure that endpoints are tried in order by deactiving the adapters
// Ensure that endpoints are tried in order by deactivating the adapters
// one after the other.
//
for($i = 0; $i < $nRetry && $test->getAdapterName() == "Adapter61"; $i++);
Expand Down
4 changes: 2 additions & 2 deletions python/test/Ice/binding/AllTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ def allTests(helper, communicator):
nRetry = 5

#
# Ensure that endpoints are tried in order by deactiving the adapters
# Ensure that endpoints are tried in order by deactivating the adapters
# one after the other.
#
i = 0
Expand Down Expand Up @@ -561,7 +561,7 @@ def allTests(helper, communicator):
nRetry = 5

#
# Ensure that endpoints are tried in order by deactiving the adapters
# Ensure that endpoints are tried in order by deactivating the adapters
# one after the other.
#
i = 0
Expand Down
2 changes: 1 addition & 1 deletion ruby/test/Ice/binding/AllTests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def allTests(helper, communicator)
nRetry = 5

#
# Ensure that endpoints are tried in order by deactiving the adapters
# Ensure that endpoints are tried in order by deactivating the adapters
# one after the other.
#
i = 0
Expand Down
6 changes: 6 additions & 0 deletions swift/src/Ice/Communicator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,10 @@ public protocol Communicator: AnyObject {
///
/// - returns: `Dispatch.DispatchQueue` - The dispatch queue associated wih the Communicator's server thread pool.
func getServerDispatchQueue() throws -> Dispatch.DispatchQueue

/// Makes a new proxy. This is an internal operation used by the generated code.
///
/// - Parameter proxyString: The stringified proxy.
/// - Returns: The new proxy.
func makeProxyImpl<ProxyImpl>(_ proxyString: String) throws -> ProxyImpl where ProxyImpl: ObjectPrxI
}
23 changes: 17 additions & 6 deletions swift/src/Ice/CommunicatorI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,7 @@ class CommunicatorI: LocalObject<ICECommunicator>, Communicator {
}

func stringToProxy(_ str: String) throws -> ObjectPrx? {
return try autoreleasepool {
guard let prxHandle = try handle.stringToProxy(str: str) as? ICEObjectPrx else {
return nil
}
return ObjectPrxI(handle: prxHandle, communicator: self)
}
try stringToProxyImpl(str)
}

func proxyToString(_ obj: ObjectPrx?) -> String {
Expand Down Expand Up @@ -256,6 +251,22 @@ class CommunicatorI: LocalObject<ICECommunicator>, Communicator {
try handle.getServerDispatchQueue()
}
}

func makeProxyImpl<ProxyImpl>(_ proxyString: String) throws -> ProxyImpl where ProxyImpl: ObjectPrxI {
guard let proxy: ProxyImpl = try stringToProxyImpl(proxyString) else {
throw ProxyParseException(str: "Invalid empty proxy string")
}
return proxy
}

private func stringToProxyImpl<ProxyImpl>(_ str: String) throws -> ProxyImpl? where ProxyImpl: ObjectPrxI {
return try autoreleasepool {
guard let prxHandle = try handle.stringToProxy(str: str) as? ICEObjectPrx else {
return nil
}
return ProxyImpl(handle: prxHandle, communicator: self)
}
}
}

extension Communicator {
Expand Down
18 changes: 15 additions & 3 deletions swift/src/Ice/Proxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,19 @@ public protocol ObjectPrx: CustomStringConvertible, AnyObject {
func ice_collocationOptimized(_ collocated: Bool) -> Self
}

/// Makes a new proxy from a communicator and a proxy string.
///
/// - Parameters:
/// - communicator: The communicator of the new proxy.
/// - proxyString: The proxy string to parse.
/// - type: The type of the new proxy.
/// - Throws: `Ice.ProxyParseException` if the proxy string is invalid.
/// - Returns: A new proxy with the requested type.
public func makeProxy(communicator: Ice.Communicator, proxyString: String, type: ObjectPrx.Protocol) throws -> ObjectPrx
{
try communicator.makeProxyImpl(proxyString) as ObjectPrxI
}

/// Casts a proxy to `Ice.ObjectPrx`. This call contacts the server and will throw an Ice run-time exception
/// if the target object does not exist or the server cannot be reached.
///
Expand Down Expand Up @@ -784,9 +797,8 @@ open class ObjectPrxI: ObjectPrx {
isTwoway = impl.isTwoway
}

func fromICEObjectPrx<ObjectPrxType>(_ h: ICEObjectPrx) -> ObjectPrxType
where ObjectPrxType: ObjectPrxI {
return ObjectPrxType(handle: h, communicator: communicator)
private func fromICEObjectPrx<ProxyImpl>(_ h: ICEObjectPrx) -> ProxyImpl where ProxyImpl: ObjectPrxI {
return ProxyImpl(handle: h, communicator: communicator)
}

static func fromICEObjectPrx(
Expand Down
21 changes: 10 additions & 11 deletions swift/test/Ice/ami/AllTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ func allTests(_ helper: TestHelper, collocated: Bool = false) throws {
let communicator = helper.communicator()
let output = helper.getWriter()

var sref = "test:\(helper.getTestEndpoint(num: 0))"
var obj = try communicator.stringToProxy(sref)!
var p = try makeProxy(
communicator: communicator, proxyString: "test:\(helper.getTestEndpoint(num: 0))", type: TestIntfPrx.self)

var p = uncheckedCast(prx: obj, type: TestIntfPrx.self)
sref = "testController:\(helper.getTestEndpoint(num: 1))"
obj = try communicator.stringToProxy(sref)!

let testController = uncheckedCast(prx: obj, type: TestIntfControllerPrx.self)
let testController = try makeProxy(
communicator: communicator,
proxyString: "testController:\(helper.getTestEndpoint(num: 1))",
type: TestIntfControllerPrx.self)

output.write("testing async invocation...")
do {
Expand Down Expand Up @@ -61,7 +60,7 @@ func allTests(_ helper: TestHelper, collocated: Bool = false) throws {

output.write("testing local exceptions... ")
do {
let indirect = uncheckedCast(prx: p.ice_adapterId("dummy"), type: TestIntfPrx.self)
let indirect = p.ice_adapterId("dummy")
try indirect.opAsync().wait()
} catch is Ice.NoEndpointException {}

Expand All @@ -77,8 +76,8 @@ func allTests(_ helper: TestHelper, collocated: Bool = false) throws {
var initData = Ice.InitializationData()
initData.properties = communicator.getProperties().clone()
let ic = try helper.initialize(initData)
let o = try ic.stringToProxy(p.ice_toString())!
let p2 = try checkedCast(prx: o, type: TestIntfPrx.self)!
let p2 = try makeProxy(communicator: ic, proxyString: p.ice_toString(), type: TestIntfPrx.self)
try p2.ice_pingAsync().wait()
ic.destroy()
do {
try p2.opAsync().wait()
Expand All @@ -89,7 +88,7 @@ func allTests(_ helper: TestHelper, collocated: Bool = false) throws {

output.write("testing exception callback... ")
do {
let i = uncheckedCast(prx: p.ice_adapterId("dummy"), type: TestIntfPrx.self)
let i = p.ice_adapterId("dummy")

do {
_ = try i.ice_isAAsync(id: "::Test::TestIntf").wait()
Expand Down
Loading

0 comments on commit 5d5b650

Please sign in to comment.