users@grizzly.java.net

Re: Seeking Some Guidance on Using Grizzly / NIO

From: Edwin Lee <edwin11_1979_at_yahoo.com.sg>
Date: Wed, 29 Apr 2009 20:27:02 +0800 (SGT)

Hi Alexey, Thanks so much for the pointers and examples! i'll keep these in mind, but, as the server is to be deployed in a production system, it's really preferable to use the 1.x stable branch. Would really appreciate if you could advise based on the context of 1.x? Thanks again! Regards, Edwin --- On Wed, 29/4/09, Oleksiy Stashok <Oleksiy.Stashok@Sun.COM> wrote: > From: Oleksiy Stashok <Oleksiy.Stashok@Sun.COM> > > as I understand you try to work with Grizzly 1.x branch. > IMHO for your scenario it could make sense to try Grizzly > 2.0 M1, where we reworked/improved API. Grizzly 2.0 still > misses some higher level modules like Comet, but core API is > already there and ready to be used. > I will provide you comments in regards with Grizzly 2.0, > but if you'll fill strong about using 1.x branch - will try > to help you with that. > > > Now this is what i think i should do (may be horribly > off-track!). > > > > 1. Initiate a Controller. > > > > > > 2. Add TCPSelectorHandler to Controller. > In Grizzly 2.0 we initialize Transport. > >         TCPNIOTransport transport = > TransportFactory.getInstance().createTCPTransport(); >         transport.bind(PORT); >         transport.start(); > > > > 3. Set a ProtocolChainInstanceHandler to the > Controller. > > 4. In the ProtocolChainInstanceHandler, i will have > the ReadFilter, and MyOwnFilter.. > Instead of ReadFilter, in 2.0 we use TransportFilter. > >         > transport.getFilterChain().add(new TransportFilter()); >         > transport.getFilterChain().add(new MyOwnFilter()); > > > > 5. MyOwnFilter is where i do the work of retrieving > bytes from the ByteBuffer (from current WorkerThread), > storing it somewhere, checking whether a complete message > has already been formed, and sending it out if it has. > > > > 6. The above is for the server part. For the client > part, i will instantiate another Controller (or can i use > the same Controller instance?), and add a TCPSelectorHandler > (instantiated in client mode). Also for this controller, set > the ProtocolChainInstanceHandler which will have another > ReadFilter and AnotherMyOwnFilter. AnotherMyOwnFilter will > read the bytes and forward it back through the corresponding > incoming connection. > It's up to you, but IMHO you can use the same FilterChain > for client side. > > > Some questions: > > > > 1. How do i listen for and get the event where an > incoming connection is accepted? i will need to create a > corresponding outgoing connection with the target server. > Grizzly 2.0 Filter has handleAccept() callback method, > which will be notified, when new connection is getting > accepted. > So you can basically implement handleAccept() method of > MyOwnFilter() to make a connection to target endpoint. > > Example [1] > > > > > 2. And also, how can i get hold of the channel > associated with this incoming connection? i will need this > channel to forward response back later on. > You can either have Map in MyOwnFilter, or use Connection > attributes. > Connection attribute example is here [1], take a look how > > > > > > > > 3. How do i initiate and establish an outgoing > connection with the target server, and get hold of the > channel associated with the outgoing connection? > To connect to remote endpoint, do this: >             > Future<Connection> future = > transport.connect("localhost", PORT); >             TCPNIOConnection > connection = (TCPNIOConnection) future.get(10, > TimeUnit.SECONDS); > > to store any connection associated parameters - you can use > Attributes we talked earlier. > > > 4.. How do i listen for and get the event of a > connection closed by the other party? > You can intercept MyOwnFilter's handleClose() method, which > is called by framework, when connection is getting closed. > > > Hope this will help. > To find more examples on Grizzly 2.0 - you can use [2], or > if you have any questions - please ask. > > WBR, > Alexey. > > [1] https://grizzly.dev.java.net/source/browse/grizzly/branches/2dot0/code/samples/framework-samples/src/main/java/org/glassfish/grizzly/samples/lifecycle/LifeCycleFilter.java?view=markup > [2] https://grizzly.dev.java.net/source/browse/grizzly/branches/2dot0/code/samples/framework-samples/src/main/java/org/glassfish/grizzly/samples/ New Email names for you! Get the Email name you&#39;ve always wanted on the new @ymail and @rocketmail. Hurry before someone else does! http://mail.promotions.yahoo.com/newdomains/sg/