users@grizzly.java.net

Re: Multiple application protocols handlers over the same NIO channel ?

From: Alessio Pace <puccio_13_at_yahoo.it>
Date: Mon, 7 Apr 2008 13:51:54 +0000 (GMT)
Hi,

thanks a lot for the reply. I'll try to have a look at it, thanks!

--
Alessio Pace.



--- Lun 7/4/08, Oleksiy Stashok <Oleksiy.Stashok@Sun.COM> ha scritto:
Da: Oleksiy Stashok <Oleksiy.Stashok@Sun.COM>
Oggetto: Re: Multiple application protocols handlers over the same NIO channel ?
A: users@grizzly.dev.java.net
Data: Luned́ 7 Aprile 2008, 15:36

Hello,

think Grizzly port unification (grizzly-portunif module) can do what you need.

To use port unification - you need to add com.sun.grizzly.portunif.PUReadFilter as first filter to the protocol chain and register there custom com.sun.grizzly.portunif.ProtocolFinder and com.sun.grizzly.portunif.ProtocolHandler for each application layer protocol.
Here is small example:

        // Register 2 application layer protocols: "ABC", "QWER"
        String[] protocolIds = new String[] {"ABC", "QWER"};
        PUReadFilter puReadFilter = new PUReadFilter();
        for(String protocolId : protocolIds) {
            puReadFilter.addProtocolFinder(new SimpleProtocolFinder(protocolId));
            puReadFilter.addProtocolHandler(new SimpleProtocolHandler(protocolId));
        }

where SimpleProtocolFinder - is the simple ProtocolFinder implementation, which checks incoming request to start for specific bytes (passed in the constructor). 
SimpleProtocolHandler - is simple protocol specific handler implementation. It processes recognized application layer protocol.

So, basically for each application layer protocol, you'll need to implement ProtocolFinder and ProtocolHandler. Where ProtocolFinder will have logic for application layer protocol recognition, and ProtocolHandler - logic for application layer protocol processing.

To see example - please look at unit tests of grizzly-portunif module: com.sun.grizzly.portunif.PUBasicTest

Thanks.

WBR,
Alexey.

public class SimpleProtocolFinder implements ProtocolFinder {

    private String protocolId;
    
    private byte[] protocolIdBytes;

    public SimpleProtocolFinder(String protocolId) {
        this.protocolId = protocolId;
        protocolIdBytes = protocolId.getBytes();
    }
    
    public String find(Context context, PUProtocolRequest protocolRequest) throws IOException {
        ByteBuffer buffer = protocolRequest.getByteBuffer();
        int position = buffer.position();
        int limit = buffer.limit();
        try {
            buffer.flip();

            // Check if request bytes are starting from protocolId bytes

            if (buffer.remaining() >= protocolId.length()) {
                for(int i=0; i<protocolId.length(); i++) {
                    if (buffer.get(i) != protocolIdBytes[i]) {
                        // It's not our protocol
                        return null;
                    }
                }
                
                // Found!
                return protocolId;
            }
        } finally {
            buffer.limit(limit);
            buffer.position(position);
        }
        
        return null;
    }
}

public class SimpleProtocolHandler implements ProtocolHandler {

    private String protocolId;

    public SimpleProtocolHandler(String protocolId) {
        this.protocolId = protocolId;
    }
    
    // Which protocols we support
    public String[] getProtocols() {
        return new String[] {protocolId};
    }

    public boolean handle(Context context, PUProtocolRequest protocolRequest) throws IOException {
        ByteBuffer buffer = protocolRequest.getByteBuffer();
        SelectionKey key = protocolRequest.getSelectionKey();
        
        buffer.flip();
/** ------------- Here you process a request ---------- */
        return true;
    }

    // Key is expired, can we cancel it?
    public boolean expireKey(SelectionKey key) {
        return true;
    }

    // Use some other ByteBuffer, where PUReadFilter will read an incoming data
    public ByteBuffer getByteBuffer() {
        // Use thread associated byte buffer
        return null;
    }
}

On Apr 6, 2008, at 12:04 , Alessio Pace wrote:
Hello,

I'm new to this ML, and I'm pretty new to Grizzly too (I've seen some presentations and some documentation).

I would like to know if I can use Grizzly to register all my application layer protocols over the same channel (ie. a NIO DatagramChannel), and dispatch in/out messages to/from my application layer specific handlers. 

My requirements are that there is one DatagramChannel opened once and for all (to send/receive stuff),  then the application layer handlers just send/receive stuff to/from the layer below (Grizzly filter chain or whatever) without knowing of the detail of the transport protocol (if not really necessary).

If it is doable, is there any example to look at or some guide lines that you can provide me?

Thanks in advance,

Alessio Pace


Inviato da Yahoo! Mail.
La casella di posta intelligente. --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscribe@grizzly.dev.java.net For additional commands, e-mail: users-help@grizzly.dev.java.net



Inviato da Yahoo! Mail.
La casella di posta intelligente.