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)

[2712 byte] By [Tigerfanga] at [2007-10-3 4:32:21]
# 1
This may be a synchronization problem. Try the selector wakeup before the cancel instead of after it.However I would rethink this whole design. The whole point of using Selectors is that you don't need multiple threads at all, or multiple selectors either.
ejpa at 2007-7-14 22:35:52 > top of Java-index,Core,Core APIs...
# 2

ejp: You say that I should re think this design ? Hmmm, well, I'm still a little new to client / server programming. I'm open to ideas. Basically what I'm trying to make is a video chat program.

My thoughts were that for each video host that is online a separete thread will be running. And each client that logs in will connect to the video host. This is my problem so far.... getting the client to connect with the host. I have a lobby class that everyone connects to to begin with, then the clients are transfered to the chat channel they chose. So, knowing that about my project can anyone suggest some ideas?I didn't really want to spawn a new thread for every socket connection, I want to keep the thread count as low as possible.

In the meantime I'm going to read up more on Selectors and how they work. Because for some reason I'm having problems deregistering a socket from one selector, and the registering it to another.

Thanks for any help.

Mike

Message was edited by:

Tigerfang

Tigerfanga at 2007-7-14 22:35:52 > top of Java-index,Core,Core APIs...
# 3
Ummmm, could this kind of *hybrid* design, theads + NBIO, have any viability on any app requirement or situation? I like to have guru ejp's reply. Sorry I still don't have bought his book but I will soon.
hiwaa at 2007-7-14 22:35:52 > top of Java-index,Core,Core APIs...
# 4
You can do it with multiple threads but then why engage in the pain of NIO?
ejpa at 2007-7-14 22:35:52 > top of Java-index,Core,Core APIs...