Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.78 KB | None | 0 0
  1. package de.cornelius.may.telnet;
  2.  
  3. import de.cornelius.may.Bot;
  4. import lombok.Getter;
  5. import org.jetbrains.annotations.NotNull;
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.IOException;
  9. import java.io.InputStreamReader;
  10. import java.io.PrintWriter;
  11. import java.net.Socket;
  12. import java.util.Objects;
  13. import java.util.concurrent.*;
  14.  
  15. public class Telnet {
  16.  
  17.     @Getter
  18.     private String host;
  19.  
  20.     @Getter
  21.     private int port;
  22.  
  23.     @Getter
  24.     private String confirmationString;
  25.  
  26.     private boolean connected;
  27.     private Socket socket;
  28.     private PrintWriter out;
  29.     private BufferedReader in;
  30.  
  31.     public Telnet(String host, int port, String confirmationString) throws IOException {
  32.         this.host = host;
  33.         this.port = port;
  34.         this.confirmationString = confirmationString;
  35.         socket = new Socket(host, port);
  36.         out = new PrintWriter(socket.getOutputStream(), true);
  37.         in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  38.         connected = true;
  39.         Bot.getLogManager().info("Connected telnet: \n");
  40.         while(in.ready()) {
  41.             String line = in.readLine();
  42.             if(line.equals("")) continue;
  43.             Bot.getLogManager().info(line);
  44.         }
  45.     }
  46.  
  47.     public synchronized void close() throws IOException {
  48.         if(!connected) throw new IllegalArgumentException("Not connected!");
  49.         connected = false;
  50.         out.close();
  51.         in.close();
  52.         socket.close();
  53.     }
  54.  
  55.     public synchronized String sendCommand(String command) {
  56.         out.println(command);
  57.         try {
  58.             Future<Object> response = Bot.getPool().submit(getResponse);
  59.             return response.get(1, TimeUnit.MINUTES).toString();
  60.         } catch (InterruptedException | ExecutionException | TimeoutException ex) {
  61.             Bot.getLogManager().error("Error while getting response for command '" + command + "'", ex);
  62.         }
  63.         return null;
  64.     }
  65.  
  66.     private Callable<Object> getResponse = new Callable<Object>() {
  67.         @NotNull
  68.         public Object call() {
  69.             StringBuilder responseBuilder = new StringBuilder();
  70.             while(true) {
  71.                 String line = null;
  72.                 try {
  73.                     line = in.readLine();
  74.                 } catch (IOException ex) {
  75.                     Bot.getLogManager().error("Error while reading response line", ex);
  76.                 }
  77.                 if(Objects.equals(line, "") || Objects.equals(line, "\r\n")) continue;
  78.                 responseBuilder.append(line).append("\n");
  79.                 if(Objects.equals(line, confirmationString)) break;
  80.             }
  81.             String response = responseBuilder.toString();
  82.             return response.substring(0, (response.length() - 1));
  83.         }
  84.     };
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement