directory-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Piyush Purang <ppur...@gmail.com>
Subject [MINA] Need some enlightenment ...
Date Wed, 11 Jan 2006 11:16:19 GMT
I have been trying to quickly integrate MINA into a small project that
i have undertaken.

I decided to use the HTTPProtocolHandler example as my guide. So in
the run method of my Worker thread I have these two lines of code tha
basically try and read all of the request at one go. (Is that
adviced?)

--- codesnippet 1
try {
                Reader reader = new InputStreamReader(new
BufferedInputStream(in), IOUtils.UTF_8);
                String request = IOUtils.read(reader,
                        IOUtils.DEFAULT_EOF_CHARACTER);
--- /codesnippet 1

The utility method read from the utility class IOUtils is as follows....

--- codesnippet2
	public static final String read(Reader reader, char eofChar)
			throws IOException {
		if(reader == null)
			throw new IllegalArgumentException("Reader can't be null!");
		
		char[] buffer = new char[BUFFER_CAPACITY];
		StringWriter stringCapture = new StringWriter(BUFFER_CAPACITY);

		LOGGER.trace("Beginning capture of input stream content...");
		long timeStart = System.currentTimeMillis();
		for (boolean eof = false; !eof;) {
			// it makes sense to try and read the reader iff it is ready to be
			// read!
			if (reader.ready()) {
				LOGGER.trace("Reader is ready to be read!");
				int amount = 0;
				if ((amount = reader.read(buffer)) > 0) {
					char pop = buffer[amount - 1];
					if (pop == eofChar) {
						LOGGER.trace("Encountered eof charachter.");
						eof = true;
						LOGGER.trace("Writing amount = " + (amount - 1));
						stringCapture.write(buffer, 0, amount - 1);
					} else {
						LOGGER.trace("Writing amount = " + amount);
						stringCapture.write(buffer, 0, amount);
					}
					stringCapture.flush();
				}
				LOGGER.trace("Read " + amount + " bytes");
			}
		}
		LOGGER.trace("Completed capturing input stream content in  ["
				+ ((System.currentTimeMillis() - timeStart) / 1000) + "s].");
		return stringCapture.toString();
	}
--- /codesnippet2


The client uses simple java sockets to connect with the server and
uses java.io classes in conjunction with IOUtils to write(which writes
the eof character as the last byte) and read content.

Running the server and client (individual java VMs) leads to odd
behaviour.. Most of the attempts at running them end up in both server
and client hanging up with CPU at 50+% .. obviously both of them are
trapped in the for loop of the read method ..

--- server log
0 [main] DEBUG de.cgs.vwtools.monitor.server.Server  - Listening on port 33333
6453 [IoThreadPool-1] DEBUG de.cgs.vwtools.monitor.server.Server  -
/127.0.0.1:1746
6453 [Thread-2] DEBUG de.cgs.vwtools.monitor.server.IOUtils  -
Beginning capture of input stream content...
6906 [Thread-2] DEBUG de.cgs.vwtools.monitor.server.IOUtils  - Reader
is ready to be read!

--- client log
0 [Thread-3] DEBUG de.cgs.vwtools.client.applets.CpuApplet  - writing
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cpuRequest className="de.cgs.vwtools.monitor.cpu.handler.CpuRequestHandler"
xmlns="http://cgs.de/vwtools/monitor/cpu/handler"><retreiveSnapshot
xmlns=""><dateTo>1136976270671</dateTo></retreiveSnapshot></cpuRequest>
16 [Thread-3] DEBUG de.cgs.vwtools.monitor.server.IOUtils  - wrote 275
16 [Thread-3] DEBUG de.cgs.vwtools.client.applets.CpuApplet  - Waiting
to read the response.
16 [Thread-3] DEBUG de.cgs.vwtools.monitor.server.IOUtils  - Beginning
capture of input stream content...


But sometimes just sometimes ...

--- sever log
0 [main] DEBUG de.cgs.vwtools.monitor.server.Server  - Listening on port 33333
14703 [IoThreadPool-1] DEBUG de.cgs.vwtools.monitor.server.Server  -
/127.0.0.1:1779
14750 [Thread-2] DEBUG de.cgs.vwtools.monitor.server.IOUtils  -
Beginning capture of input stream content...
15125 [Thread-2] DEBUG de.cgs.vwtools.monitor.server.IOUtils  - Reader
is ready to be read!
15125 [Thread-2] DEBUG de.cgs.vwtools.monitor.server.IOUtils  -
Encountered eof charachter.
15125 [Thread-2] DEBUG de.cgs.vwtools.monitor.server.IOUtils  -
Writing amount = 274
15125 [Thread-2] DEBUG de.cgs.vwtools.monitor.server.IOUtils  - Read 275 bytes
15125 [Thread-2] DEBUG de.cgs.vwtools.monitor.server.IOUtils  -
Completed capturing input stream content in  [0s].
15125 [Thread-2] DEBUG de.cgs.vwtools.monitor.server.Server  - Request
captured: <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cpuRequest className="de.cgs.vwtools.monitor.cpu.handler.CpuRequestHandler"
xmlns="http://cgs.de/vwtools/monitor/cpu/handler"><retreiveSnapshot
xmlns=""><dateTo>1136976475190</dateTo></retreiveSnapshot></cpuRequest>
15735 [Thread-2] DEBUG de.cgs.vwtools.monitor.server.RequestDispatcher
 - Dispatching request to
de.cgs.vwtools.monitor.cpu.handler.CpuRequestHandler
15735 [Thread-2] DEBUG
de.cgs.vwtools.monitor.cpu.handler.CpuRequestHandler  - Processing
request<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cpuRequest className="de.cgs.vwtools.monitor.cpu.handler.CpuRequestHandler"
xmlns="http://cgs.de/vwtools/monitor/cpu/handler"><retreiveSnapshot
xmlns=""><dateTo>1136976475190</dateTo></retreiveSnapshot></cpuRequest>
15781 [Thread-2] DEBUG
de.cgs.vwtools.monitor.cpu.handler.CpuRequestHandler  - Retreiving
snapshots.


Of course the client log doesn't differ too much from the unsuccessful attempt.

What I can't understand is why this startegy sometimes (rather seldom)
succeeds and most of the times fails?

And another question would of course be how can I rectify this?

When server and client both use IOUtills with reading and writing
directly to socket streams things work out rather nicely.

Thanks for any comments, questions, suggestions etc.

Cheers
Piyush

Mime
View raw message