Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

Network: Code review - Detecting Client() timeout and behaving well during connection process

$
0
0
I've built a simple little network system based on Server and Client.  I can't always control when the parts start so I'm trying to make the connection process as smooth as possible.  

I'd like to be able have the client start first and then cleanly pick up when the server starts.  It needs to retry a connection, or at least handle the timeout cleanly.  And I like to be able to stop the app early if I change my mind and decide to not to start.

The server side works fine with nothing special.  The app is responsive works as expected while waiting for a client to connect.  The client side seems to block until it either times out or connects.  During the client's timeout period the app is not responsive and can't be quit early if the user decides to give up.  Finally if it does timeout there isn't a direct way to detect that it timed out.

I eventually built a bit of code that does works to some level.  I have a simple state machine in draw() that works through a couple startup states.

  1.       println("waiting for server");
  2.       // there's also a text() here expleining this to user.
  3.       boolean gotNetwork = true;
  4.       myClient = new Client(this, "127.0.0.1", 21015);
  5.       try {      
  6.         String tmp = myClient.ip();
  7.       }
  8.       // it appears there is no direct way to detect a successful connection.
  9.       // However Client.ip() will throw a NullPointException when it's not connected.
  10.       catch (java.lang.NullPointerException e) {
  11.         gotNetwork = false;
  12.       }
  13.       if (gotNetwork) {
  14.         initStage++;             // controls my draw statemachine.  incrementing moves to next state 
  15.       }
    There's doesn't appear to be a direct way to notice that the connection failed so I do it by looking for a NullPointerException from .ip() .  Also, this lets me handle the 'ESC' key when it times out and falls off the end of draw().   It's not instant but it works.

    Any thoughts on how I'm pulling this off?  Suggestions on a better a way to do either of these things?

        Viewing all articles
        Browse latest Browse all 1768

        Trending Articles