summaryrefslogtreecommitdiff
path: root/swift/test/Ice/acm/TestI.swift
blob: e30a60938cfd282b294227578634239a674312c8 (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//
// 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 {
    class HearbeatCallbackI {
        let _semaphore: DispatchSemaphore
        var _count: Int32

        public init() {
            _count = 0
            _semaphore = DispatchSemaphore(value: 0)
        }

        func hearbeat(conn _: Ice.Connection?) {
            _count += 1
            _semaphore.signal()
        }

        func waitForCount(count: Int32) {
            while _count < count {
                _semaphore.wait()
            }
        }
    }

    let _semaphore: DispatchSemaphore
    var _hearbeatCallback: HearbeatCallbackI!

    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 = HearbeatCallbackI()
        current.con?.setHeartbeatCallback { conn in
            self._hearbeatCallback.hearbeat(conn: conn)
        }
    }

    func waitForHeartbeatCount(count: Int32, current _: Current) {
        _hearbeatCallback.waitForCount(count: count)
    }
}