/** * Returns the port that the server is binded to. * * @return An int represent the port number. */ public int getPort() { Preconditions.checkState(isRunning()); return port; }
@Override protected void run() throws Exception { LOG.info("Running commandPortService at localhost:" + port); serve(); }
@Test public void testCommandPortServer() throws Exception { IncrementCommandHandler handler = new IncrementCommandHandler(); final CommandPortService server = CommandPortService.builder("test") .addCommandHandler("increment", "Increments a counter", handler) .build(); Futures.addCallback(server.start(), new FutureCallback<Service.State>() { @Override public void onSuccess(Service.State result) { Socket clientSocket = new Socket("localhost", server.getPort()); try { BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream(), "UTF-8")); server.stopAndWait(); Assert.assertEquals(Service.State.TERMINATED, server.state());
/** * Builds the {@link CommandPortService}. * * @return A {@link CommandPortService}. */ public CommandPortService build() { if (!hasHelp) { final String helpString = helpStringBuilder.toString(); handlerBuilder.put("help", new CommandHandler() { @Override public void handle(BufferedWriter respondWriter) throws IOException { respondWriter.write(helpString); respondWriter.newLine(); } }); } return new CommandPortService(port, handlerBuilder.build()); } }
/** * Builds the {@link CommandPortService}. * * @return A {@link CommandPortService}. */ public CommandPortService build() { if (!hasHelp) { final String helpString = helpStringBuilder.toString(); handlerBuilder.put("help", new CommandHandler() { @Override public void handle(BufferedWriter respondWriter) throws IOException { respondWriter.write(helpString); respondWriter.newLine(); } }); } return new CommandPortService(port, handlerBuilder.build()); } }
/** * Returns the port that the server is binded to. * * @return An int represent the port number. */ public int getPort() { Preconditions.checkState(isRunning()); return port; }
@Override protected void run() throws Exception { LOG.info("Running commandPortService at localhost:" + port); serve(); }
while (isRunning()) { try { Socket socket = serverSocket.accept();
while (isRunning()) { try { Socket socket = serverSocket.accept();