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
|
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
import Foundation
import Ice
class RemoteCommunicatorI: RemoteCommunicator {
func createObjectAdapter(acmTimeout: Int32,
close: Int32,
heartbeat: Int32,
current: Ice.Current) throws -> RemoteObjectAdapterPrx? {
let communicator = current.adapter!.getCommunicator()
let properties = communicator.getProperties()
let defaultProtocol = properties.getPropertyWithDefault(key: "Ice.Default.Protocol", value: "tcp")
let defaultHost = properties.getPropertyWithDefault(key: "Ice.Default.Host", value: "127.0.0.1")
let name = UUID().uuidString
if acmTimeout >= 0 {
properties.setProperty(key: "\(name).ACM.Timeout", value: "\(acmTimeout)")
}
if close >= 0 {
properties.setProperty(key: "\(name).ACM.Close", value: "\(close)")
}
if heartbeat >= 0 {
properties.setProperty(key: "\(name).ACM.Heartbeat", value: "\(heartbeat)")
}
properties.setProperty(key: "\(name).ThreadPool.Size", value: "2")
let adapter = try communicator.createObjectAdapterWithEndpoints(
name: name,
endpoints: "\(defaultProtocol) -h \"\(defaultHost)\""
)
return try uncheckedCast(
prx: current.adapter!.addWithUUID(RemoteObjectAdapterDisp(RemoteObjectAdapterI(adapter: adapter))),
type: RemoteObjectAdapterPrx.self
)
}
func shutdown(current: Ice.Current) throws {
current.adapter!.getCommunicator().shutdown()
}
}
class RemoteObjectAdapterI: RemoteObjectAdapter {
var _adapter: Ice.ObjectAdapter
var _testIntf: TestIntfPrx
init(adapter: Ice.ObjectAdapter) throws {
_adapter = adapter
_testIntf = try uncheckedCast(prx: adapter.add(servant: TestIntfDisp(TestI()),
id: Ice.stringToIdentity("test")),
type: TestIntfPrx.self)
try _adapter.activate()
}
func getTestIntf(current _: Current) -> TestIntfPrx? {
return _testIntf
}
func activate(current _: Current) throws {
try _adapter.activate()
}
func hold(current _: Current) {
_adapter.hold()
}
func deactivate(current _: Current) {
_adapter.destroy()
}
}
class TestI: TestIntf {
let _semaphore: DispatchSemaphore
var _hearbeatCallback: DispatchSemaphore!
public init() {
_semaphore = DispatchSemaphore(value: 0)
}
func sleep(seconds: Int32, current _: Current) {
_ = _semaphore.wait(timeout: .now() + Double(seconds))
}
func sleepAndHold(seconds: Int32, current: Current) {
current.adapter!.hold()
_ = _semaphore.wait(timeout: .now() + Double(seconds))
}
func interruptSleep(current _: Current) {
_semaphore.signal()
}
func startHeartbeatCount(current: Current) {
_hearbeatCallback = DispatchSemaphore(value: 0)
current.con?.setHeartbeatCallback { _ in
self._hearbeatCallback.signal()
}
}
func waitForHeartbeatCount(count: Int32, current _: Current) {
for _ in 0..<count {
self._hearbeatCallback.wait()
}
}
}
|