@Override
 public void stop() throws Exception {
   cnxnFactory.shutdown();
   cnxnFactory.join();
   if (zkServer.isRunning()) {
     zkServer.shutdown();
   }
 }
  @Override
  public void start() throws Exception {
    ServerConfig config = new ServerConfig();
    config.parse(new String[] {"2181", "zk"});

    zkServer = new ZooKeeperServer();
    zkServer.setTxnLogFactory(
        new FileTxnSnapLog(new File(config.getDataLogDir()), new File(config.getDataDir())));
    zkServer.setTickTime(config.getTickTime());
    zkServer.setMinSessionTimeout(config.getMinSessionTimeout());
    zkServer.setMaxSessionTimeout(config.getMaxSessionTimeout());
    cnxnFactory = ServerCnxnFactory.createFactory();
    cnxnFactory.configure(config.getClientPortAddress(), config.getMaxClientCnxns());
    cnxnFactory.startup(zkServer);
  }
 /**
  * This method instantiates a new server. Starting of the server instance has been moved to a
  * separate method {@link ClientBase#startServerInstance(File, ServerCnxnFactory, String)}.
  * Because any exception on starting the server would leave the server running and the caller
  * would not be able to shutdown the instance. This may affect other test cases.
  *
  * @return newly created server instance
  * @see <a href="https://issues.apache.org/jira/browse/ZOOKEEPER-1852">ZOOKEEPER-1852</a> for more
  *     information.
  */
 public static ServerCnxnFactory createNewServerInstance(
     ServerCnxnFactory factory, String hostPort, int maxCnxns)
     throws IOException, InterruptedException {
   final int port = getPort(hostPort);
   LOG.info("CREATING server instance 127.0.0.1:{}", port);
   if (factory == null) {
     factory = ServerCnxnFactory.createFactory(port, maxCnxns);
   }
   return factory;
 }
 /** Starting the given server instance */
 public static void startServerInstance(File dataDir, ServerCnxnFactory factory, String hostPort)
     throws IOException, InterruptedException {
   final int port = getPort(hostPort);
   LOG.info("STARTING server instance 127.0.0.1:{}", port);
   ZooKeeperServer zks = new ZooKeeperServer(dataDir, dataDir, 3000);
   factory.startup(zks);
   Assert.assertTrue(
       "waiting for server up",
       ClientBase.waitForServerUp("127.0.0.1:" + port, CONNECTION_TIMEOUT));
 }
  @Override
  public synchronized void start(final StartContext context) throws StartException {
    File dir = new File(dataDir).getAbsoluteFile();

    try {
      zooKeeperServer = new ZooKeeperServer(dir, dir, (int) tickTime);

      ServerCnxnFactory serverCnxnFactory =
          ServerCnxnFactory.createFactory(binding.getValue().getSocketAddress(), 5);
      zooKeeperServer.setServerCnxnFactory(serverCnxnFactory);

      context.asynchronous();
      executor.getValue().execute(new ZooKeeperServerRunner(zooKeeperServer, context));
    } catch (IOException e) {
      context.failed(new StartException(e));
    }
  }
  static void shutdownServerInstance(ServerCnxnFactory factory, String hostPort) {
    if (factory != null) {
      ZKDatabase zkDb = null;
      {
        ZooKeeperServer zs = getServer(factory);
        if (zs != null) {
          zkDb = zs.getZKDatabase();
        }
      }
      factory.shutdown();
      try {
        if (zkDb != null) {
          zkDb.close();
        }
      } catch (IOException ie) {
        LOG.warn("Error closing logs ", ie);
      }
      final int PORT = getPort(hostPort);

      Assert.assertTrue(
          "waiting for server down",
          ClientBase.waitForServerDown("127.0.0.1:" + PORT, CONNECTION_TIMEOUT));
    }
  }
  /** Test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2299 */
  @Test
  public void testClientAddress() throws Exception {
    QuorumPeer quorumPeer = new QuorumPeer();
    LocalPeerBean remotePeerBean = new LocalPeerBean(quorumPeer);

    /** Case 1: When cnxnFactory is null */
    String result = remotePeerBean.getClientAddress();
    assertNotNull(result);
    assertEquals(0, result.length());

    /** Case 2: When only client port is configured */
    ServerCnxnFactory cnxnFactory = ServerCnxnFactory.createFactory();
    int clientPort = PortAssignment.unique();
    InetSocketAddress address = new InetSocketAddress(clientPort);
    cnxnFactory.configure(address, 5, false);
    quorumPeer.setCnxnFactory(cnxnFactory);

    result = remotePeerBean.getClientAddress();
    String ipv4 = "0.0.0.0:" + clientPort;
    String ipv6 = "0:0:0:0:0:0:0:0:" + clientPort;
    assertTrue(result.equals(ipv4) || result.equals(ipv6));
    // cleanup
    cnxnFactory.shutdown();

    /** Case 3: When both client port and client address is configured */
    clientPort = PortAssignment.unique();
    InetAddress clientIP = InetAddress.getLoopbackAddress();
    address = new InetSocketAddress(clientIP, clientPort);
    cnxnFactory = ServerCnxnFactory.createFactory();
    cnxnFactory.configure(address, 5, false);
    quorumPeer.setCnxnFactory(cnxnFactory);

    result = remotePeerBean.getClientAddress();
    String expectedResult = clientIP.getHostAddress() + ":" + clientPort;
    assertEquals(expectedResult, result);
    // cleanup
    cnxnFactory.shutdown();
  }