Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.20 KB | None | 0 0
  1.     // Method for receiving from server
  2.     public void receive() {
  3.         Runnable serverReceiver = new Runnable() {
  4.             public void run() {
  5.                 // Generally the isr (InputStreamReader) is set to null after Process.waitFor() is finished.
  6.                 if (isr != null) {
  7.                     try {
  8.                         // Generally the br (BufferedReader) is set to null after Process.waitFor() is finished.
  9.                         if ((isr.ready()) && (br != null)) {
  10.                             try {
  11.                                 StringBuilder line = new StringBuilder();
  12.                                 System.out.println("About to read");
  13.                                 // Reads from the BufferedReader while it's got stuff to share.
  14.                                 while(br.ready()) {
  15.                                     try {
  16.                                         int character = br.read();
  17.                                         if (character != -1) {
  18.                                             if ((!Character.toString(Character.toChars(character)[0]).equals(">")) && (!Character.toString(Character.toChars(character)[0]).equals("\n"))) {
  19.                                                 line.append(Character.toChars(character));
  20.                                             }
  21.                                         }
  22.                                     } catch (IOException e) {
  23.                                         System.out.println("IOException on br.read()");
  24.                                     }
  25.                                 }
  26.                                 System.out.println(line.toString());        // Debug output
  27.  
  28.                                 // Replace a blank console output but add to a non-blank one
  29.                                 // consoleOutput is a reference to the same object in the GUI view class.  It is only used for this purpose, thus I passed it to this class that handles the server crap.
  30.                 if (consoleOutput.getText().equals("")) {
  31.                                     //System.out.println("1 time");
  32.                                     consoleOutput.setText(line.toString());
  33.                                 }
  34.                                     // If consoleOutput already has data, add to it
  35.                                 else {
  36.                                     consoleOutput.setText(consoleOutput.getText() + line.toString());
  37.                                 }
  38.                                
  39.                             } catch (IOException e) {
  40.                                 System.out.println("IOException on br.ready()");
  41.                             }
  42.                         }
  43.                     } catch (IOException e) {
  44.                         System.out.println("IOException on isr.ready()");
  45.                     }
  46.                 }
  47.             }
  48.         };
  49.         SwingUtilities.invokeLater(serverReceiver);
  50.     }
  51.  
  52.  
  53.  
  54.     // Worker to stop the server
  55.     SwingWorker Stop = new SwingWorker<Boolean, Boolean>() {
  56.         @Override
  57.         public Boolean doInBackground() {
  58.             send("stop");                           // Sends the command to shut down the server
  59.             try {
  60.                 System.out.println("Stopping server.");
  61.                 ps.waitFor();
  62.  
  63.                 serverStarted = false;
  64.                 System.out.println("Stopped server succesfully.");
  65.                 return true;
  66.             } catch (InterruptedException e) {
  67.                 System.out.println("ps.waitFor() interrupted!");
  68.                 return false;
  69.             }
  70.         }
  71.  
  72.         @Override
  73.         public void done() {
  74.             try {
  75.                     // Close the reading streams
  76.                     isr.close();
  77.                     isr = null;
  78.                     br.close();
  79.                     br = null;
  80.                 } catch (IOException e) {
  81.                     System.out.println("Error stopping read streams");
  82.                 }
  83.         }
  84.     };
  85.  
  86.  
  87.  
  88. /*
  89. Stop.execute() is called when gui's stop button is pressed.
  90. The output of the server needs to continue streaming the consoleOutput until the task is completely finished but currently it stops right after pressing the stop button.
  91. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement