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.server; 18 19 import dagger.Module; 20 import dagger.Provides; 21 import io.grpc.Server; 22 import io.grpc.ServerBuilder; 23 import io.grpc.netty.NettyServerBuilder; 24 import java.net.InetSocketAddress; 25 import java.net.SocketAddress; 26 import javax.inject.Singleton; 27 28 /** 29 * Installing this module into a {@link Singleton @Singleton} component means the component can 30 * provide a {@linkplain NettyServerBuilder Netty}-based {@link Server}. 31 */ 32 @Module(includes = ServerModule.class) 33 public final class NettyServerModule { 34 35 private final SocketAddress socketAddress; 36 NettyServerModule(SocketAddress socketAddress)37 private NettyServerModule(SocketAddress socketAddress) { 38 this.socketAddress = socketAddress; 39 } 40 41 /** 42 * A module that binds to {@code port} on the wildcard address. 43 */ bindingToPort(int port)44 public static NettyServerModule bindingToPort(int port) { 45 return new NettyServerModule(new InetSocketAddress(port)); 46 } 47 48 /** 49 * A module that binds to {@code socketAddress}. 50 */ bindingTo(SocketAddress socketAddress)51 public static NettyServerModule bindingTo(SocketAddress socketAddress) { 52 return new NettyServerModule(socketAddress); 53 } 54 55 @Provides serverBuilder()56 ServerBuilder<?> serverBuilder() { 57 return NettyServerBuilder.forAddress(socketAddress); 58 } 59 } 60