Server { public static void bind(ServerSocket socket, InetSocketAddress address, int backlog) throws IOException { bind(socket, address, backlog, null, null); } protected  Server(String bindAddress, int port,
                  Class<? extends Writable> paramClass, int handlerCount, 
                  Configuration conf); protected  Server(String bindAddress, int port,
      Class<? extends Writable> rpcRequestClass, int handlerCount,
      int numReaders, int queueSizePerHandler, Configuration conf,
      String serverName, SecretManager<? extends TokenIdentifier> secretManager); @SuppressWarnings("unchecked") protected  Server(String bindAddress, int port,
      Class<? extends Writable> rpcRequestClass, int handlerCount,
      int numReaders, int queueSizePerHandler, Configuration conf,
      String serverName, SecretManager<? extends TokenIdentifier> secretManager,
      String portRangeConfig); void addTerseExceptions(Class<?>... exceptionClass); void addSuppressedLoggingExceptions(Class<?>... exceptionClass); void setAlignmentContext(AlignmentContext alignmentContext); static void registerProtocolEngine(RPC.RpcKind rpcKind, 
          Class<? extends Writable> rpcRequestWrapperClass,
          RpcInvoker rpcInvoker); Class<? extends Writable> getRpcRequestWrapper(
      RpcKindProto rpcKind); static RpcInvoker getRpcInvoker(RPC.RpcKind rpcKind); static Server get(); @VisibleForTesting static ThreadLocal<Call> getCurCall(); static int getCallId(); static int getCallRetryCount(); static InetAddress getRemoteIp(); static String getAuxiliaryPortEstablishedQOP(); static byte[] getClientId(); static String getRemoteAddress(); static UserGroupInformation getRemoteUser(); static String getProtocol(); static boolean isRpcInvocation(); static int getPriorityLevel(); static void bind(ServerSocket socket, InetSocketAddress address, 
                          int backlog); static void bind(ServerSocket socket, InetSocketAddress address, 
      int backlog, Configuration conf, String rangeConf); @VisibleForTesting RpcMetrics getRpcMetrics(); @VisibleForTesting RpcDetailedMetrics getRpcDetailedMetrics(); void refreshServiceAcl(Configuration conf, PolicyProvider provider); @Private void refreshServiceAclWithLoadedConfiguration(Configuration conf,
      PolicyProvider provider); @InterfaceAudience.LimitedPrivate({"HDFS", "MapReduce"}) ServiceAuthorizationManager getServiceAuthorizationManager(); synchronized void refreshCallQueue(Configuration conf); void queueCall(Call call); synchronized void addAuxiliaryListener(int auxiliaryPort); void setSocketSendBufSize(int size); void setTracer(Tracer t); synchronized void start(); synchronized void stop(); synchronized void join(); synchronized InetSocketAddress getListenerAddress(); synchronized Set<InetSocketAddress> getAuxiliaryListenerAddresses(); @Deprecated Writable call(Writable param, long receiveTime); abstract Writable call(RPC.RpcKind rpcKind, String protocol,
      Writable param, long receiveTime); int getPort(); int getNumOpenConnections(); String getNumOpenConnectionsPerUser(); long getNumDroppedConnections(); int getCallQueueLen(); boolean isClientBackoffEnabled(); void setClientBackoffEnabled(boolean value); int getMaxQueueSize(); int getNumReaders(); String getServerName(); static final Logger LOG; static final Logger AUDITLOG; }
@Test public void testBind() throws Exception { Configuration conf = new Configuration(); ServerSocket socket = new ServerSocket(); InetSocketAddress address = new InetSocketAddress("0.0.0.0",0); socket.bind(address); try { int min = socket.getLocalPort(); int max = min + 100; conf.set("TestRange", min+"-"+max); ServerSocket socket2 = new ServerSocket(); InetSocketAddress address2 = new InetSocketAddress("0.0.0.0", 0); Server.bind(socket2, address2, 10, conf, "TestRange"); try { assertTrue(socket2.isBound()); assertTrue(socket2.getLocalPort() > min); assertTrue(socket2.getLocalPort() <= max); } finally { socket2.close(); } } finally { socket.close(); } }
@Test public void testBindSimple() throws Exception { ServerSocket socket = new ServerSocket(); InetSocketAddress address = new InetSocketAddress("0.0.0.0",0); Server.bind(socket, address, 10); try { assertTrue(socket.isBound()); } finally { socket.close(); } }
@Test public void testEmptyConfig() throws Exception { Configuration conf = new Configuration(); conf.set("TestRange", ""); ServerSocket socket = new ServerSocket(); InetSocketAddress address = new InetSocketAddress("0.0.0.0", 0); try { Server.bind(socket, address, 10, conf, "TestRange"); assertTrue(socket.isBound()); } finally { socket.close(); } }
@Test public void testBindError() throws Exception { Configuration conf = new Configuration(); ServerSocket socket = new ServerSocket(); InetSocketAddress address = new InetSocketAddress("0.0.0.0",0); socket.bind(address); try { int min = socket.getLocalPort(); conf.set("TestRange", min+"-"+min); ServerSocket socket2 = new ServerSocket(); InetSocketAddress address2 = new InetSocketAddress("0.0.0.0", 0); boolean caught = false; try { Server.bind(socket2, address2, 10, conf, "TestRange"); } catch (BindException e) { caught = true; } finally { socket2.close(); } assertTrue("Failed to catch the expected bind exception",caught); } finally { socket.close(); } }