/src/grpc-swift/FuzzTesting/Sources/EchoImplementation/EchoProvider.swift
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2018, gRPC Authors All rights reserved. |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | * you may not use this file except in compliance with the License. |
6 | | * You may obtain a copy of the License at |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | import EchoModel |
17 | | import GRPC |
18 | | import NIOCore |
19 | | import SwiftProtobuf |
20 | | |
21 | | public class EchoProvider: Echo_EchoProvider { |
22 | | public let interceptors: Echo_EchoServerInterceptorFactoryProtocol? |
23 | | |
24 | 92.1k | public init(interceptors: Echo_EchoServerInterceptorFactoryProtocol? = nil) { |
25 | 92.1k | self.interceptors = interceptors |
26 | 92.1k | } |
27 | | |
28 | | public func get( |
29 | | request: Echo_EchoRequest, |
30 | | context: StatusOnlyCallContext |
31 | 14.0k | ) -> EventLoopFuture<Echo_EchoResponse> { |
32 | 14.0k | let response = Echo_EchoResponse.with { |
33 | 14.0k | $0.text = "Swift echo get: " + request.text |
34 | 14.0k | } |
35 | 14.0k | return context.eventLoop.makeSucceededFuture(response) |
36 | 14.0k | } |
37 | | |
38 | | public func expand( |
39 | | request: Echo_EchoRequest, |
40 | | context: StreamingResponseCallContext<Echo_EchoResponse> |
41 | 6.32k | ) -> EventLoopFuture<GRPCStatus> { |
42 | 121k | let responses = request.text.components(separatedBy: " ").lazy.enumerated().map { i, part in |
43 | 121k | Echo_EchoResponse.with { |
44 | 121k | $0.text = "Swift echo expand (\(i)): \(part)" |
45 | 121k | } |
46 | 121k | } |
47 | 6.32k | |
48 | 6.32k | context.sendResponses(responses, promise: nil) |
49 | 6.32k | return context.eventLoop.makeSucceededFuture(.ok) |
50 | 6.32k | } |
51 | | |
52 | | public func collect( |
53 | | context: UnaryResponseCallContext<Echo_EchoResponse> |
54 | 68.3k | ) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> { |
55 | 68.3k | var parts: [String] = [] |
56 | 185k | return context.eventLoop.makeSucceededFuture({ event in |
57 | 185k | switch event { |
58 | 185k | case let .message(message): |
59 | 121k | parts.append(message.text) |
60 | 185k | |
61 | 185k | case .end: |
62 | 64.4k | let response = Echo_EchoResponse.with { |
63 | 64.4k | $0.text = "Swift echo collect: " + parts.joined(separator: " ") |
64 | 64.4k | } |
65 | 64.4k | context.responsePromise.succeed(response) |
66 | 185k | } |
67 | 185k | }) |
68 | 68.3k | } |
69 | | |
70 | | public func update( |
71 | | context: StreamingResponseCallContext<Echo_EchoResponse> |
72 | 41.9k | ) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> { |
73 | 41.9k | var count = 0 |
74 | 1.36M | return context.eventLoop.makeSucceededFuture({ event in |
75 | 1.36M | switch event { |
76 | 1.36M | case let .message(message): |
77 | 1.36M | let response = Echo_EchoResponse.with { |
78 | 1.36M | $0.text = "Swift echo update (\(count)): \(message.text)" |
79 | 1.36M | } |
80 | 1.36M | count += 1 |
81 | 1.36M | context.sendResponse(response, promise: nil) |
82 | 1.36M | |
83 | 1.36M | case .end: |
84 | 8.42k | context.statusPromise.succeed(.ok) |
85 | 1.36M | } |
86 | 1.36M | }) |
87 | 41.9k | } |
88 | | } |