summaryrefslogtreecommitdiff
path: root/swift/test/Ice/info/TestI.swift
blob: 624b5271e980ee7fc6c4ea672db2cb5fa79d6fb0 (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
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//

import Ice

func getIPEndpointInfo(_ info: Ice.EndpointInfo) -> Ice.IPEndpointInfo? {
    var curr: Ice.EndpointInfo? = info
    while curr != nil {
        if curr is Ice.IPEndpointInfo {
            return curr as? Ice.IPEndpointInfo
        }
        curr = curr?.underlying
    }
    return nil
}

func getIPConnectionInfo(_ info: Ice.ConnectionInfo) -> Ice.IPConnectionInfo? {
    var curr: Ice.ConnectionInfo? = info
    while curr != nil {
        if curr is Ice.IPConnectionInfo {
            return curr as? Ice.IPConnectionInfo
        }
        curr = curr?.underlying
    }
    return nil
}

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

    func getEndpointInfoAsContext(current: Ice.Current) throws -> Ice.Context {
        var ctx = Ice.Context()
        let info = current.con!.getEndpoint().getInfo()!
        ctx["timeout"] = "\(info.timeout)"
        ctx["compress"] = info.compress ? "true" : "false"
        ctx["datagram"] = info.datagram() ? "true" : "false"
        ctx["secure"] = info.datagram() ? "true" : "false"
        ctx["type"] = "\(info.type())"

        let ipinfo = getIPEndpointInfo(info)!
        ctx["host"] = ipinfo.host
        ctx["port"] = "\(ipinfo.port)"

        if let udpinfo = ipinfo as? Ice.UDPEndpointInfo {
            ctx["mcastInterface"] = udpinfo.mcastInterface
            ctx["mcastTtl"] = "\(udpinfo.mcastTtl)"
        }
        return ctx
    }

    func getConnectionInfoAsContext(current: Ice.Current) throws -> Ice.Context {
        var ctx = Ice.Context()
        let info = try current.con!.getInfo()
        ctx["adapterName"] = info.adapterName
        ctx["incoming"] = info.incoming ? "true" : "false"

        let ipinfo = getIPConnectionInfo(info)!
        ctx["localAddress"] = ipinfo.localAddress
        ctx["localPort"] = "\(ipinfo.localPort)"
        ctx["remoteAddress"] = ipinfo.remoteAddress
        ctx["remotePort"] = "\(ipinfo.remotePort)"

        if let wsinfo = info as? WSConnectionInfo {
            for (key, value) in wsinfo.headers {
                ctx["ws.\(key)"] = value
            }
        }
        return ctx
    }
}