users@grizzly.java.net

Seeking Some Guidance on Using Grizzly / NIO

From: Edwin Lee <edwin11_1979_at_yahoo.com.sg>
Date: Wed, 29 Apr 2009 18:34:11 +0800 (SGT)

Hello all, i am new to Grizzly and NIO. i have read some of the documentations, javadoc, and examples around, and have some idea to do what i intend, but the knowledge is in patches, with some gaps around. Hence, would appreciate some pointers from all of you! :-) What i'm trying to do is a very simple TCP relay server. 1. i will listen to incoming TCP connections on a port. 2. Whenever an incoming connection is made, i will initiate a corresponding outgoing connection to the target server. (i.e.. from the perspective of my server, there is a 1 - 1 mapping between the incoming connections and the outgoing connections). 3. When i receive some bytes from one of the incoming connection, i will store it. When the bytes make up a complete message, i will send out the whole message to the target server through the outgoing connection that corresponds to the incoming connection through which the message is received. 4. Same thing vice-versa. When i receive a response from an outgoing connection, i will forward it back through the corresponding incoming connection. 5. When a connection is closed - either the incoming or outgoing connection - i will close the corresponding other connection. Now this is what i think i should do (may be horribly off-track!). 1. Initiate a Controller. 2. Add TCPSelectorHandler to Controller. 3. Set a ProtocolChainInstanceHandler to the Controller. 4. In the ProtocolChainInstanceHandler, i will have the ReadFilter, and 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. 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. 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. 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? 4. How do i listen for and get the event of a connection closed by the other party? Thanks very much in advance! Regards, Edwin 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/