Search code examples
javarmi

How to invoke method on the client from the server?


I'm developing a game both with sockets and RMI. The sockets part is done now i'm starting the RMI part. I want to be able to invite someone to play (A->B) and get the response(B->A).

Can you help me?

Thanks in advance. Cheers

EDIT : I've been researching and i'm currently implementing the client as a remote object as well (defining the interface and implementing it). I think it's called callback. Am I on the right path?

What i'm thinking is A calls a method on a remote object on B's computer. The server on B asks the client (which is also a server in the same computer) if it wants to play with A gets the response and returns it to A. Like i said i just finished implementing this game with tcp and udp sockets and now i have to do the same with RMI. There is no way for two clients to communicate directly, like they do over a TCP connection, using RMI, is there?


Solution

  • I've been researching and i'm currently implementing the client as a remote object as well (defining the interface and implementing it). I think it's called callback. Am I on the right path?

    RMI is usually implemented as a client and a server. The client makes calls on the server which returns an object that can be consumed on the client. After reading the comments, they are correct that given the way you have described the problem, you will need to make the client also a RMI server. However, I'm not convinced this is necessary.

    For example, let's say you have 2 clients that are both trying to play a game. The protocol might look something like this:

    1. A contacts the server S. Server stores the address A and then returns a waiting-for-another-player type message to A.
    2. A then contacts S every couple of seconds to see if the other player has shown up.
    3. B then contacts S and a game is started between A and B. S returns a start-game message to B. S stores some sort of Game object in its memory (and/or on disk) which tracks the progress of the game.
    4. The next time A contacts the server it also gets a start-game message.

    So instead of the server needing to contact A or B, they poll every so often to get game updates and the like.

    The method calls to S could also wait for the other player and not return until there is one. I think the polling option is better however because then S knows if A stops calling and A doesn't have to worry about S crashing and hanging.

    Hope this helps.