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

import Ice
import PromiseKit
import TestCommon

class BlobjectI: Ice.Blobject {
    func ice_invoke(inEncaps: Data, current: Ice.Current) throws -> (ok: Bool, outParams: Data) {
        let communicator = current.adapter!.getCommunicator()
        let inS = Ice.InputStream(communicator: communicator, bytes: inEncaps)
        _ = try inS.startEncapsulation()
        let outS = Ice.OutputStream(communicator: communicator)
        outS.startEncapsulation()
        if current.operation == "opOneway" {
            return (true, Data())
        } else if current.operation == "opString" {
            let s: String = try inS.read()
            outS.write(s)
            outS.write(s)
            outS.endEncapsulation()
            return (true, outS.finished())
        } else if current.operation == "opException" {
            if current.ctx["raise"] != nil {
                throw MyException()
            }
            let ex = MyException()
            outS.write(ex)
            outS.endEncapsulation()
            return (false, outS.finished())
        } else if current.operation == "shutdown" {
            communicator.shutdown()
            return (true, Data())
        } else if current.operation == "ice_isA" {
            let s: String = try inS.read()
            if s == "::Test::MyClass" {
                outS.write(true)
            } else {
                outS.write(false)
            }
            outS.endEncapsulation()
            return (true, outS.finished())
        } else {
            throw Ice.OperationNotExistException(id: current.id, facet: current.facet, operation: current.operation)
        }
    }
}

class BlobjectAsyncI: Ice.BlobjectAsync {
    func ice_invokeAsync(inEncaps: Data, current: Current) -> Promise<(ok: Bool, outParams: Data)> {
        do {
            let communicator = current.adapter!.getCommunicator()
            let inS = Ice.InputStream(communicator: communicator, bytes: inEncaps)
            _ = try inS.startEncapsulation()
            let outS = Ice.OutputStream(communicator: communicator)
            outS.startEncapsulation()
            if current.operation == "opOneway" {
                return Promise.value((true, Data()))
            } else if current.operation == "opString" {
                let s: String = try inS.read()
                outS.write(s)
                outS.write(s)
                outS.endEncapsulation()
                return Promise.value((true, outS.finished()))
            } else if current.operation == "opException" {
                let ex = MyException()
                outS.write(ex)
                outS.endEncapsulation()
                return Promise.value((false, outS.finished()))
            } else if current.operation == "shutdown" {
                communicator.shutdown()
                return Promise.value((false, Data()))
            } else if current.operation == "ice_isA" {
                let s: String = try inS.read()
                if s == "::Test::MyClass" {
                    outS.write(true)
                } else {
                    outS.write(false)
                }
                outS.endEncapsulation()
                return Promise.value((true, outS.finished()))
            } else {
                throw Ice.OperationNotExistException(id: current.id,
                                                     facet: current.facet,
                                                     operation: current.operation)
            }
        } catch {
            return Promise<(ok: Bool, outParams: Data)> { seal in
                seal.reject(error)
            }
        }
    }
}