Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.rmb.randomnodatabase.taskmanagement;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.net.UnknownHostException;
- /**
- * Send stop signal to {@link StopListener} via port
- * {@link StopListener#DEFAULT_PORT}.
- *
- * See: http://bit.ly/1oKSnHu
- *
- * @author RobertMarkBram
- */
- public final class StopSender {
- /** Host to use. */
- public static final String HOST = "localhost";
- /** Create StopSender. */
- public StopSender() {
- }
- /**
- * Open socket and send default stop signal.
- */
- public void sendStopSignal() {
- sendStopSignal(StopListener.STOP_SIGNAL);
- }
- /**
- * Open socket and send stop signal.
- *
- * @param message
- * whatever string you want to send to {@link StopListener}, may be
- * used for communicating error messages.
- */
- public void sendStopSignal(final String message) {
- try (Socket socket = new Socket(HOST, StopListener.DEFAULT_PORT);
- PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
- BufferedReader in =
- new BufferedReader(new InputStreamReader(
- socket.getInputStream()));) {
- out.println(message);
- final String received = in.readLine();
- System.out.println(received);
- } catch (UnknownHostException e) {
- System.err.println("Don't know about host [" + HOST + "].");
- System.exit(1);
- } catch (IOException e) {
- System.err.println("Couldn't get I/O for the connection to [" + HOST
- + "].");
- System.exit(1);
- }
- }
- /**
- * @param args
- * - 0: stop message. May be null.
- */
- public static void main(final String[] args) {
- StopSender sender = new StopSender();
- if (args.length > 0) {
- sender.sendStopSignal(args[0]);
- } else {
- sender.sendStopSignal();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement