0

This is my situation in pseudocode:

function onRequest() {
    receiveConnectionRequest();

    response = function onConnect() {
        playersConnected++;
        if (playersConnected == 4) {
            sendAllPlayersTheirCards()
        }
        return OK;
    }();

    sendResponse(response);

}

When players 1-3 connect, they get added to the list of players and OK is returned to them, at which point they will set stuff up on their side. When player 4 connects, however, before the response to his request is sent, all players are sent their cards. Because player 4 has not received a response to his request yet, he hasn't initialised correctly yet and errors upon receiving his cards.

What I'd like to have is this:

function onRequest() {
    receiveConnectionRequest();

    response = function onConnect() {
        playersConnected++;
        if (playersConnected == 4) {
            plan(sendAllPlayersTheirCards())
        }
        return OK;
    }();

    sendResponse(response);

    executePlanned() // now cards are sent
}

Is there a general pattern for that? The onConnect function is in a different class and should not be aware of the implementation details of onRequest.

I'm specifically trying to implement this in Java, but generic solutions are welcome.

Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301
  • Looks like delegation. plan() is, here, setting up the delegation and executePlanned() is invoking the queued actions. – sorpigal Nov 29 '10 at 17:06

5 Answers5

1

Many good solutions to this, but I would suggest using a call back.

Don't think of it as doing something after the method returns, think of it as calling back to report a state or trigger an event.

Search for event pattern , or call backs in Java. Here is one link I found.

The delegation pattern is very similar to.

Andy
  • 8,841
  • 8
  • 45
  • 68
0

Can you not send the response first and then do the "if(playersConnected" check after?

jzd
  • 23,473
  • 9
  • 54
  • 76
0

I'm not sure about this closure syntax you have here. Can you just have this instead?

function onRequest() {
    playersConnected++;

    sendResponse(OK);

    if (playersConnected == 4) {
        sendAllPlayersTheirCards()
    }
}
daveb
  • 74,111
  • 6
  • 45
  • 51
  • I can't because the code that handles the receiving of the request and the sending of the response is part of a generic package, and the code that does playersConnected++ is part of another, game-specific package. – Bart van Heukelom Nov 29 '10 at 23:08
0

This seems like a relevant tutorial:

Using Callable to Return Results From Runnables

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
0

I could do it like this:

function onRequest(request) {

 game.handleRequest(request, function(response) {
  sendResponse(response);
 }

}

class game
function handleRequest(request, sendResponse:Function) {
 players++;
 sendResponse(OK);
 if (players == 4) sendCards();
}
Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301