SHARE
TWEET

Flip a coin endlessly v2 - StopListener

robertmarkbram Sep 14th, 2014 12 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.rmb.randomnodatabase.taskmanagement;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.PrintWriter;
  7. import java.net.ServerSocket;
  8. import java.net.Socket;
  9.  
  10. /**
  11.  * Listen for stop signal and set {@link #isActive()} to false.
  12.  *
  13.  * See: http://bit.ly/1oKSnHu
  14.  *
  15.  */
  16. public final class StopListener extends Thread {
  17.  
  18.    /** Default port to listen to. */
  19.    public static final int DEFAULT_PORT = 7;
  20.  
  21.    /** Default stop signal. */
  22.    public static final String STOP_SIGNAL = "stop signal sent";
  23.  
  24.    /** Port to listen at. */
  25.    private final int port;
  26.  
  27.    /** True until stop signal is received. */
  28.    private boolean active = true;
  29.  
  30.    /**
  31.     * Record incoming message here, if something other than STOP comes through.
  32.     */
  33.    private final StringBuilder message;
  34.  
  35.    /**
  36.     * Create listener with custom port.
  37.     *
  38.     * @param thePort
  39.     *           port to listen at
  40.     */
  41.    public StopListener(final int thePort) {
  42.       port = thePort;
  43.       message = new StringBuilder();
  44.    }
  45.  
  46.    /**
  47.     * Create listener with default port.
  48.     */
  49.    public StopListener() {
  50.       this(DEFAULT_PORT);
  51.    }
  52.  
  53.    @Override
  54.    public void run() {
  55.       try (ServerSocket serverSocket = new ServerSocket(port);
  56.             Socket clientSocket = serverSocket.accept();
  57.             PrintWriter out =
  58.                   new PrintWriter(clientSocket.getOutputStream(), true);
  59.             BufferedReader in =
  60.                   new BufferedReader(new InputStreamReader(
  61.                         clientSocket.getInputStream()));) {
  62.          String inputLine;
  63.          while ((inputLine = in.readLine()) != null) {
  64.             message.append(inputLine);
  65.             out.println("Stop signal received.");
  66.             active = false;
  67.             break;
  68.          }
  69.       } catch (IOException e) {
  70.          System.out.println("Exception caught when trying to listen on port ["
  71.                + port + "] or listening for a connection");
  72.          System.out.println(e.getMessage());
  73.       }
  74.    }
  75.  
  76.    /**
  77.     * @return true until stop signal is received. False when stop signal was
  78.     *         received.
  79.     */
  80.    public boolean isActive() {
  81.       return active;
  82.    }
  83.  
  84.    /**
  85.     * @return message delivered to this listener by client.
  86.     */
  87.    public String getMessage() {
  88.       return message.toString();
  89.    }
  90.  
  91.    /**
  92.     * Test listener.
  93.     *
  94.     * @param args
  95.     *           not used.
  96.     */
  97.    public static void main(final String[] args) {
  98.       StopListener listener = new StopListener();
  99.       System.out.println("Listening now - listener.isActive() ["
  100.             + listener.isActive() + "].");
  101.       listener.start();
  102.       try {
  103.          listener.join();
  104.       } catch (InterruptedException e) {
  105.          e.printStackTrace();
  106.       }
  107.       System.out.println("Stopped - listener.isActive() ["
  108.             + listener.isActive() + "].");
  109.    }
  110. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top