1 /* 2 * Copyright 2018 The gRPC 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 io.grpc.netty; 18 19 import com.google.common.base.Preconditions; 20 import com.google.common.collect.ImmutableMap; 21 import io.grpc.InternalChannelz.TcpInfo; 22 import io.netty.channel.Channel; 23 import java.util.Map; 24 import javax.annotation.Nullable; 25 26 /** 27 * An class for getting low level socket info. 28 */ 29 final class NettySocketSupport { 30 private static volatile Helper instance = new NettySocketHelperImpl(); 31 32 interface Helper { 33 /** 34 * Returns the info on the socket if possible. Returns null if the info can not be discovered. 35 */ 36 @Nullable getNativeSocketOptions(Channel ch)37 NativeSocketOptions getNativeSocketOptions(Channel ch); 38 } 39 40 /** 41 * A TcpInfo and additional other info that will be turned into channelz socket options. 42 */ 43 public static class NativeSocketOptions { 44 @Nullable 45 public final TcpInfo tcpInfo; 46 public final ImmutableMap<String, String> otherInfo; 47 48 /** Creates an instance. */ NativeSocketOptions( TcpInfo tcpInfo, Map<String, String> otherInfo)49 public NativeSocketOptions( 50 TcpInfo tcpInfo, 51 Map<String, String> otherInfo) { 52 Preconditions.checkNotNull(otherInfo); 53 this.tcpInfo = tcpInfo; 54 this.otherInfo = ImmutableMap.copyOf(otherInfo); 55 } 56 } 57 getNativeSocketOptions(Channel ch)58 public static NativeSocketOptions getNativeSocketOptions(Channel ch) { 59 return instance.getNativeSocketOptions(ch); 60 } 61 setHelper(Helper helper)62 static void setHelper(Helper helper) { 63 instance = Preconditions.checkNotNull(helper); 64 } 65 66 private static final class NettySocketHelperImpl implements Helper { 67 @Override getNativeSocketOptions(Channel ch)68 public NativeSocketOptions getNativeSocketOptions(Channel ch) { 69 // TODO(zpencer): if netty-epoll, use reflection to call EpollSocketChannel.tcpInfo() 70 // And/or if some other low level socket support library is available, call it now. 71 return null; 72 } 73 } 74 } 75