Login to the RMI server
I am trying to implement a two player game that can be played over a network using RMI. I have an interface Game
and a class GameImpl
that implements the interface. To allow the client to play one game, I have a server class that looks like this:
public class RMIServer {
public RMIServer(int port) throws RemoteException, AlreadyBoundException {
Registry registry = LocateRegistry.createRegistry(port);
Game game = new GameImpl();
registry.bind("Game", game);
}
public static void main(String[] args) throws RemoteException, AlreadyBoundException {
RMIServer server = new RMIServer(1099);
}
}
On the other hand, the client class looks like this:
public class RMIClient {
RMIClient(String host, int port) throws RemoteException, NotBoundException {
Registry registry = LocateRegistry.getRegistry(host, port);
Game game = (Game) registry.lookup("Game");
game.play();
}
public static void main(String[] args) throws RemoteException, NotBoundException {
RMIClient client = new RMIClient("127.0.0.1", 1099);
}
}
This implementation works well if only one player needs to play the game over the network. The problem I am currently facing is that I want to make a login so that two different players can play against each other over the network. The main problem is that I really don't know what to go with with potential users and all their games, and how I can verify their login.
I was thinking about saving Map<User, Game>
to the server and registering the correct game if the user is logged in, but I have no idea how to login or how to make sure the client is in the right game.
Any help is appreciated
source to share
I was asked the same questions when I developed a 2-player minesweeper using RMI. First you need to ask yourself how do you want games to be created?
-
Do you want to create a game between the first two players available ("play ASAP" mode)? Example:
- player1 connects - player2 connects - the server automatically starts a game between player1 and player2
-
Would you like the players to choose their opponent (saloon)? Example:
- player1 connects - player2 connects - player3 connects - player1 decides to start a game with player3
If you decide to go with the second mode, here's one way.
On the server, save the list with the currently connected players. Every time a client connects to the server and logs in successfully, add it to this list:
/** The connected players */
private List<Player> connectedPlayers;
Also prepare a map with a game instance associated with each player (by the way, you were on the right track!).
/** The games currently being played */
private Map<Player, Game> gameList;
When a game is created between two players (player1 wants to play with player2), add them to the games list.
Game game = new Game(player1, player2);
this.gameList.put(player1, game);
this.gameList.put(player2, game);
If you need to know if a user is available and not yet in the game:
public boolean isUserAvailable(String potentialOpponent) throws RemoteException {
for (Entry<Player, Game> entry : this.gameList.entrySet()) {
if (entry.getKey().getUsername().equals(potentialOpponent)) {
return false;
}
}
return true;
}
When the game is over, remove 2 players from the map.
this.gameList.remove(game.getPlayerHost());
this.gameList.remove(game.getPlayerClient());
I won't copy / paste more code here, but if you need inspiration, you can find the source code in my game.These files are interesting:
source to share
First of all, you solve it at an extremely low level. Unless you have a compelling reason, you should consider using a higher level of abstraction like ...
- Spring Framework and optionally also Spring Remoting RMI
- Enterprise JavaBeans
- Apache Camel
These technologies will allow you to continue to use RMI as a transport , but also easily create components using the Session Facade .
It would be very tedious to do it all yourself.
Clarify your answer. You must save your game object using the Facial Session template.
This is your main question ...
I don’t know how to login, or how to make sure the client is in the right game.
And it's deceptively complex.
- "How to Login" is an implementation of the account service (complete with tokens / cookies).
- "How to make sure the client is getting the right game" is a session scope implementation that can maintain your game objects and return them to the user who presents the correct tokens / credentials.
source to share