users@grizzly.java.net

Re: Should I use Context to store the clients references ?

From: Survivant 00 <survivant00_at_gmail.com>
Date: Tue, 15 Jul 2008 07:55:07 -0400

oups.. I send the email by error..


the clientConnectionHandler is used to keep track of the client. SendBack
the data to it, unsubscribe when the client quit..


public class ClientConnectionHandler {

    protected QuoteManager f_manager;
    protected SelectionKey f_key;
    protected SelectorHandler f_selectorHandler;

    protected boolean f_shutdown = false;

    /**
     * manage the connection with the client
     * @param manager le QuoteManager
     */
    public ClientConnectionHandler(QuoteManager manager, SelectionKey key,
SelectorHandler selectorHandler){
        f_manager = manager;
        f_key = key;
        f_selectorHandler = selectorHandler;
    }


    public SelectionKey getKey() {
        return f_key;
    }

    public void setKey(SelectionKey f_key) {
        this.f_key = f_key;
    }

    public SelectorHandler getSelectorHandler() {
        return f_selectorHandler;
    }

    public void setSelectorHandler(SelectorHandler handler) {
        f_selectorHandler = handler;
    }


    /**
     * close the client connection
     */
    public synchronized void close(){

        System.out.println("ClientConnection close");
        if(f_shutdown){
            return;
        }
         // No more bytes can be read from the channel
        f_selectorHandler.getSelectionKeyHandler().cancel(f_key);

        // unsubcribe to quotefeed
        //f_manager.unRegisterClientConnectionHandler(this);
        f_manager.unsubcribeClient(this);

        f_shutdown = true;
    }

}



and I have a requestHandler.. that send the data back to the client


@Override
    public void sendToClient(StringBuffer sb) {

        ByteBuffer writeBuffer =
ByteBuffer.allocateDirect(sb.toString().getBytes().length);

        writeBuffer.put(sb.toString().getBytes());

        writeBuffer.flip();

        System.out.println("SENDING QUOTE TO CLIENT =" + sb.toString());

        try {
            ClientConnectionHandler clientConnectionHandler =
getClientConnectionHandler();

clientConnectionHandler.getSelectorHandler().getAsyncQueueWriter().write(clientConnectionHandler.getKey(),writeBuffer);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }



2008/7/15 Survivant 00 <survivant00_at_gmail.com>:

> sure that I have a snippets and thanks for your help.
>
>
> public class QuoteQueryManagerFilter implements ProtocolFilter {
>
> private QuoteManager manager;
>
> public QuoteQueryManagerFilter(QuoteManager manager) {
> this.manager = manager;
> }
>
> public boolean execute(Context context) throws IOException {
> String query = (String)
> context.removeAttribute(ProtocolParser.MESSAGE);
>
> if(query==null || query.trim().length()==0){
> return false;
> }
>
> System.out.println("query = " + query);
>
> //here should be the parsed query command string with
> // which query should be able to create a command
> // I have no time to rewrite ClientConnectionHandler
> // Basically you have in context a link to client connection
> //with that and for example TCPSelectorHandler your commands can
> //send data to client
>
> /*
> * For now instead of keeping whole Context, you can just keep
> SelectionKey as reference to the client,
> * and send response back using:
> selectorHandler.getAsyncQueueWriter().write(SelectionKey, ...);
> */
>
> // on va chercher le ClientConnectionHandler si il existe
>
> ClientConnectionHandler clientConnectionHandler = null;
>
> if(manager.selectionKeyMap.containsKey(context.getSelectionKey())){
> clientConnectionHandler =
> manager.selectionKeyMap.get(context.getSelectionKey());
> } else {
> clientConnectionHandler = new ClientConnectionHandler(manager,
> context.getSelectionKey(), context.getSelectorHandler());
> }
>
> manager.processQuery(clientConnectionHandler, query);
>
>
>
> 2008/7/15 Oleksiy Stashok <Oleksiy.Stashok_at_sun.com>:
>
> Hi,
>>
>>
>> I have a chain of 2 filters.
>>>
>>> #1 - parsing the request
>>> #2 - processing the request
>>>
>>> and loop until the client quit.
>>>
>>> I want to kept the info about the connection like :
>>> context.getSelectionKey(), context.getSelectorHandler() in a Object like :
>>>
>>> ClientTracker(SelectionKey key, SelectorHandler selectorHandler)
>>>
>>> and I'll send data back to the client using the key and the
>>> selectorhandler.
>>>
>>> but I don't know where to create that class (ClientTracker) and where to
>>> put it to use it later.
>>>
>>> if I create the ClientTracker into the 2 filters.. a new reference will
>>> be created each time the client send a request.. it's not what I want.
>>>
>>> I tought of putting the ClientTracker in the context and check if the
>>> class exist in the context.. but like the context could be invalidate, I
>>> could lose the info..
>>>
>>> Normally, I would have created the ClientTracker equivalent class, after
>>> the socketserver.accept();
>>>
>>
>> Do you want to store somewhere complete map of clients (ClientTracker),
>> which are currently connected?
>> Also, can you pls. provide more details how you want to use ClientTracker
>> class? Some code snippets...
>>
>> Thank you.
>>
>> WBR,
>> Alexey.
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
>> For additional commands, e-mail: users-help_at_grizzly.dev.java.net
>>
>>
>