i woud almost think the business channel between the dealer and the players shoud be a connected TCP socket (not a UDP multicast). I assume the multicast socket is for a broadcast of events as they occur from the dealer server right.
So the idea of allowing the dealer to know how many players could be handled by maintaining a list of connected TCP socket clients
|
Code:
|
ServerSocket sock = null;
Vector<Connection> connectedClients = new Vector<Connection>();
public void runServer() {
try {
sock = new ServerSocket(port, 0, null);
}
catch (IOException ex) {
return;
}
System.out.println("tcp server socket thread starting, listening on " + listen + ":" + port);
running = true;
while (running) {
try {
System.out.println("waiting for a connection.");
Socket clientSock = sock.accept();
System.out.println("new client connected: " + clientSock.getRemoteSocketAddress());
// i invented this class, one thread instance per connected client for its own handler of messages
ClientConnection c = new ClientConnection(this, clientSock);
connectedClients.add(c);
c.start();
}
catch (IOException ex) {
LOG.error(ex, ex);
}
} // while running
} |
Where the ClientConnection object is a class to store all of the 'state' of a connected client in the server.
might be something like
|
Code:
|
class ClientConnection extends Thread {
Socket clientSocket;
TcpSocketListener parent;
public ClientConnection (TcpSocketListener aParent, Socket aClientSocket) {
this.parent = aParent;
this.clientSocket = aClientSocket;
}
public void run() {
// stuff that would be done here, like listening for actions from the client
}
// invoked by the handler from this class to have this connected client removed from the server's connected list
public void close() {
connectedClients.remove(this);
}
// maybe a method here to send message (e.g. do a deal )
// but again, we would probably want to define our own high level message object
// and work to have this message sent back and forth.
// (e.g. serialize a bean like we did in the multicast stuff)
public void sendMessage(MessageBean aMessage) {
// TODO: this woud be called by the server thread, in a for loop for each connected client
// but this woud make use of this object's connected socket.
}
} |
So the game operation might try to get the contents of the connectedClients vector (of ClientConnection beans), and do a for(...) each of these, deal a card.