thufir

execution and controller

Sep 1st, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. thufir@dur:~/NetBeansProjects/TelnetConsole$
  2. thufir@dur:~/NetBeansProjects/TelnetConsole$ java -jar dist/TelnetConsole.jar
  3. noSuchElementException...
  4.  
  5. command
  6. no command sent
  7. noSuchElementException...
  8.  
  9. command
  10. no command sent
  11. noSuchElementException...
  12. ------------------------------------------------------------------------------
  13. * Welcome to THE WEATHER UNDERGROUND telnet service! *
  14. ------------------------------------------------------------------------------
  15. * *
  16. * National Weather Service information provided by Alden Electronics, Inc. *
  17. * and updated each minute as reports come in over our data feed. *
  18. * *
  19. * **Note: If you cannot get past this opening screen, you must use a *
  20. * different version of the "telnet" program--some of the ones for IBM *
  21. * compatible PC's have a bug that prevents proper connection. *
  22. * *
  23. * comments: [email protected] *
  24. ------------------------------------------------------------------------------
  25.  
  26. Press Return to continue:
  27. command
  28.  
  29. byte 10
  30. no command sent
  31.  
  32. noSuchElementException...
  33.  
  34. command
  35. no command sent
  36. noSuchElementException...
  37.  
  38. command
  39. no command sent
  40. anoSuchElementException...
  41.  
  42. command
  43. no command sent
  44.  
  45. your command a
  46. command a
  47. byte 97
  48. no command sent a
  49. noSuchElementException...
  50.  
  51. command
  52. no command sent
  53. b
  54. your command b
  55. command b
  56. byte 98
  57. no command sent b
  58. noSuchElementException...
  59.  
  60. command
  61. no command sent
  62.  
  63. your command
  64. command
  65. no command sent
  66. noSuchElementException...
  67.  
  68. command
  69. no command sent
  70. noSuchElementException...
  71.  
  72. command
  73. no command sent
  74. ^Cthufir@dur:~/NetBeansProjects/TelnetConsole$
  75. thufir@dur:~/NetBeansProjects/TelnetConsole$
  76. thufir@dur:~/NetBeansProjects/TelnetConsole$
  77.  
  78.  
  79.  
  80.  
  81. package telnet;
  82.  
  83. import static java.lang.System.out;
  84. import java.io.IOException;
  85. import java.io.InputStream;
  86. import java.io.OutputStream;
  87. import java.net.InetAddress;
  88. import java.net.SocketException;
  89. import java.util.Observable;
  90. import java.util.Observer;
  91. import java.util.Properties;
  92. import java.util.concurrent.ConcurrentLinkedQueue;
  93. import org.apache.commons.net.telnet.TelnetClient;
  94.  
  95. public final class Controller implements Observer {
  96.  
  97. private TelnetClient telnetClient = new TelnetClient();
  98. private InputStreamReader serverReader = new InputStreamReader();
  99. private ConsoleReader consoleReader = new ConsoleReader();
  100. private QueueWatcher dataProcessor = new QueueWatcher();
  101. private Triggers triggers = new Triggers();
  102. private final ConcurrentLinkedQueue<Character> telnetData = new ConcurrentLinkedQueue();
  103. private OutputStream outputStream = telnetClient.getOutputStream();
  104.  
  105. public void readPrintParse(final InputStream inputStream) throws SocketException, IOException {
  106. serverReader.print(inputStream, telnetData);
  107. serverReader.addObserver(this);
  108. consoleReader.read();
  109. consoleReader.addObserver(this);
  110. dataProcessor.read(telnetData);
  111. dataProcessor.addObserver(this);
  112. triggers.addObserver(this);
  113. }
  114.  
  115. private void sendCommand(String command) {
  116. out.println("command\t\t" + command);
  117. try {
  118. byte[] bytes = command.getBytes();
  119. for (int i = 0; i < bytes.length; i++) {
  120. out.println("byte\t" + bytes[i]);
  121. }
  122. outputStream.write(bytes);
  123. outputStream.flush();
  124. } catch (IOException | NullPointerException ex) {
  125. out.println("no command sent\t" + command);
  126. }
  127. }
  128.  
  129. @Override
  130. public void update(Observable o, Object arg) {
  131. String command = "help";
  132.  
  133. if (o instanceof QueueWatcher) {
  134. String data = dataProcessor.getFinalData();
  135. out.println("noSuchElementException...\n" + data);
  136. command = triggers.parse(data);
  137. sendCommand(command);
  138. }
  139.  
  140. if (o instanceof ConsoleReader) {
  141. command = consoleReader.getCommand();
  142. out.println("your command\t\t" + command);
  143. sendCommand(command);
  144. }
  145. }
  146.  
  147. public Controller() throws SocketException, IOException {
  148. Properties props = PropertiesReader.getProps();
  149. InetAddress host = InetAddress.getByName(props.getProperty("host"));
  150. int port = Integer.parseInt(props.getProperty("port"));
  151. telnetClient.connect(host, port);
  152. readPrintParse(telnetClient.getInputStream());
  153. }
  154.  
  155. public static void main(String[] args) throws SocketException, IOException {
  156. new Controller();
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment