summaryrefslogtreecommitdiff
path: root/swift/test/Ice/admin/TestI.swift
blob: 545d30e50898dc95f28d076279e44be402a1488e (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
130
131
132
133
134
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//

import Foundation
import Ice
import TestCommon

class TestFacetI: TestFacet {
    func op(current _: Ice.Current) {}
}

class RemoteCommunicatorI: RemoteCommunicator {
    var _communicator: Ice.Communicator
    var _changes: [String: String] = [:]
    var _lock = os_unfair_lock()

    init(communicator: Ice.Communicator) {
        _communicator = communicator
    }

    func getAdmin(current _: Ice.Current) throws -> Ice.ObjectPrx? {
        return try _communicator.getAdmin()
    }

    func getChanges(current _: Ice.Current) throws -> [String: String] {
        return withLock(&_lock) {
            _changes
        }
    }

    func print(message: String, current _: Ice.Current) throws {
        _communicator.getLogger().print(message)
    }

    func trace(category: String, message: String, current _: Ice.Current) throws {
        _communicator.getLogger().trace(category: category, message: message)
    }

    func warning(message: String, current _: Ice.Current) throws {
        _communicator.getLogger().warning(message)
    }

    func error(message: String, current _: Ice.Current) throws {
        _communicator.getLogger().error(message)
    }

    func shutdown(current _: Ice.Current) throws {
        _communicator.shutdown()
    }

    func waitForShutdown(current _: Ice.Current) throws {
        //
        // Note that we are executing in a thread of the *main* communicator,
        // not the one that is being shut down.
        //
        _communicator.waitForShutdown()
    }

    func destroy(current _: Ice.Current) throws {
        _communicator.destroy()
    }

    func updated(changes: [String: String]) {
        withLock(&_lock) {
            _changes = changes
        }
    }
}

class NullLogger: Ice.Logger {
    func print(_: String) {}

    func trace(category _: String, message _: String) {}

    func warning(_: String) {}

    func error(_: String) {}

    func getPrefix() -> String {
        return "NullLogger"
    }

    func cloneWithPrefix(_: String) -> Logger {
        return self
    }
}

class RemoteCommunicatorFactoryI: RemoteCommunicatorFactory {
    func createCommunicator(props: [String: String], current: Ice.Current) throws -> RemoteCommunicatorPrx? {
        //
        // Prepare the property set using the given properties.
        //
        let properties = Ice.createProperties()
        for (key, value) in props {
            properties.setProperty(key: key, value: value)
        }

        var initData = Ice.InitializationData()
        initData.properties = properties
        if properties.getPropertyAsInt("NullLogger") > 0 {
            initData.logger = NullLogger()
        }

        //
        // Initialize a new communicator.
        //
        let communicator = try Ice.initialize(initData)

        //
        // Install a custom admin facet.
        //
        try communicator.addAdminFacet(servant: TestFacetDisp(TestFacetI()), facet: "TestFacet")

        //
        // The RemoteCommunicator servant also implements PropertiesAdminUpdateCallback.
        // Set the callback on the admin facet.
        //
        let servant = RemoteCommunicatorI(communicator: communicator)

        if let propDisp = communicator.findAdminFacet("Properties") as? PropertiesAdminDisp,
            let propFacet = propDisp.servant as? NativePropertiesAdmin {
            _ = propFacet.addUpdateCallback { changes in
                servant.updated(changes: changes)
            }
        }
        return try uncheckedCast(prx: current.adapter!.addWithUUID(RemoteCommunicatorDisp(servant)),
                                 type: RemoteCommunicatorPrx.self)
    }

    func shutdown(current: Ice.Current) throws {
        current.adapter!.getCommunicator().shutdown()
    }
}