directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r123881 - in incubator/directory/network/trunk/mina: . src/examples/org/apache/mina/examples/echoserver src/examples/org/apache/mina/examples/netcat src/examples/org/apache/mina/examples/reverser src/examples/org/apache/mina/examples/sumup src/examples/org/apache/mina/examples/tennis src/java/org/apache/mina/io/socket src/java/org/apache/mina/protocol/codec xdocs
Date Sun, 02 Jan 2005 04:48:07 GMT
Author: trustin
Date: Sat Jan  1 20:48:05 2005
New Revision: 123881

URL: http://svn.apache.org/viewcvs?view=rev&rev=123881
Log:
 * Added documentation for protocol.codec package
 * Added example JavaDoc reports
 * Arranged navigation
 * Fixed: sessionIdle is fired too soon
Added:
   incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/
   incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/AbstractMessage.java
  (contents, props changed)
   incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/AddMessage.java
  (contents, props changed)
   incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/Client.java
  (contents, props changed)
   incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/ClientProtocolProvider.java
  (contents, props changed)
   incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/ClientSessionHandler.java
  (contents, props changed)
   incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/Constants.java
  (contents, props changed)
   incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/ResultMessage.java
  (contents, props changed)
   incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/Server.java
  (contents, props changed)
   incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/ServerProtocolProvider.java
  (contents, props changed)
   incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/ServerSessionHandler.java
  (contents, props changed)
   incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/SumUpMessageRecognizer.java
  (contents, props changed)
   incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/package.html
  (contents, props changed)
   incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/codec/package.html
  (contents, props changed)
Modified:
   incubator/directory/network/trunk/mina/maven.xml
   incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/echoserver/package.html
   incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/netcat/package.html
   incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/package.html
   incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/tennis/package.html
   incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketIoProcessor.java
   incubator/directory/network/trunk/mina/xdocs/index.xml
   incubator/directory/network/trunk/mina/xdocs/navigation.xml

Modified: incubator/directory/network/trunk/mina/maven.xml
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/maven.xml?view=diff&rev=123881&p1=incubator/directory/network/trunk/mina/maven.xml&r1=123880&p2=incubator/directory/network/trunk/mina/maven.xml&r2=123881
==============================================================================
--- incubator/directory/network/trunk/mina/maven.xml	(original)
+++ incubator/directory/network/trunk/mina/maven.xml	Sat Jan  1 20:48:05 2005
@@ -22,6 +22,22 @@
 		<maven:addPath id="maven.compile.src.set" refid="my.other.src.dir"/>
 	</preGoal>
 
+	<postGoal name="maven-javadoc-plugin:register">
+		<doc:registerReport 
+			name="Example JavaDocs" 
+			pluginName="maven-javadoc-plugin"
+			link="apidocs-examples/index"
+			target="_blank"
+			description="Example JavaDoc API documentation."/>
+	</postGoal>
+
+	<postGoal name="maven-javadoc-plugin:report">
+		<ant:javadoc
+			packagenames="org.apache.mina.examples.*"
+			sourcepath="${basedir}/src/examples"
+			destdir="${maven.docs.dest}/apidocs-examples" />
+	</postGoal>
+
 	<postGoal name="maven-jxr-plugin:register">
 		<doc:registerReport 
 			name="Examples Xref" 
@@ -30,7 +46,7 @@
 			target="_blank"
 			description="A set of browsable cross-referenced example sources."/>
 	</postGoal>
-
+	
 	<postGoal name="maven-jxr-plugin:report">
 		<maven:pluginVar var="javadocDestdir" plugin='maven-javadoc-plugin' property='maven.javadoc.destdir'/>
 		<maven:pluginVar var="stylesheetPath" plugin='maven-jxr-plugin' property='maven.jxr.stylesheet'/>

Modified: incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/echoserver/package.html
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/echoserver/package.html?view=diff&rev=123881&p1=incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/echoserver/package.html&r1=123880&p2=incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/echoserver/package.html&r2=123881
==============================================================================
--- incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/echoserver/package.html
(original)
+++ incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/echoserver/package.html
Sat Jan  1 20:48:05 2005
@@ -3,6 +3,6 @@
 <head>
 </head>
 <body>
-Echo server.
+Echo server which demonstates low-level I/O layer.
 </body>
 </html>

Modified: incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/netcat/package.html
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/netcat/package.html?view=diff&rev=123881&p1=incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/netcat/package.html&r1=123880&p2=incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/netcat/package.html&r2=123881
==============================================================================
--- incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/netcat/package.html
(original)
+++ incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/netcat/package.html
Sat Jan  1 20:48:05 2005
@@ -3,6 +3,7 @@
 <head>
 </head>
 <body>
-NetCat client (Network + Unix <code>cat</code> command).
+NetCat client (Network + Unix <code>cat</code> command) which demonstates low-level
I/O layer.
+</body>
 </body>
 </html>

Modified: incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/package.html
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/package.html?view=diff&rev=123881&p1=incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/package.html&r1=123880&p2=incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/package.html&r2=123881
==============================================================================
--- incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/package.html
(original)
+++ incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/package.html
Sat Jan  1 20:48:05 2005
@@ -3,6 +3,7 @@
 <head>
 </head>
 <body>
-Reverser server which reverses all text lines.
+Reverser server which reverses all text lines demonstating high-level protocol layer.
+</body>
 </body>
 </html>

Added: incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/AbstractMessage.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/AbstractMessage.java?view=auto&rev=123881
==============================================================================
--- (empty file)
+++ incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/AbstractMessage.java
Sat Jan  1 20:48:05 2005
@@ -0,0 +1,102 @@
+/*
+ * @(#) $Id$
+ */
+package org.apache.mina.examples.sumup;
+
+import java.nio.ByteBuffer;
+
+import net.gleamynode.netty2.Message;
+import net.gleamynode.netty2.MessageParseException;
+
+/**
+ * A base message for SumUp protocol messages.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public abstract class AbstractMessage implements Message {
+
+	private final int type;
+
+	private int sequence;
+
+	private boolean readHeader;
+
+	private boolean wroteHeader;
+
+	protected AbstractMessage(int type) {
+		this.type = type;
+	}
+
+	public int getSequence() {
+		return sequence;
+	}
+
+	public void setSequence(int sequence) {
+		this.sequence = sequence;
+	}
+
+	public final boolean read(ByteBuffer buf) throws MessageParseException {
+		// read a header if not read yet.
+		if (!readHeader) {
+			readHeader = readHeader(buf);
+			if (!readHeader)
+				return false;
+		}
+
+		// Header is read, now try to read body
+		if (readBody(buf)) {
+			// finished reading single complete message
+			readHeader = false; // reset state
+			return true;
+		} else
+			return false;
+	}
+
+	private boolean readHeader(ByteBuffer buf) throws MessageParseException {
+		// if header is not fully read, don't read it.
+		if (buf.remaining() < Constants.HEADER_LEN)
+			return false;
+
+		// read header and validate the message
+		int readType = buf.getShort();
+		if (type != readType)
+			throw new MessageParseException("type mismatches: " + readType
+					+ " (expected: " + type + ')');
+
+		// read sequence number of the message
+		sequence = buf.getInt();
+		return true;
+	}
+
+	protected abstract boolean readBody(ByteBuffer buf)
+			throws MessageParseException;
+
+	public boolean write(ByteBuffer buf) {
+		// write a header if not written yet.
+		if (!wroteHeader) {
+			wroteHeader = writeHeader(buf);
+			if (!wroteHeader)
+				return false; // buffer is almost full perhaps
+		}
+
+		// Header is written, now try to write body
+		if (writeBody(buf)) {
+			// finished writing single complete message
+			wroteHeader = false;
+			return true;
+		} else {
+			return false;
+		}
+	}
+
+	private boolean writeHeader(ByteBuffer buf) {
+		// check if there is enough space to write header
+		if (buf.remaining() < Constants.HEADER_LEN) return false;
+		buf.putShort((short) type);
+		buf.putInt(sequence);
+		return true;
+	}
+
+	protected abstract boolean writeBody(ByteBuffer buf);
+}
\ No newline at end of file

Added: incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/AddMessage.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/AddMessage.java?view=auto&rev=123881
==============================================================================
--- (empty file)
+++ incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/AddMessage.java
Sat Jan  1 20:48:05 2005
@@ -0,0 +1,53 @@
+/*
+ * @(#) $Id$
+ */
+package org.apache.mina.examples.sumup;
+
+import java.nio.ByteBuffer;
+
+import net.gleamynode.netty2.MessageParseException;
+
+/**
+ * <code>ADD</code> message in SumUp protocol.
+ *
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public class AddMessage extends AbstractMessage {
+	
+	private int value;
+
+	protected AddMessage() {
+		super(Constants.ADD);
+	}
+	
+	public int getValue() {
+		return value;
+	}
+	
+	public void setValue(int value) {
+		this.value = value;
+	}
+
+	protected boolean readBody(ByteBuffer buf) throws MessageParseException {
+		// don't read body if it is partially readable
+		if (buf.remaining() < Constants.ADD_BODY_LEN) return false;
+		value = buf.getInt();
+		return true;
+	}
+
+	protected boolean writeBody(ByteBuffer buf) {
+		// check if there is enough space to write body
+		if (buf.remaining() < Constants.ADD_BODY_LEN)
+			return false;
+		
+		buf.putInt(value);
+		
+		return true;
+	}
+	
+	public String toString() {
+		// it is a good practice to create toString() method on message classes.
+		return getSequence() + ":ADD(" + value + ')';
+	}
+}

Added: incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/Client.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/Client.java?view=auto&rev=123881
==============================================================================
--- (empty file)
+++ incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/Client.java
Sat Jan  1 20:48:05 2005
@@ -0,0 +1,77 @@
+/*
+ * @(#) $Id$
+ */
+package org.apache.mina.examples.sumup;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+
+import org.apache.mina.io.Connector;
+import org.apache.mina.io.IoSession;
+import org.apache.mina.io.filter.IoThreadPoolFilter;
+import org.apache.mina.io.socket.SocketConnector;
+import org.apache.mina.protocol.IoAdapter;
+import org.apache.mina.protocol.ProtocolProvider;
+import org.apache.mina.protocol.filter.ProtocolThreadPoolFilter;
+
+/**
+ * (<strong>Entry Point</strong>) Starts SumUp client.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public class Client {
+	private static final String HOSTNAME = "localhost";
+	private static final int PORT = 8080;
+	private static final int CONNECT_TIMEOUT = 30; // seconds
+
+	public static void main(String[] args) throws Throwable {
+	    if (args.length == 0) {
+	        System.out.println("Please specify the list of any integers");
+	        return;
+	    }
+
+		// prepare values to sum up
+		int[] values = new int[args.length];
+		for (int i = 0; i < args.length; i++) {
+			values[i] = Integer.parseInt(args[i]);
+		}
+
+        // Create I/O and Protocol thread pool filter.
+        // I/O thread pool performs encoding and decoding of messages.
+        // Protocol thread pool performs actual protocol flow.
+        IoThreadPoolFilter ioThreadPoolFilter = new IoThreadPoolFilter();
+        ProtocolThreadPoolFilter protocolThreadPoolFilter = new ProtocolThreadPoolFilter();
+
+        // and start both.
+        ioThreadPoolFilter.start();
+        protocolThreadPoolFilter.start();
+
+		Connector connector = new SocketConnector();
+		IoAdapter adapter = new IoAdapter();
+		
+		connector.addFilter(Integer.MAX_VALUE, ioThreadPoolFilter);
+		adapter.addFilter(Integer.MAX_VALUE, protocolThreadPoolFilter);
+		
+		ProtocolProvider protocolProvider = new ClientProtocolProvider(values);
+		IoSession session;
+		for ( ;; ) {
+			try {
+				session = connector.connect(new InetSocketAddress(HOSTNAME, PORT), CONNECT_TIMEOUT, adapter.adapt(protocolProvider));
+				break;
+			} catch (IOException e) {
+				System.err.println("Failed to connect.");
+				e.printStackTrace();
+				Thread.sleep(5000);
+			}
+		}
+		
+		// wait until the summation is done
+		while ( session.isConnected() ) {
+			Thread.sleep(100);
+		}
+		
+		ioThreadPoolFilter.stop();
+		protocolThreadPoolFilter.stop();
+	}
+}
\ No newline at end of file

Added: incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/ClientProtocolProvider.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/ClientProtocolProvider.java?view=auto&rev=123881
==============================================================================
--- (empty file)
+++ incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/ClientProtocolProvider.java
Sat Jan  1 20:48:05 2005
@@ -0,0 +1,46 @@
+/*
+ * @(#) $Id$
+ */
+package org.apache.mina.examples.sumup;
+
+import org.apache.mina.protocol.ProtocolCodecFactory;
+import org.apache.mina.protocol.ProtocolDecoder;
+import org.apache.mina.protocol.ProtocolEncoder;
+import org.apache.mina.protocol.ProtocolHandler;
+import org.apache.mina.protocol.ProtocolProvider;
+import org.apache.mina.protocol.codec.NettyDecoder;
+import org.apache.mina.protocol.codec.NettyEncoder;
+
+/**
+ * {@link ProtocolProvider} for SumUp client.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$, 
+ */
+public class ClientProtocolProvider implements ProtocolProvider {
+
+	private static final ProtocolCodecFactory CODEC_FACTORY = new ProtocolCodecFactory() {
+
+		public ProtocolEncoder newEncoder() {
+			return new NettyEncoder();
+		}
+
+		public ProtocolDecoder newDecoder() {
+			return new NettyDecoder(new SumUpMessageRecognizer(SumUpMessageRecognizer.CLIENT_MODE));
+		}
+	};
+	
+	private final ProtocolHandler handler;
+	
+	public ClientProtocolProvider(int[] values) {
+		handler = new ClientSessionHandler(values);
+	}
+
+	public ProtocolCodecFactory getCodecFactory() {
+		return CODEC_FACTORY;
+	}
+
+	public ProtocolHandler getHandler() {
+		return handler;
+	}
+}

Added: incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/ClientSessionHandler.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/ClientSessionHandler.java?view=auto&rev=123881
==============================================================================
--- (empty file)
+++ incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/ClientSessionHandler.java
Sat Jan  1 20:48:05 2005
@@ -0,0 +1,79 @@
+/*
+ * @(#) $Id$
+ */
+package org.apache.mina.examples.sumup;
+
+import org.apache.mina.common.IdleStatus;
+import org.apache.mina.protocol.ProtocolHandler;
+import org.apache.mina.protocol.ProtocolSession;
+
+/**
+ * {@link ProtocolHandler} for SumUp client.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public class ClientSessionHandler implements ProtocolHandler {
+	private final int[] values;
+	private boolean complete;
+
+	public ClientSessionHandler(int[] values) {
+		this.values = values;
+	}
+
+	public boolean isComplete() {
+		return complete;
+	}
+
+	public void sessionOpened(ProtocolSession session) {
+		System.out.println("OPENED");
+		// send summation requests
+		for (int i = 0; i < values.length; i++) {
+			AddMessage m = new AddMessage();
+			m.setSequence(i);
+			m.setValue(values[i]);
+			session.write(m);
+		}
+	}
+
+	public void sessionClosed(ProtocolSession session) {
+		System.out.println("CLOSED");
+	}
+
+	public void messageReceived(ProtocolSession session, Object message) {
+		System.out.println("RCVD: " + message);
+		// server only sends ResultMessage. otherwise, we will have to identify
+		// its type using instanceof operator.
+		ResultMessage rm = (ResultMessage) message;
+		if (rm.isOk()) {
+			// server returned OK code.
+			// if received the result message which has the last sequence
+			// number,
+			// it is time to disconnect.
+			if (rm.getSequence() == values.length - 1) {
+				// print the sum and disconnect.
+				System.out.println("The sum: " + rm.getValue());
+				session.close();
+				complete = true;
+			}
+		} else {
+			// seever returned error code because of overflow, etc.
+			System.err.println("Server error, disconnecting...");
+			session.close();
+			complete = true;
+		}
+	}
+
+	public void messageSent(ProtocolSession session, Object message) {
+		System.out.println("SENT: " + message);
+	}
+
+	public void sessionIdle(ProtocolSession session, IdleStatus status) {
+		// there is no idle time for client
+	}
+
+	public void exceptionCaught(ProtocolSession session, Throwable cause) {
+		cause.printStackTrace();
+		session.close();
+	}
+}
\ No newline at end of file

Added: incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/Constants.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/Constants.java?view=auto&rev=123881
==============================================================================
--- (empty file)
+++ incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/Constants.java
Sat Jan  1 20:48:05 2005
@@ -0,0 +1,31 @@
+/*
+ * @(#) $Id$
+ */
+package org.apache.mina.examples.sumup;
+
+/**
+ * Contains SumUp protocol constants.
+ *
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public class Constants {
+	public static final int TYPE_LEN = 2;
+	public static final int SEQUENCE_LEN = 4;
+	public static final int HEADER_LEN = TYPE_LEN + SEQUENCE_LEN;
+	public static final int BODY_LEN = 12;
+	
+	public static final int RESULT = 0;
+	public static final int ADD = 1;
+	
+	public static final int RESULT_CODE_LEN = 2;
+	public static final int RESULT_VALUE_LEN = 4;
+	public static final int ADD_BODY_LEN = 4;
+	
+	public static final int RESULT_OK = 0;
+	public static final int RESULT_ERROR = 1;
+	
+	private Constants() {
+	}
+
+}

Added: incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/ResultMessage.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/ResultMessage.java?view=auto&rev=123881
==============================================================================
--- (empty file)
+++ incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/ResultMessage.java
Sat Jan  1 20:48:05 2005
@@ -0,0 +1,100 @@
+/*
+ * @(#) $Id$
+ */
+package org.apache.mina.examples.sumup;
+
+import java.nio.ByteBuffer;
+
+import net.gleamynode.netty2.MessageParseException;
+
+/**
+ * <code>RESULT</code> message in SumUp protocol.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public class ResultMessage extends AbstractMessage {
+
+	private boolean ok;
+
+	private int value;
+
+	private boolean processedResultCode;
+
+	public ResultMessage() {
+		super(Constants.RESULT);
+	}
+
+	public boolean isOk() {
+		return ok;
+	}
+
+	public void setOk(boolean ok) {
+		this.ok = ok;
+	}
+
+	public int getValue() {
+		return value;
+	}
+
+	public void setValue(int value) {
+		this.value = value;
+	}
+
+	protected boolean readBody(ByteBuffer buf) throws MessageParseException {
+		if (!processedResultCode) {
+			processedResultCode = readResultCode(buf);
+			if (!processedResultCode)
+				return false;
+		}
+
+		if (ok) {
+			if (readValue(buf)) {
+				processedResultCode = false;
+				return true;
+			} else
+				return false;
+		} else {
+			processedResultCode = false;
+			return true;
+		}
+	}
+
+	private boolean readResultCode(ByteBuffer buf) {
+		if (buf.remaining() < Constants.RESULT_CODE_LEN)
+			return false;
+		ok = buf.getShort() == Constants.RESULT_OK;
+		return true;
+	}
+
+	private boolean readValue(ByteBuffer buf) {
+		if (buf.remaining() < Constants.RESULT_VALUE_LEN)
+			return false;
+		value = buf.getInt();
+		return true;
+
+	}
+
+	protected boolean writeBody(ByteBuffer buf) {
+		// check if there is enough space to write body
+		if (buf.remaining() < Constants.RESULT_CODE_LEN
+				+ Constants.RESULT_VALUE_LEN)
+			return false;
+		
+		buf
+				.putShort((short) (ok ? Constants.RESULT_OK
+						: Constants.RESULT_ERROR));
+		if (ok)
+			buf.putInt(value);
+		
+		return true;
+	}
+
+	public String toString() {
+		if (ok) {
+			return getSequence() + ":RESULT(" + value + ')';
+		} else {
+			return getSequence() + ":RESULT(ERROR)";
+		}
+	}
+}
\ No newline at end of file

Added: incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/Server.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/Server.java?view=auto&rev=123881
==============================================================================
--- (empty file)
+++ incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/Server.java
Sat Jan  1 20:48:05 2005
@@ -0,0 +1,43 @@
+/*
+ * @(#) $Id$
+ */
+package org.apache.mina.examples.sumup;
+
+import java.net.InetSocketAddress;
+
+import org.apache.mina.io.Acceptor;
+import org.apache.mina.io.filter.IoThreadPoolFilter;
+import org.apache.mina.io.socket.SocketAcceptor;
+import org.apache.mina.protocol.IoAdapter;
+import org.apache.mina.protocol.filter.ProtocolThreadPoolFilter;
+
+/**
+ * (<strong>Entry Point</strong>) Starts SumUp server.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public class Server {
+	private static final int SERVER_PORT = 8080;
+
+	public static void main(String[] args) throws Throwable {
+        // Create I/O and Protocol thread pool filter.
+        // I/O thread pool performs encoding and decoding of messages.
+        // Protocol thread pool performs actual protocol flow.
+        IoThreadPoolFilter ioThreadPoolFilter = new IoThreadPoolFilter();
+        ProtocolThreadPoolFilter protocolThreadPoolFilter = new ProtocolThreadPoolFilter();
+
+        // and start both.
+        ioThreadPoolFilter.start();
+        protocolThreadPoolFilter.start();
+
+		Acceptor acceptor = new SocketAcceptor();
+		IoAdapter adapter = new IoAdapter();
+		
+		acceptor.addFilter(Integer.MAX_VALUE, ioThreadPoolFilter);
+		adapter.addFilter(Integer.MAX_VALUE, protocolThreadPoolFilter);
+
+		acceptor.bind(new InetSocketAddress(SERVER_PORT), adapter.adapt(new ServerProtocolProvider()));
+		System.out.println("Listening on port " + SERVER_PORT);
+	}
+}
\ No newline at end of file

Added: incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/ServerProtocolProvider.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/ServerProtocolProvider.java?view=auto&rev=123881
==============================================================================
--- (empty file)
+++ incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/ServerProtocolProvider.java
Sat Jan  1 20:48:05 2005
@@ -0,0 +1,45 @@
+/*
+ * @(#) $Id$
+ */
+package org.apache.mina.examples.sumup;
+
+import org.apache.mina.protocol.ProtocolCodecFactory;
+import org.apache.mina.protocol.ProtocolDecoder;
+import org.apache.mina.protocol.ProtocolEncoder;
+import org.apache.mina.protocol.ProtocolHandler;
+import org.apache.mina.protocol.ProtocolProvider;
+import org.apache.mina.protocol.codec.NettyDecoder;
+import org.apache.mina.protocol.codec.NettyEncoder;
+
+/**
+ * {@link ProtocolProvider} for SumUp server.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$, 
+ */
+public class ServerProtocolProvider implements ProtocolProvider {
+
+	private static final ProtocolCodecFactory CODEC_FACTORY = new ProtocolCodecFactory() {
+
+		public ProtocolEncoder newEncoder() {
+			return new NettyEncoder();
+		}
+
+		public ProtocolDecoder newDecoder() {
+			return new NettyDecoder(new SumUpMessageRecognizer(SumUpMessageRecognizer.SERVER_MODE));
+		}
+	};
+	
+	private static final ProtocolHandler HANDLER = new ServerSessionHandler();
+	
+	public ServerProtocolProvider() {
+	}
+
+	public ProtocolCodecFactory getCodecFactory() {
+		return CODEC_FACTORY;
+	}
+
+	public ProtocolHandler getHandler() {
+		return HANDLER;
+	}
+}

Added: incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/ServerSessionHandler.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/ServerSessionHandler.java?view=auto&rev=123881
==============================================================================
--- (empty file)
+++ incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/ServerSessionHandler.java
Sat Jan  1 20:48:05 2005
@@ -0,0 +1,81 @@
+/*
+ * @(#) $Id$
+ */
+package org.apache.mina.examples.sumup;
+
+import net.gleamynode.netty2.SessionListener;
+
+import org.apache.mina.common.IdleStatus;
+import org.apache.mina.protocol.ProtocolHandler;
+import org.apache.mina.protocol.ProtocolSession;
+
+/**
+ * {@link SessionListener}for SumUp server.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public class ServerSessionHandler implements ProtocolHandler {
+
+	public ServerSessionHandler() {
+	}
+
+	public void sessionOpened(ProtocolSession session) {
+		System.out.println("OPENED");
+		// set idle time to 60 seconds
+		session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 60);
+
+		// initial sum is zero
+		session.setAttachment(new Integer(0));
+	}
+
+	public void sessionClosed(ProtocolSession session) {
+		System.out.println("CLOSED");
+	}
+
+	public void messageReceived(ProtocolSession session, Object message) {
+		System.out.println("RCVD: " + message);
+		// client only sends AddMessage. otherwise, we will have to identify
+		// its type using instanceof operator.
+		AddMessage am = (AddMessage) message;
+
+		// add the value to the current sum.
+		int sum = ((Integer) session.getAttachment()).intValue();
+		int value = am.getValue();
+		long expectedSum = (long) sum + value;
+		if (expectedSum > Integer.MAX_VALUE || expectedSum < Integer.MIN_VALUE) {
+			// if the sum overflows or underflows, return error message
+			ResultMessage rm = new ResultMessage();
+			rm.setSequence(am.getSequence()); // copy sequence
+			rm.setOk(false);
+			session.write(rm);
+		} else {
+			// sum up
+			sum = (int) expectedSum;
+			session.setAttachment(new Integer(sum));
+
+			// return the result message
+			ResultMessage rm = new ResultMessage();
+			rm.setSequence(am.getSequence()); // copy sequence
+			rm.setOk(true);
+			rm.setValue(sum);
+			session.write(rm);
+		}
+	}
+
+	public void messageSent(ProtocolSession session, Object message) {
+		System.out.println("SENT: " + message);
+	}
+
+	public void sessionIdle(ProtocolSession session, IdleStatus status) {
+		System.out.println("Disconnecting the idle.");
+		// disconnect an idle client
+		session.close();
+	}
+
+	public void exceptionCaught(ProtocolSession session, Throwable cause) {
+		cause.printStackTrace();
+		// close the connection on exceptional situation
+		session.close();
+	}
+}
\ No newline at end of file

Added: incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/SumUpMessageRecognizer.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/SumUpMessageRecognizer.java?view=auto&rev=123881
==============================================================================
--- (empty file)
+++ incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/SumUpMessageRecognizer.java
Sat Jan  1 20:48:05 2005
@@ -0,0 +1,66 @@
+/*
+ * @(#) $Id$
+ */
+package org.apache.mina.examples.sumup;
+
+import java.nio.ByteBuffer;
+
+import net.gleamynode.netty2.Message;
+import net.gleamynode.netty2.MessageParseException;
+import net.gleamynode.netty2.MessageRecognizer;
+
+/**
+ * Recognizes SumUp protocol messages. Works both in server mode and client
+ * mode. Sesver will receive only <code>ADD</code> message, and client will
+ * receive only <code>RESULT</code> message.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public class SumUpMessageRecognizer implements MessageRecognizer {
+
+	public static final int CLIENT_MODE = 1;
+
+	public static final int SERVER_MODE = 2;
+
+	private int mode;
+
+	public SumUpMessageRecognizer(int mode) {
+		switch (mode) {
+		case CLIENT_MODE:
+		case SERVER_MODE:
+			this.mode = mode;
+			break;
+		default:
+			throw new IllegalArgumentException("invalid mode: " + mode);
+		}
+	}
+
+	public Message recognize(ByteBuffer buf) throws MessageParseException {
+		// return null if message type is not arrived yet.
+		if (buf.remaining() < Constants.TYPE_LEN)
+			return null;
+		
+		int type = buf.getShort();
+		switch (mode) {
+		// server can receive ADD message only.
+		case SERVER_MODE:
+			switch (type) {
+			case Constants.ADD:
+				return new AddMessage();
+			default:
+				throw new MessageParseException("unknown type: " + type);
+			}
+		// client can receive RESULT message only.
+		case CLIENT_MODE:
+			switch (type) {
+			case Constants.RESULT:
+				return new ResultMessage();
+			default:
+				throw new MessageParseException("unknown type: " + type);
+			}
+		default:
+			throw new InternalError(); // this cannot happen
+		}
+	}
+}
\ No newline at end of file

Added: incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/package.html
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/package.html?view=auto&rev=123881
==============================================================================
--- (empty file)
+++ incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/sumup/package.html
Sat Jan  1 20:48:05 2005
@@ -0,0 +1,10 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
+<html>
+<head>
+</head>
+<body>
+A SumUp client/server example migrated from <a href="http://gleamynode.net/dev/projects/netty2-example-sumup/">Netty
SumUp example</a>.
+
+You'll learn how to migrate from Netty to MINA with this example.
+</body>
+</html>

Modified: incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/tennis/package.html
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/tennis/package.html?view=diff&rev=123881&p1=incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/tennis/package.html&r1=123880&p2=incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/tennis/package.html&r2=123881
==============================================================================
--- incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/tennis/package.html
(original)
+++ incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/tennis/package.html
Sat Jan  1 20:48:05 2005
@@ -3,6 +3,6 @@
 <head>
 </head>
 <body>
-Two tennis players play a game.
+Two tennis players play a game which demonstates in-VM pipes.
 </body>
 </html>

Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketIoProcessor.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketIoProcessor.java?view=diff&rev=123881&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketIoProcessor.java&r1=123880&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketIoProcessor.java&r2=123881
==============================================================================
--- incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketIoProcessor.java
(original)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketIoProcessor.java
Sat Jan  1 20:48:05 2005
@@ -269,6 +269,8 @@
         }
         catch( Throwable e )
         {
+        	if ( e instanceof IOException )
+        		scheduleRemove( session );
             session.getFilterManager().fireExceptionCaught( session, e );
         }
     }
@@ -336,7 +338,7 @@
                                     long lastIoTime )
     {
         if( idleTime > 0 && !session.isIdle( status )
-            && ( currentTime - lastIoTime ) >= idleTime )
+            && lastIoTime != 0 && ( currentTime - lastIoTime ) >= idleTime
)
         {
             session.setIdle( status, true );
             session.getFilterManager().fireSessionIdle( session, status );
@@ -383,6 +385,7 @@
             }
             catch( IOException e )
             {
+            	scheduleRemove( session );
                 session.getFilterManager().fireExceptionCaught( session, e );
             }
         }

Added: incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/codec/package.html
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/codec/package.html?view=auto&rev=123881
==============================================================================
--- (empty file)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/codec/package.html
Sat Jan  1 20:48:05 2005
@@ -0,0 +1,8 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
+<html>
+<head>
+</head>
+<body>
+<code>ProtocolEncoder</code> and <code>ProtocolDecoder</code> implementations.
+</body>
+</html>

Modified: incubator/directory/network/trunk/mina/xdocs/index.xml
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/xdocs/index.xml?view=diff&rev=123881&p1=incubator/directory/network/trunk/mina/xdocs/index.xml&r1=123880&p2=incubator/directory/network/trunk/mina/xdocs/index.xml&r2=123881
==============================================================================
--- incubator/directory/network/trunk/mina/xdocs/index.xml	(original)
+++ incubator/directory/network/trunk/mina/xdocs/index.xml	Sat Jan  1 20:48:05 2005
@@ -13,8 +13,8 @@
                 performance and high scalability network applications easily.
             </p>
             <p>
-                MINA will be a simple yet full-featured network application which
-                provides:
+                MINA will be a simple yet full-featured network application
+                which provides:
                 <ul>
                     <li>Support for TCP and UDP transport types,</li>
                     <li>Support for both server (inetd-like) and client side,</li>
@@ -22,8 +22,8 @@
                     <li>JMX managability,</li>
                     <li>Traffic throttling,</li>
                     <li>Overload shielding,</li>
-                    <li>Integration with well known containers such as Spring and OSGi,
-                        </li>
+                    <li>Integration with well known containers such as Spring
+                    	and OSGi,</li>
                 </ul>
             </p>
         </section>
@@ -31,8 +31,9 @@
         <section name="Getting Started">
             <p>
                 Tutorial is under construction.  Please start from
-                <a href="xref-examples/index.html">examples</a> referring
-                to <a href="apidocs/index.html">JavaDocs</a>.
+                <a target="_blank" href="xref-examples/index.html">examples</a>
+                referring to
+                <a target="_blank" href="apidocs/index.html">JavaDocs</a>.
                 <table>
                     <tr>
                         <th>Name</th>
@@ -40,23 +41,28 @@
                         <th>Side</th>
                     </tr>
                     <tr>
-                        <td><a href="xref-examples/org/apache/mina/examples/reverser/package-summary.html">Reverser</a></td>
+                        <td><a target="classFrame" href="xref-examples/org/apache/mina/examples/reverser/package-summary.html">Reverser</a></td>
                         <td>High-level protocol layer</td>
                         <td>Server</td>
                     </tr>
                     <tr>
-                        <td><a href="xref-examples/org/apache/mina/examples/echoserver/package-summary.html">Echo
server</a></td>
+                        <td><a target="classFrame" href="xref-examples/org/apache/mina/examples/echoserver/package-summary.html">Echo
server</a></td>
                         <td>Low-level I/O layer</td>
                         <td>Server</td>
                     </tr>
                     <tr>
-                        <td><a href="xref-examples/org/apache/mina/examples/netcat/package-summary.html">NetCat</a></td>
+                        <td><a target="classFrame" href="xref-examples/org/apache/mina/examples/netcat/package-summary.html">NetCat</a></td>
                         <td>Low-level I/O layer</td>
                         <td>Client</td>
                     </tr>
                     <tr>
-                        <td><a href="xref-examples/org/apache/mina/examples/tennis/package-summary.html">Tennis</a></td>
+                        <td><a target="classFrame" href="xref-examples/org/apache/mina/examples/tennis/package-summary.html">Tennis</a></td>
                         <td>In-VM pipe support in protocol layer.</td>
+                        <td>Both</td>
+                    </tr>
+                    <tr>
+                        <td><a target="classFrame" href="xref-examples/org/apache/mina/examples/sumup/package-summary.html">SumUp</a></td>
+                        <td>Migration from <a target="_blank" href="http://gleamynode.net/dev/projects/netty2-example-sumup/">Netty
SumUp example</a>.</td>
                         <td>Both</td>
                     </tr>
                 </table>

Modified: incubator/directory/network/trunk/mina/xdocs/navigation.xml
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/xdocs/navigation.xml?view=diff&rev=123881&p1=incubator/directory/network/trunk/mina/xdocs/navigation.xml&r1=123880&p2=incubator/directory/network/trunk/mina/xdocs/navigation.xml&r2=123881
==============================================================================
--- incubator/directory/network/trunk/mina/xdocs/navigation.xml	(original)
+++ incubator/directory/network/trunk/mina/xdocs/navigation.xml	Sat Jan  1 20:48:05 2005
@@ -27,15 +27,17 @@
 			<item name="Overview" href="/index.html"/>
 			<item name="News and Status" href="/news.html"/>
 			<item name="Roadmap" href="/roadmap.html"/>
-			<item name="JavaDocs" href="/apidocs/index.html"/>
-			<item name="Examples" href="/xref-examples/index.html"/>
 		</menu>
-
-		<menu name="Project Information">
-			<!--
-			<item name="Open Issues" href="http://nagoya.apache.org/jira/browse/DIRSEDA"/>
-			-->
-			<item name="Source Repositories" href="http://svn.apache.org/viewcvs.cgi/incubator/directory/network/trunk/mina/?root=Apache-SVN"/>
+		
+		<menu name="MINA References">
+			<item name="JavaDocs" href="/apidocs/index.html" target="_blank"/>
+			<item name="Source code" href="/xref/index.html" target="_blank"/>
+			<item name="Source code (SVN)" href="http://svn.apache.org/viewcvs.cgi/incubator/directory/network/trunk/mina/?root=Apache-SVN"
target="_blank"/>
+		</menu>
+		
+		<menu name="MINA Examples">
+			<item name="JavaDocs" href="/apidocs-examples/index.html" target="_blank"/>
+			<item name="Source code" href="/xref-examples/index.html" target="_blank"/>
 		</menu>
 	</body>
 </project>

Mime
View raw message