mina-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nik...@apache.org
Subject svn commit: r641054 - in /mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck: AuthenticationHandler.java CommandDecoder.java Main.java PasswordCommand.java TapeDeckServer.java UserCommand.java
Date Tue, 25 Mar 2008 22:26:33 GMT
Author: niklas
Date: Tue Mar 25 15:26:22 2008
New Revision: 641054

URL: http://svn.apache.org/viewvc?rev=641054&view=rev
Log:
Fixed state machine example to work with recent changes.
Extended TapeDeck sample to use an IoFilter state machine (commented out for now).

Added:
    mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/AuthenticationHandler.java
    mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/PasswordCommand.java
    mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/UserCommand.java
Modified:
    mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/CommandDecoder.java
    mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/Main.java
    mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/TapeDeckServer.java

Added: mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/AuthenticationHandler.java
URL: http://svn.apache.org/viewvc/mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/AuthenticationHandler.java?rev=641054&view=auto
==============================================================================
--- mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/AuthenticationHandler.java
(added)
+++ mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/AuthenticationHandler.java
Tue Mar 25 15:26:22 2008
@@ -0,0 +1,157 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.
+ *
+ */
+package org.apache.mina.example.tapedeck;
+
+import static org.apache.mina.statemachine.event.IoFilterEvents.*;
+
+import org.apache.mina.common.IoFutureListener;
+import org.apache.mina.common.IoSession;
+import org.apache.mina.common.WriteRequest;
+import org.apache.mina.common.IoFilter.NextFilter;
+import org.apache.mina.example.tapedeck.TapeDeckServer.TapeDeckContext;
+import org.apache.mina.statemachine.StateControl;
+import org.apache.mina.statemachine.annotation.IoFilterTransition;
+import org.apache.mina.statemachine.annotation.State;
+import org.apache.mina.statemachine.context.AbstractStateContext;
+
+/**
+ *
+ * @author The Apache MINA Project (dev@mina.apache.org)
+ * @version $Rev: 600461 $, $Date: 2007-12-03 10:55:52 +0100 (Mon, 03 Dec 2007) $
+ */
+public class AuthenticationHandler {
+    @State public static final String ROOT = "Root";
+    @State(ROOT) public static final String START = "Start";
+    @State(ROOT) public static final String WAIT_USER = "WaitUser";
+    @State(ROOT) public static final String WAIT_PASSWORD = "WaitPassword";
+    @State(ROOT) public static final String DONE = "Done";
+    @State(ROOT) public static final String FAILED = "Failed";
+
+    static class AuthenticationContext extends AbstractStateContext {
+        public String user;
+        public String password;
+        public int tries = 0;
+    }
+
+    @IoFilterTransition(on = SESSION_OPENED, in = START, next = WAIT_USER)
+    public void sendAuthRequest(NextFilter nextFilter, IoSession session) {
+        session.write("+ Greetings from your tape deck!. Please authenticate yourself.");
+    }
+    
+    @IoFilterTransition(on = MESSAGE_RECEIVED, in = WAIT_USER, next = WAIT_PASSWORD)
+    public void user(AuthenticationContext context, NextFilter nextFilter, IoSession session,
UserCommand cmd) {
+        context.user = cmd.getUsername();
+        session.write("+ Give me your password (hint: try your username backwards)");
+    }
+    
+    @IoFilterTransition(on = MESSAGE_RECEIVED, in = WAIT_PASSWORD, next = DONE)
+    public void password(AuthenticationContext context, NextFilter nextFilter, IoSession
session, PasswordCommand cmd) {
+        context.password = cmd.getPassword();
+        if (context.password.equals(reverse(context.user))) {
+            session.write("+ Authentication succeeded! Your tape deck has been unlocked.");
+        } else {
+            context.tries++;
+            if (context.tries < 3) {
+                session.write("- Authentication failed! Please try again.");
+                StateControl.breakAndGotoNext(WAIT_USER);
+            } else {
+                session.write("- Authentication failed too many times! Bye bye.").addListener(IoFutureListener.CLOSE);
+                StateControl.breakAndGotoNext(FAILED);
+            }
+        }
+    }
+    
+    
+    @IoFilterTransition(on = MESSAGE_RECEIVED, in = ROOT)
+    public void quit(TapeDeckContext context, IoSession session, QuitCommand cmd) {
+        session.write("+ Bye! Please come back!").addListener(IoFutureListener.CLOSE);
+    }
+    
+    private String reverse(String s) {
+        char[] expectedPassword = new char[s.length()];
+        for (int i = 0; i < expectedPassword.length; i++) {
+            expectedPassword[i] = s.charAt(s.length() - i - 1);
+        }
+        return new String(expectedPassword);
+    }
+    
+    @IoFilterTransition(on = MESSAGE_RECEIVED, in = WAIT_USER, weight = 10)
+    public void errorWaitingForUser(IoSession session, Command cmd) {
+        if (cmd instanceof QuitCommand) {
+            StateControl.breakAndContinue();
+        }
+        session.write("Unexpected command '" + cmd.getName() + "'. Expected 'user <username>'.");
+    }
+    
+    @IoFilterTransition(on = MESSAGE_RECEIVED, in = WAIT_PASSWORD, weight = 10)
+    public void errorWaitingForPassword(IoSession session, Command cmd) {
+        if (cmd instanceof QuitCommand) {
+            StateControl.breakAndContinue();
+        }
+        session.write("Unexpected command '" + cmd.getName() + "'. Expected 'password <password>'.");
+    }
+    
+    @IoFilterTransition(on = EXCEPTION_CAUGHT, in = ROOT)
+    public void commandSyntaxError(IoSession session, CommandSyntaxException e) {
+        session.write("- " + e.getMessage());
+    }
+    
+    @IoFilterTransition(on = EXCEPTION_CAUGHT, in = ROOT, weight = 10)
+    public void exceptionCaught(IoSession session, Exception e) {
+        e.printStackTrace();
+        session.close();
+    }
+    
+//    
+//    @IoFilterTransition(on = SESSION_CREATED, in = ROOT)
+//    public void sessionCreated(NextFilter nextFilter, IoSession session) {
+//        nextFilter.sessionCreated(session);
+//    }
+//    @IoFilterTransition(on = SESSION_OPENED, in = ROOT)
+//    public void sessionOpened(NextFilter nextFilter, IoSession session) {
+//        nextFilter.sessionOpened(session);
+//    }
+    
+    @IoFilterTransition(on = SESSION_CLOSED, in = DONE)
+    public void sessionClosed(NextFilter nextFilter, IoSession session) {
+        nextFilter.sessionClosed(session);
+    }
+    @IoFilterTransition(on = EXCEPTION_CAUGHT, in = DONE)
+    public void exceptionCaught(NextFilter nextFilter, IoSession session, Throwable cause)
{
+        nextFilter.exceptionCaught(session, cause);
+    }
+    @IoFilterTransition(on = MESSAGE_RECEIVED, in = DONE)
+    public void messageReceived(NextFilter nextFilter, IoSession session, Object message)
{
+        nextFilter.messageReceived(session, message);
+    }
+    @IoFilterTransition(on = MESSAGE_SENT, in = DONE)
+    public void messageSent(NextFilter nextFilter, IoSession session, WriteRequest writeRequest)
{
+        nextFilter.messageSent(session, writeRequest);
+    }
+    
+    @IoFilterTransition(on = CLOSE, in = ROOT)
+    public void filterClose(NextFilter nextFilter, IoSession session) {
+        nextFilter.filterClose(session);
+    }
+    @IoFilterTransition(on = WRITE, in = ROOT)
+    public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest)
{
+        nextFilter.filterWrite(session, writeRequest);
+    }
+}

Modified: mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/CommandDecoder.java
URL: http://svn.apache.org/viewvc/mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/CommandDecoder.java?rev=641054&r1=641053&r2=641054&view=diff
==============================================================================
--- mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/CommandDecoder.java
(original)
+++ mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/CommandDecoder.java
Tue Mar 25 15:26:22 2008
@@ -70,6 +70,16 @@
             return new QuitCommand();
         } else if (InfoCommand.NAME.equals(cmd)) {
             return new InfoCommand();
+        } else if (UserCommand.NAME.equals(cmd)) {
+            if (arg == null) {
+                throw new CommandSyntaxException("No username specified");
+            }
+            return new UserCommand(arg);
+        } else if (PasswordCommand.NAME.equals(cmd)) {
+            if (arg == null) {
+                throw new CommandSyntaxException("No password specified");
+            }
+            return new PasswordCommand(arg);
         }
         
         throw new CommandSyntaxException("Unknown command: " + cmd);

Modified: mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/Main.java
URL: http://svn.apache.org/viewvc/mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/Main.java?rev=641054&r1=641053&r2=641054&view=diff
==============================================================================
--- mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/Main.java (original)
+++ mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/Main.java Tue Mar 25
15:26:22 2008
@@ -21,12 +21,15 @@
 
 import java.net.InetSocketAddress;
 
+import org.apache.mina.common.IoFilter;
 import org.apache.mina.common.IoHandler;
 import org.apache.mina.filter.codec.ProtocolCodecFilter;
 import org.apache.mina.filter.codec.textline.TextLineEncoder;
+import org.apache.mina.filter.logging.LoggingFilter;
 import org.apache.mina.statemachine.StateMachine;
 import org.apache.mina.statemachine.StateMachineFactory;
-import org.apache.mina.statemachine.StateMachineProxyFactory;
+import org.apache.mina.statemachine.StateMachineProxyBuilder;
+import org.apache.mina.statemachine.annotation.IoFilterTransition;
 import org.apache.mina.statemachine.annotation.IoHandlerTransition;
 import org.apache.mina.statemachine.context.IoSessionStateContextLookup;
 import org.apache.mina.statemachine.context.StateContext;
@@ -46,15 +49,30 @@
     private static final int PORT = 12345;
     
     private static IoHandler createIoHandler() {
-        StateMachine sm = StateMachineFactory.getInstance(IoHandlerTransition.class)
-                                .create(TapeDeckServer.EMPTY, new TapeDeckServer());
-        
-        return StateMachineProxyFactory.create(IoHandler.class, sm, 
+        StateMachine sm = StateMachineFactory.getInstance(
+                IoHandlerTransition.class).create(TapeDeckServer.EMPTY,
+                new TapeDeckServer());
+
+        return new StateMachineProxyBuilder().setStateContextLookup(
                 new IoSessionStateContextLookup(new StateContextFactory() {
                     public StateContext create() {
                         return new TapeDeckServer.TapeDeckContext();
                     }
-                }));
+                })).create(IoHandler.class, sm);
+    }
+    
+    private static IoFilter createAuthenticationIoFilter() {
+        StateMachine sm = StateMachineFactory.getInstance(
+                IoFilterTransition.class).create(AuthenticationHandler.START,
+                new AuthenticationHandler());
+
+        return new StateMachineProxyBuilder().setStateContextLookup(
+                new IoSessionStateContextLookup(new StateContextFactory() {
+                    public StateContext create() {
+                        return new AuthenticationHandler.AuthenticationContext();
+                    }
+                }, "authContext")).setIgnoreUnhandledEvents(true).setIgnoreStateContextLookupFailure(true).create(
+                IoFilter.class, sm);
     }
     
     public static void main(String[] args) throws Exception {
@@ -62,7 +80,10 @@
         acceptor.setReuseAddress(true);
         ProtocolCodecFilter pcf = new ProtocolCodecFilter(
                 new TextLineEncoder(), new CommandDecoder());
+        acceptor.getFilterChain().addLast("log1", new LoggingFilter("log1"));
         acceptor.getFilterChain().addLast("codec", pcf);
+//        acceptor.getFilterChain().addLast("authentication", createAuthenticationIoFilter());
+        acceptor.getFilterChain().addLast("log2", new LoggingFilter("log2"));
         acceptor.setHandler(createIoHandler());
         acceptor.bind(new InetSocketAddress(PORT));
     }

Added: mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/PasswordCommand.java
URL: http://svn.apache.org/viewvc/mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/PasswordCommand.java?rev=641054&view=auto
==============================================================================
--- mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/PasswordCommand.java
(added)
+++ mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/PasswordCommand.java
Tue Mar 25 15:26:22 2008
@@ -0,0 +1,45 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.
+ *
+ */
+package org.apache.mina.example.tapedeck;
+
+/**
+ * Represents the <code>password &lt;password&gt;</code> command.
+ *
+ * @author The Apache MINA Project (dev@mina.apache.org)
+ * @version $Rev: 587133 $, $Date: 2007-10-22 17:54:15 +0200 (Mon, 22 Oct 2007) $
+ */
+public class PasswordCommand extends Command {
+    public static final String NAME = "password";
+
+    private final String password;
+    
+    public PasswordCommand(String password) {
+        this.password = password;
+    }
+
+    public String getPassword() {
+        return password;
+    }
+    
+    @Override
+    public String getName() {
+        return NAME;
+    }
+}

Modified: mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/TapeDeckServer.java
URL: http://svn.apache.org/viewvc/mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/TapeDeckServer.java?rev=641054&r1=641053&r2=641054&view=diff
==============================================================================
    (empty)

Added: mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/UserCommand.java
URL: http://svn.apache.org/viewvc/mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/UserCommand.java?rev=641054&view=auto
==============================================================================
--- mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/UserCommand.java (added)
+++ mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/UserCommand.java Tue
Mar 25 15:26:22 2008
@@ -0,0 +1,45 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.
+ *
+ */
+package org.apache.mina.example.tapedeck;
+
+/**
+ * Represents the <code>user &lt;username&gt;</code> command.
+ *
+ * @author The Apache MINA Project (dev@mina.apache.org)
+ * @version $Rev: 587133 $, $Date: 2007-10-22 17:54:15 +0200 (Mon, 22 Oct 2007) $
+ */
+public class UserCommand extends Command {
+    public static final String NAME = "user";
+
+    private final String username;
+    
+    public UserCommand(String username) {
+        this.username = username;
+    }
+
+    public String getUsername() {
+        return username;
+    }
+    
+    @Override
+    public String getName() {
+        return NAME;
+    }
+}



Mime
View raw message