You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by tr...@apache.org on 2004/12/08 13:50:39 UTC

svn commit: r111267 - incubator/directory/seda/branches/trustin/src/examples/org/apache/mina/examples/reverser

Author: trustin
Date: Wed Dec  8 04:50:37 2004
New Revision: 111267

URL: http://svn.apache.org/viewcvs?view=rev&rev=111267
Log:
 * Added 'reverser' example server which demonstrates protocol layer.
 * Implemented protocol layer (see FIXME)
Added:
   incubator/directory/seda/branches/trustin/src/examples/org/apache/mina/examples/reverser/
   incubator/directory/seda/branches/trustin/src/examples/org/apache/mina/examples/reverser/Main.java   (contents, props changed)
   incubator/directory/seda/branches/trustin/src/examples/org/apache/mina/examples/reverser/ReverseProtocolProvider.java   (contents, props changed)
   incubator/directory/seda/branches/trustin/src/examples/org/apache/mina/examples/reverser/ReverseProtocolSessionHandler.java   (contents, props changed)
   incubator/directory/seda/branches/trustin/src/examples/org/apache/mina/examples/reverser/TextLineCodec.java   (contents, props changed)

Added: incubator/directory/seda/branches/trustin/src/examples/org/apache/mina/examples/reverser/Main.java
Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/trustin/src/examples/org/apache/mina/examples/reverser/Main.java?view=auto&rev=111267
==============================================================================
--- (empty file)
+++ incubator/directory/seda/branches/trustin/src/examples/org/apache/mina/examples/reverser/Main.java	Wed Dec  8 04:50:37 2004
@@ -0,0 +1,44 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+/*
+ * @(#) $Id$
+ */
+package org.apache.mina.examples.reverser;
+
+import java.net.InetSocketAddress;
+
+import org.apache.mina.core.Acceptor;
+import org.apache.mina.core.socket.TcpAcceptor;
+import org.apache.mina.protocol.CoreAdapter;
+
+
+/**
+ * TODO Document me.
+ *
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$,
+ */
+public class Main {
+    private static final int PORT = 8080;
+
+    public static void main(String[] args) throws Exception {
+        Acceptor acceptor = new TcpAcceptor();
+        acceptor.bind(new InetSocketAddress(PORT),
+                      CoreAdapter.adapt(new ReverseProtocolProvider()));
+        System.out.println("Listening on port " + PORT);
+    }
+}

Added: incubator/directory/seda/branches/trustin/src/examples/org/apache/mina/examples/reverser/ReverseProtocolProvider.java
Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/trustin/src/examples/org/apache/mina/examples/reverser/ReverseProtocolProvider.java?view=auto&rev=111267
==============================================================================
--- (empty file)
+++ incubator/directory/seda/branches/trustin/src/examples/org/apache/mina/examples/reverser/ReverseProtocolProvider.java	Wed Dec  8 04:50:37 2004
@@ -0,0 +1,28 @@
+/*
+ * @(#) $Id$
+ */
+package org.apache.mina.examples.reverser;
+
+import org.apache.mina.protocol.ProtocolCodec;
+import org.apache.mina.protocol.ProtocolProvider;
+import org.apache.mina.protocol.ProtocolSessionHandler;
+
+/**
+ * TODO Document me.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$, 
+ */
+public class ReverseProtocolProvider implements ProtocolProvider {
+	
+	private static ProtocolSessionHandler HANDLER = new ReverseProtocolSessionHandler();
+
+	public ProtocolCodec newCodec() {
+		return new TextLineCodec();
+	}
+
+	public ProtocolSessionHandler getHandler() {
+		return HANDLER;
+	}
+
+}

Added: incubator/directory/seda/branches/trustin/src/examples/org/apache/mina/examples/reverser/ReverseProtocolSessionHandler.java
Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/trustin/src/examples/org/apache/mina/examples/reverser/ReverseProtocolSessionHandler.java?view=auto&rev=111267
==============================================================================
--- (empty file)
+++ incubator/directory/seda/branches/trustin/src/examples/org/apache/mina/examples/reverser/ReverseProtocolSessionHandler.java	Wed Dec  8 04:50:37 2004
@@ -0,0 +1,51 @@
+/*
+ * @(#) $Id$
+ */
+package org.apache.mina.examples.reverser;
+
+import org.apache.mina.core.IdleStatus;
+import org.apache.mina.protocol.ProtocolSession;
+import org.apache.mina.protocol.ProtocolSessionHandler;
+
+/**
+ * TODO Document me.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$, 
+ */
+public class ReverseProtocolSessionHandler implements ProtocolSessionHandler {
+
+	public void sessionOpened(ProtocolSession session) {
+		System.out.println(session.getRemoteAddress() + " OPENED");
+	}
+
+	public void sessionClosed(ProtocolSession session) {
+		System.out.println(session.getRemoteAddress() + " CLOSED");
+	}
+
+	public void sessionIdle(ProtocolSession session, IdleStatus status) {
+		System.out.println(session.getRemoteAddress() + " IDLE(" + status + ")");
+	}
+
+	public void exceptionCaught(ProtocolSession session, Throwable cause) {
+		System.out.println(session.getRemoteAddress() + " EXCEPTION");
+		cause.printStackTrace(System.out);
+		session.close();
+	}
+
+	public void messageReceived(ProtocolSession session, Object message) {
+		System.out.println(session.getRemoteAddress() + " RCVD: " + message);
+		
+		String str = message.toString();
+		StringBuffer buf = new StringBuffer(str.length());
+		for (int i = str.length()-1; i >= 0; i--) {
+			buf.append(str.charAt(i));
+		}
+		
+		session.write(buf.toString());
+	}
+
+	public void messageSent(ProtocolSession session, Object message) {
+		System.out.println(session.getRemoteAddress() + " SENT: " + message);
+	}
+}

Added: incubator/directory/seda/branches/trustin/src/examples/org/apache/mina/examples/reverser/TextLineCodec.java
Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/trustin/src/examples/org/apache/mina/examples/reverser/TextLineCodec.java?view=auto&rev=111267
==============================================================================
--- (empty file)
+++ incubator/directory/seda/branches/trustin/src/examples/org/apache/mina/examples/reverser/TextLineCodec.java	Wed Dec  8 04:50:37 2004
@@ -0,0 +1,71 @@
+/*
+ * @(#) $Id$
+ */
+package org.apache.mina.examples.reverser;
+
+import org.apache.mina.core.ReadBuffer;
+import org.apache.mina.core.WriteBuffer;
+import org.apache.mina.protocol.ProtocolCodec;
+import org.apache.mina.protocol.ProtocolSession;
+import org.apache.mina.protocol.ProtocolViolationException;
+
+/**
+ * TODO Document me.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$, 
+ */
+public class TextLineCodec implements ProtocolCodec {
+	
+	private String encodeData;
+	private int encodePos;
+	private StringBuffer decodeBuf = new StringBuffer();
+
+	public boolean encode(ProtocolSession session, Object message,
+			WriteBuffer out) throws ProtocolViolationException {
+		
+		if (message != encodeData) {
+			String val = message.toString();
+			if (val.length() > 256) {
+				throw new ProtocolViolationException("Cannot encode too long string.");
+			}
+			encodeData = val + "\r\n";
+			encodePos = 0;
+		}
+		
+		for (; encodePos < encodeData.length(); encodePos++) {
+			if (out.hasRemaining()) {
+				out.put((byte) encodeData.charAt(encodePos));
+			} else {
+				return false;
+			}
+		}
+		
+		encodeData = null;
+		return true;
+	}
+
+	public Object decode(ProtocolSession session, ReadBuffer in)
+			throws ProtocolViolationException {
+		do {
+			byte b = in.get();
+			switch (b) {
+			case '\r':
+				break;
+			case '\n':
+				String result = decodeBuf.toString();
+				decodeBuf.delete(0, decodeBuf.length());
+				return result;
+			default:
+				decodeBuf.append((char) b);
+			}
+			
+			if (decodeBuf.length() > 256) {
+				decodeBuf.delete(0, decodeBuf.length());
+				throw new ProtocolViolationException("The line is too long.");
+			}
+		} while (in.hasRemaining());
+		
+		return null;
+	}
+}