Grizzly 2.0 StreamReader/StreamWriter API

When you work with Grizzly 2.0, you'll most probably come to situation, when you need to read or write some data on network :))

Core Grizzly 2.0 I/O API is based on 3 main entities:

  • Connection, which represents any kind of Transport connection. As example for TCP NIO Transport, one Connection represents one NIO SocketChannel.
  • StreamReader represents Connection's input stream, using which it's possible to read data from Connection.
  • StreamWriter represents Connection's output stream to write data to Connection.

So to read data from Connection, we need to read data from Connection's StreamReader: connection.getStreamReader().readXXX(); The similar step, but with StreamWriter, we need to make in order to write some data to Connection: connection.getStreamWriter().writeXXX(...);

StreamReader and StreamWriter API has a set of readXXX(), writeXXX() methods to work with Java primitives and arrays of primitives. For example to write float value to Connection we call corresponding StreamWriter write method: connection.getStreamWriter().writeFloat(0.3f);

How Streams could work in non-blocking?

In Grizzly 2.0 we can work with StreamReader and StreamWriter either in blocking or non-blocking mode. The mode could be checked and set using following methods: isBlocking(), setBlocking(boolean).

When Streams operate in blocking mode - all their methods, which may work asynchronously (return "Future") will work in blocking mode, and returned Future will always have ready result.

As for non-blocking mode, ok let's start from...

StreamReader. 

In order to use StreamReader in non-blocking mode - we may want to check if StreamReader has enough data to be read: streamReader.availableDataSize(); So once StreamReader has enough data - we can safely read it without blocking. 

It is also possible to ask StreamReader to notify us once it will have enough data, or provide any other custom Condition, which StreamReader should check each time new data come - and notify us, once this Condition is met. For example:

Future<Integer> future = streamReader.notifyAvailable(10, completionHandler);

StreamReader returns Future object, which will mark as done, once StreamReader will have available 10 bytes for reading. At the same time we pass the CompletionHandler, which will be notified, once StreamReader will have 10 bytes available. So it's possible to have poll and push-like notifications with StreamReader.

We can ask StreamReader to get notification, when more complex conditions are met.

Future<Integer> future = streamReader.notifyCondition(customCondition, completionHandler); 

This way we implemented SSL handshake mechanism, so SSLStreamReader notifies us, when handshake status becomes NEED_WRAP.

StreamWriter. 

Non-blocking mode for StreamWriter means, that stream flushing will be done non-blocking. For example:

Future<Integer> future = streamWriter.flush(); 

where flush() operation returns Future<Integer>, which could be used to check if bytes were flushed and how may bytes were written on Connection. It is also possible to pass CompletionHandler to flush() operation, which will be notified by StreamWriter, once bytes will be flushed.

 

Comments:

Nice post. Being not really familiar with socketprogramming, I stumbled over grizzly and it seems a nice and solid API to me. I would love to buy a book or read some good tutorials about v2.0 - but currently, it seems to be difficult to get even simple things done in the grizzly way... e.g. I tried to listen for a simple "command" and therefore I looked at the EchoFilter... for me it looks like implementing a Filter is the way to go for retrieving and working with messages ... but than, it seems like my buffer needs o have exactly the same size as the received mesage - otherwise a BufferUnderrunException occurs...

It would be great to see some even trivial tutorials/documentations about v2.0 ... Grizzly should gain more attention!

Posted by mcahornsirup on May 16, 2009 at 05:40 AM CEST #

Could you take some examples for the below:
1. CompletionHandler for the method notifyAvailable of class StreamReader, I hope to see a class implement CompletionHandler;
2. Condition for the method notifyCondition of class StreamReaderr, I hope to see classes implement Condition, CompletionHandler;
3. CompletionHandler for the method flush of class StreamWriter, I hope to see a class implement CompletionHandler;

thank you!

Posted by wenwu on May 21, 2009 at 02:02 AM CEST #

2mcahornsirup:
We're trying to provide as much as possible documentations with blogs and javadocs, but complete tutorial is the part, which is missed in Grizzly.
To get quick help with the Grizzly related question, you can use our mailing lists [1]. If you can describe your scenario and share the code - I'll try to help you to solve the issues.

Thanks.

[1] https://grizzly.dev.java.net/servlets/ProjectMailingListList

Posted by oleksiys on May 26, 2009 at 08:03 AM CEST #

2wenwu:
the reference to the Grizzly mailing list.
http://www.nabble.com/About-CompletionHandler-of-Grizzly2.0-td23663880.html#a23663880

Posted by oleksiys on May 26, 2009 at 08:07 AM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

oleksiys

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today