1 /* 2 * Copyright (C) 2016 The Dagger Authors. 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 17 package dagger.grpc.functional.server; 18 19 import static java.util.Collections.singletonList; 20 21 import com.google.protos.test.BaristaGrpc; 22 import com.google.protos.test.BaristaGrpc.BaristaImplBase; 23 import com.google.protos.test.CoffeeService.CoffeeRequest; 24 import com.google.protos.test.CoffeeService.CoffeeResponse; 25 import com.google.protos.test.CoffeeService.CoffeeType; 26 import dagger.grpc.server.GrpcService; 27 import io.grpc.stub.StreamObserver; 28 import java.util.List; 29 import javax.inject.Inject; 30 31 @GrpcService(grpcClass = BaristaGrpc.class) 32 class FriendlyBarista extends BaristaImplBase { 33 34 @Inject FriendlyBarista()35 FriendlyBarista() {} 36 37 @Override unaryGetCoffee( CoffeeRequest request, StreamObserver<CoffeeResponse> responseObserver)38 public void unaryGetCoffee( 39 CoffeeRequest request, StreamObserver<CoffeeResponse> responseObserver) { 40 responseObserver.onNext(response("Here you go!", request.getTypeList())); 41 responseObserver.onCompleted(); 42 } 43 44 @Override clientStreamingGetCoffee( final StreamObserver<CoffeeResponse> responseObserver)45 public StreamObserver<CoffeeRequest> clientStreamingGetCoffee( 46 final StreamObserver<CoffeeResponse> responseObserver) { 47 return new StreamObserver<CoffeeRequest>() { 48 49 private final CoffeeResponse.Builder response = CoffeeResponse.newBuilder(); 50 51 @Override 52 public void onNext(CoffeeRequest value) { 53 response.addAllCup(value.getTypeList()); 54 } 55 56 @Override 57 public void onError(Throwable t) {} 58 59 @Override 60 public void onCompleted() { 61 response.setMessage("All yours!"); 62 responseObserver.onNext(response.build()); 63 responseObserver.onCompleted(); 64 } 65 }; 66 } 67 68 @Override serverStreamingGetCoffee( CoffeeRequest request, StreamObserver<CoffeeResponse> responseObserver)69 public void serverStreamingGetCoffee( 70 CoffeeRequest request, StreamObserver<CoffeeResponse> responseObserver) { 71 for (CoffeeType type : request.getTypeList()) { 72 responseObserver.onNext(response("Here's a " + type, singletonList(type))); 73 } 74 responseObserver.onCompleted(); 75 } 76 77 @Override bidiStreamingGetCoffee( final StreamObserver<CoffeeResponse> responseObserver)78 public StreamObserver<CoffeeRequest> bidiStreamingGetCoffee( 79 final StreamObserver<CoffeeResponse> responseObserver) { 80 return new StreamObserver<CoffeeRequest>() { 81 82 private int responses; 83 84 @Override 85 public void onNext(CoffeeRequest value) { 86 responseObserver.onNext(response("Enjoy!", value.getTypeList())); 87 if (responses++ > 10) { 88 responseObserver.onNext(CoffeeResponse.newBuilder().setMessage("We're done.").build()); 89 responseObserver.onCompleted(); 90 } 91 } 92 93 @Override 94 public void onError(Throwable t) {} 95 96 @Override 97 public void onCompleted() { 98 responseObserver.onCompleted(); 99 } 100 }; 101 } 102 103 private CoffeeResponse response(String message, List<CoffeeType> types) { 104 return CoffeeResponse.newBuilder().addAllCup(types).setMessage(message).build(); 105 } 106 } 107