Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New makeProxy factory function in Swift #2275

Merged
merged 3 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 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
15 changes: 6 additions & 9 deletions swift/test/Ice/ami/AllTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ func allTests(_ helper: TestHelper, collocated: Bool = false) throws {
let output = helper.getWriter()

var sref = "test:\(helper.getTestEndpoint(num: 0))"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also remove this sref variables, and write the string directly as the "proxyString:" parameter.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in this test.

var obj = try communicator.stringToProxy(sref)!
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We no longer need this intermediary ObjectPrx.

var p = try makeProxy(communicator: communicator, proxyString: sref, 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: sref, type: TestIntfControllerPrx.self)

output.write("testing async invocation...")
do {
Expand Down Expand Up @@ -61,7 +58,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 +74,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 +86,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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The various ice_ factory method already returned a proxy with the same type.

let i = p.ice_adapterId("dummy")

do {
_ = try i.ice_isAAsync(id: "::Test::TestIntf").wait()
Expand Down
56 changes: 21 additions & 35 deletions swift/test/Ice/binding/AllTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ func allTests(_ helper: TestHelper) throws {
let communicator = helper.communicator()
let output = helper.getWriter()

let com = try uncheckedCast(
prx: communicator.stringToProxy("communicator:\(helper.getTestEndpoint(num: 0))")!,
let com = try makeProxy(
communicator: communicator,
proxyString: "communicator:\(helper.getTestEndpoint(num: 0))",
type: RemoteCommunicatorPrx.self)

output.write("testing binding with single endpoint... ")
Expand All @@ -48,12 +49,8 @@ func allTests(_ helper: TestHelper) throws {

try com.deactivateObjectAdapter(adapter)

let test3 = uncheckedCast(prx: test1, type: TestIntfPrx.self)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code made no sense. uncheckedCast here just returns test1.

try test(test3.ice_getConnection() === test1.ice_getConnection())
try test(test3.ice_getConnection() === test2.ice_getConnection())

do {
try test3.ice_ping()
try test1.ice_ping()
try test(false)
} catch is Ice.ConnectFailedException {
// expected
Expand Down Expand Up @@ -318,7 +315,7 @@ func allTests(_ helper: TestHelper) throws {
try obj.ice_getConnection()!.close(.GracefullyWithWait)
}

obj = uncheckedCast(prx: obj.ice_endpointSelection(.Random), type: TestIntfPrx.self)
obj = obj.ice_endpointSelection(.Random)
try test(obj.ice_getEndpointSelection() == .Random)

names.append("Adapter21")
Expand All @@ -344,7 +341,7 @@ func allTests(_ helper: TestHelper) throws {
]

var obj = try createTestIntfPrx(adapters)
obj = uncheckedCast(prx: obj.ice_endpointSelection(.Ordered), type: TestIntfPrx.self)
obj = obj.ice_endpointSelection(.Ordered)
try test(obj.ice_getEndpointSelection() == .Ordered)
let nRetry = 3

Expand Down Expand Up @@ -423,12 +420,8 @@ func allTests(_ helper: TestHelper) throws {
do {
let adapter = try com.createObjectAdapter(name: "Adapter41", endpoints: "default")!

let test1 = try uncheckedCast(
prx: adapter.getTestIntf()!.ice_connectionCached(false),
type: TestIntfPrx.self)
let test2 = try uncheckedCast(
prx: adapter.getTestIntf()!.ice_connectionCached(false),
type: TestIntfPrx.self)
let test1 = try adapter.getTestIntf()!.ice_connectionCached(false)
let test2 = try adapter.getTestIntf()!.ice_connectionCached(false)
try test(!test1.ice_isConnectionCached())
try test(!test2.ice_isConnectionCached())
try test(test1.ice_getConnection() != nil && test2.ice_getConnection() != nil)
Expand All @@ -438,9 +431,8 @@ func allTests(_ helper: TestHelper) throws {

try com.deactivateObjectAdapter(adapter)

let test3 = uncheckedCast(prx: test1, type: TestIntfPrx.self)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This returns test1.

do {
try test(test3.ice_getConnection() === test1.ice_getConnection())
_ = try test1.ice_getConnection()
try test(false)
} catch is Ice.ConnectFailedException {
// expected
Expand All @@ -458,9 +450,7 @@ func allTests(_ helper: TestHelper) throws {
com.createObjectAdapter(name: "Adapter53", endpoints: "default")!,
]

let obj = try uncheckedCast(
prx: createTestIntfPrx(adapters).ice_connectionCached(false),
type: TestIntfPrx.self)
let obj = try createTestIntfPrx(adapters).ice_connectionCached(false)
try test(!obj.ice_isConnectionCached())

var names = ["Adapter51", "Adapter52", "Adapter53"]
Expand Down Expand Up @@ -494,9 +484,7 @@ func allTests(_ helper: TestHelper) throws {
com.createObjectAdapter(name: "AdapterAMI53", endpoints: "default")!,
]

let obj = try uncheckedCast(
prx: createTestIntfPrx(adapters).ice_connectionCached(false),
type: TestIntfPrx.self)
let obj = try createTestIntfPrx(adapters).ice_connectionCached(false)
try test(!obj.ice_isConnectionCached())

var names = ["AdapterAMI51", "AdapterAMI52", "AdapterAMI53"]
Expand Down Expand Up @@ -531,16 +519,14 @@ func allTests(_ helper: TestHelper) throws {
]

var obj = try createTestIntfPrx(adapters)
obj = uncheckedCast(
prx: obj.ice_endpointSelection(.Ordered),
type: TestIntfPrx.self)
obj = obj.ice_endpointSelection(.Ordered)
try test(obj.ice_getEndpointSelection() == .Ordered)
obj = uncheckedCast(prx: obj.ice_connectionCached(false), type: TestIntfPrx.self)
obj = obj.ice_connectionCached(false)
try test(!obj.ice_isConnectionCached())
let nRetry = 3
var i = 0
//
// Ensure that endpoints are tried in order by deactiving the adapters
bernardnormier marked this conversation as resolved.
Show resolved Hide resolved
// Ensure that endpoints are tried in order by deactivating the adapters
// one after the other.
//
while try i < nRetry && obj.getAdapterName() == "Adapter61" {
Expand Down Expand Up @@ -616,16 +602,16 @@ func allTests(_ helper: TestHelper) throws {
]

var obj = try createTestIntfPrx(adapters)
obj = uncheckedCast(prx: obj.ice_endpointSelection(.Ordered), type: TestIntfPrx.self)
obj = obj.ice_endpointSelection(.Ordered)
try test(obj.ice_getEndpointSelection() == .Ordered)
obj = uncheckedCast(prx: obj.ice_connectionCached(false), type: TestIntfPrx.self)
obj = obj.ice_connectionCached(false)
try test(!obj.ice_isConnectionCached())

let nRetry = 3
var i = 0

//
// 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.
//
while try i < nRetry && obj.getAdapterNameAsync().wait() == "AdapterAMI61" {
Expand Down Expand Up @@ -701,7 +687,7 @@ func allTests(_ helper: TestHelper) throws {
let obj = try createTestIntfPrx(adapters)
try test(obj.getAdapterName() == "Adapter71")

let testUDP = uncheckedCast(prx: obj.ice_datagram(), type: TestIntfPrx.self)
let testUDP = obj.ice_datagram()
try test(obj.ice_getConnection() !== testUDP.ice_getConnection())
do {
_ = try testUDP.getAdapterName()
Expand All @@ -723,11 +709,11 @@ func allTests(_ helper: TestHelper) throws {
try obj.ice_getConnection()!.close(.GracefullyWithWait)
}

var testSecure = uncheckedCast(prx: obj.ice_secure(true), type: TestIntfPrx.self)
var testSecure = obj.ice_secure(true)
try test(testSecure.ice_isSecure())
testSecure = uncheckedCast(prx: obj.ice_secure(false), type: TestIntfPrx.self)
testSecure = obj.ice_secure(false)
try test(!testSecure.ice_isSecure())
testSecure = uncheckedCast(prx: obj.ice_secure(true), type: TestIntfPrx.self)
testSecure = obj.ice_secure(true)
try test(testSecure.ice_isSecure())
try test(obj.ice_getConnection() !== testSecure.ice_getConnection())

Expand Down
Loading
Loading