Processing Net : avoid print disconnection message

@kfrajer @GoToLoop first of all thanks to both of you for your time .

Ill try to explaine a little more my “weird” question and the solution i have found after your suggestions

CASE SCENARIO :

  • the thread is blocked waiting on accept()
  • an socket exception is thrown
    QUESTION :
  • Who has launched the exception ?
    ANSWER :
  • it’s me stopping the server —> OK, dont print nothing we know it
  • it is a real network problem —> ERROR, print the stack trace

As you can see my goal is to distinguish two separate cases and handle them of consequence .

As suggested i worked on the HServer.java file and extended the class.

As solution i added a boolean variable named “stopIsRequested” and overrided stop() and run() method with minimal difference from the original source code as follow :

package processing.net;

import processing.core.*;

import java.io.*;
import java.lang.reflect.*;
import java.net.*;

public class HServer extends Server 
{
    boolean stopIsRequested ;
    
    
    public HServer(PApplet parent, int port) 
    {
        this(parent, port, null);
    }

    public HServer(PApplet parent, int port, String host) 
    {
        super(parent, port, host);
    }
 
 @Override public void stop() 
    {
    this.stopIsRequested = true ;
    dispose();
    }
    
@Override public void run() {
    
    this.stopIsRequested = false;
    
    while (Thread.currentThread() == thread) {
      try {
        Socket socket = server.accept();
        Client client = new Client(parent, socket);
        synchronized (clientsLock) {
          addClient(client);
          if (serverEventMethod != null) {
            try {
              serverEventMethod.invoke(parent, this, client);
            } catch (Exception e) {
              System.err.println("Disabling serverEvent() for port " + port);
              Throwable cause = e;
              // unwrap the exception if it came from the user code
              if (e instanceof InvocationTargetException && e.getCause() != null) {
                cause = e.getCause();
              }
              cause.printStackTrace();
              serverEventMethod = null;
            }
          }
        }
      } catch (SocketException e) {
        //thrown when server.close() is called and server is waiting on accept
        if( ! stopIsRequested ) { System.err.println("Server SocketException: " + e.getMessage()); };
        thread = null;
      } catch (IOException e) {
        //errorMessage("run", e);
        e.printStackTrace();
        thread = null;
      }
    }
    }
}

what you think about this ?

Thanks in advance.

1 Like