Trouble with Selectors, please help
Hello everyone,
I'm running into some problems here that I'm really not sure how to get past. I'm writing a chat program and so far can't seem to link a client with a chat channel. Each channel in my code has it's own thread with it's own Selector listening for data. I have a Lobby manager as well with a list of all the channels a client can join. So far the lobby manager works, the client logins in with user name a password, this is validated through MySQL and then the client proceeds to the lobby.
On the host side: The host logins in, and is registered with the Selector in the lobby manager, once the lobby manager spawns a new host thread I cancel the selector with the lobby, and try to open, and register the host socket with the selector in the host thread, however the socket is not de registering from the lobby like I want it to. I cannot register the host socket until it is let go of in the lobby manager, any ideas?Here's some code.
Here is the section in the lobby manager where the host is being unregistered
if(host.memberId == hostID)
{
// Found a match...
// Unregister the socket from the lobby selector
// and register with the desired host selector.
sc.keyFor(readSelector).cancel();
readSelector.wakeup();
sc.register(host.readSelector, SelectionKey.OP_READ,null);
...
This is my constructor for the host thread. Here it checks if the socket is already registered. This is always the case so far :(
public CHostThread(CUserHost host)
{
this.host = host;
clientList =new HashSet<CUserClient>();
try{
readSelector = Selector.open();
// Now add the host to this selector
if(host.sc.isRegistered())
{
System.out.println("This host is already registered elsewhere");
}
host.sc.register(readSelector, SelectionKey.OP_READ,null);
readSelector.wakeup();
}catch(IOException ioe){}
}// public CHostThread(CUserHost host)

