Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Method for receiving from server
- public void receive() {
- Runnable serverReceiver = new Runnable() {
- public void run() {
- // Generally the isr (InputStreamReader) is set to null after Process.waitFor() is finished.
- if (isr != null) {
- try {
- // Generally the br (BufferedReader) is set to null after Process.waitFor() is finished.
- if ((isr.ready()) && (br != null)) {
- try {
- StringBuilder line = new StringBuilder();
- System.out.println("About to read");
- // Reads from the BufferedReader while it's got stuff to share.
- while(br.ready()) {
- try {
- int character = br.read();
- if (character != -1) {
- if ((!Character.toString(Character.toChars(character)[0]).equals(">")) && (!Character.toString(Character.toChars(character)[0]).equals("\n"))) {
- line.append(Character.toChars(character));
- }
- }
- } catch (IOException e) {
- System.out.println("IOException on br.read()");
- }
- }
- System.out.println(line.toString()); // Debug output
- // Replace a blank console output but add to a non-blank one
- // 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.
- if (consoleOutput.getText().equals("")) {
- //System.out.println("1 time");
- consoleOutput.setText(line.toString());
- }
- // If consoleOutput already has data, add to it
- else {
- consoleOutput.setText(consoleOutput.getText() + line.toString());
- }
- } catch (IOException e) {
- System.out.println("IOException on br.ready()");
- }
- }
- } catch (IOException e) {
- System.out.println("IOException on isr.ready()");
- }
- }
- }
- };
- SwingUtilities.invokeLater(serverReceiver);
- }
- // Worker to stop the server
- SwingWorker Stop = new SwingWorker<Boolean, Boolean>() {
- @Override
- public Boolean doInBackground() {
- send("stop"); // Sends the command to shut down the server
- try {
- System.out.println("Stopping server.");
- ps.waitFor();
- serverStarted = false;
- System.out.println("Stopped server succesfully.");
- return true;
- } catch (InterruptedException e) {
- System.out.println("ps.waitFor() interrupted!");
- return false;
- }
- }
- @Override
- public void done() {
- try {
- // Close the reading streams
- isr.close();
- isr = null;
- br.close();
- br = null;
- } catch (IOException e) {
- System.out.println("Error stopping read streams");
- }
- }
- };
- /*
- Stop.execute() is called when gui's stop button is pressed.
- 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.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement