You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by gn...@apache.org on 2013/01/17 14:02:32 UTC

svn commit: r1434652 - in /mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd: client/session/ClientSessionImpl.java common/Session.java common/SessionListener.java common/session/AbstractSession.java server/session/ServerSession.java

Author: gnodet
Date: Thu Jan 17 13:02:32 2013
New Revision: 1434652

URL: http://svn.apache.org/viewvc?rev=1434652&view=rev
Log:
[SSHD-203] Add session state notifications

Modified:
    mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
    mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/Session.java
    mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/SessionListener.java
    mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
    mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSession.java

Modified: mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
URL: http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java?rev=1434652&r1=1434651&r2=1434652&view=diff
==============================================================================
--- mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java (original)
+++ mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java Thu Jan 17 13:02:32 2013
@@ -62,11 +62,6 @@ import org.apache.sshd.server.channel.Op
  */
 public class ClientSessionImpl extends AbstractSession implements ClientSession {
 
-    public enum State {
-        ReceiveKexInit, Kex, ReceiveNewKeys, AuthRequestSent, WaitForAuth, UserAuth, Running, Unknown
-    }
-
-    private State state = State.ReceiveKexInit;
     private UserAuth userAuth;
     private AuthFuture authFuture;
     private final TcpipForwarder tcpipForward;
@@ -113,7 +108,7 @@ public class ClientSessionImpl extends A
             }
             authFuture = new DefaultAuthFuture(lock);
             userAuth = new UserAuthAgent(this, username);
-            setState(ClientSessionImpl.State.UserAuth);
+            setState(State.UserAuth);
 
             switch (userAuth.next(null)) {
                 case Success:
@@ -151,7 +146,7 @@ public class ClientSessionImpl extends A
             }
             authFuture = new DefaultAuthFuture(lock);
             userAuth = new UserAuthPassword(this, username, password);
-            setState(ClientSessionImpl.State.UserAuth);
+            setState(State.UserAuth);
 
             switch (userAuth.next(null)) {
                 case Success:
@@ -189,7 +184,7 @@ public class ClientSessionImpl extends A
             }
             authFuture = new DefaultAuthFuture(lock);
             userAuth = new UserAuthPublicKey(this, username, key);
-            setState(ClientSessionImpl.State.UserAuth);
+            setState(State.UserAuth);
 
             switch (userAuth.next(null)) {
                 case Success:
@@ -334,7 +329,7 @@ public class ClientSessionImpl extends A
                 log.info("Received SSH_MSG_IGNORE");
                 break;
             default:
-                switch (state) {
+                switch (getState()) {
                     case ReceiveKexInit:
                         if (cmd != SshConstants.Message.SSH_MSG_KEXINIT) {
                             log.error("Ignoring command " + cmd + " while waiting for " + SshConstants.Message.SSH_MSG_KEXINIT);
@@ -441,7 +436,7 @@ public class ClientSessionImpl extends A
                         }
                         break;
                     default:
-                        throw new IllegalStateException("Unsupported state: " + state);
+                        throw new IllegalStateException("Unsupported state: " + getState());
                 }
         }
     }
@@ -457,7 +452,7 @@ public class ClientSessionImpl extends A
                 if (authed) {
                     cond |= AUTHED;
                 }
-                if (state == State.WaitForAuth) {
+                if (getState() == State.WaitForAuth) {
                     cond |= WAIT_AUTH;
                 }
                 if ((cond & mask) != 0) {
@@ -489,7 +484,7 @@ public class ClientSessionImpl extends A
 
     public void setState(State newState) {
         synchronized (lock) {
-            this.state = newState;
+            super.setState(newState);
             lock.notifyAll();
         }
     }

Modified: mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/Session.java
URL: http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/Session.java?rev=1434652&r1=1434651&r2=1434652&view=diff
==============================================================================
--- mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/Session.java (original)
+++ mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/Session.java Thu Jan 17 13:02:32 2013
@@ -30,6 +30,17 @@ import org.apache.sshd.common.util.Buffe
  */
 public interface Session {
 
+    public enum State {
+        ReceiveKexInit, Kex, ReceiveNewKeys, AuthRequestSent, WaitForAuth, UserAuth, Running, Closed
+    }
+
+    /**
+     * Retrieve the state of this session.
+     * @return the session's state
+     * @see SessionListener
+     */
+    State getState();
+
     /**
      * Returns the value of the user-defined attribute of this session.
      *

Modified: mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/SessionListener.java
URL: http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/SessionListener.java?rev=1434652&r1=1434651&r2=1434652&view=diff
==============================================================================
--- mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/SessionListener.java (original)
+++ mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/SessionListener.java Thu Jan 17 13:02:32 2013
@@ -25,8 +25,23 @@ package org.apache.sshd.common;
  */
 public interface SessionListener {
 
+    /**
+     * A new session just been created
+     * @param session
+     */
     void sessionCreated(Session session);
 
-    void sessionClosed(Session s);
+    /**
+     * A session state has changed
+     * @param session
+     * @see org.apache.sshd.common.Session#getState()
+     */
+    void sessionChanged(Session session);
+
+    /**
+     * A session has been closed
+     * @param session
+     */
+    void sessionClosed(Session session);
 
 }

Modified: mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
URL: http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java?rev=1434652&r1=1434651&r2=1434652&view=diff
==============================================================================
--- mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java (original)
+++ mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java Thu Jan 17 13:02:32 2013
@@ -140,6 +140,8 @@ public abstract class AbstractSession im
     protected final Map<AttributeKey<?>, Object> attributes = new ConcurrentHashMap<AttributeKey<?>, Object>();
     protected String username;
 
+    private State state = State.ReceiveKexInit;
+
     /**
      * Create a new session.
      *
@@ -193,6 +195,18 @@ public abstract class AbstractSession im
         ioSession.setAttribute(SESSION, session);
     }
 
+    public State getState() {
+        return state;
+    }
+
+    protected void setState(State state) {
+        this.state = state;
+        final ArrayList<SessionListener> l = new ArrayList<SessionListener>(listeners);
+        for (SessionListener sl : l) {
+            sl.sessionChanged(this);
+        }
+    }
+
     /**
      * Retrieve the mina session
      *  
@@ -290,12 +304,10 @@ public abstract class AbstractSession im
                     closeFuture.setClosed();
                     lock.notifyAll();
                 }
-
+                state = State.Closed;
                 log.info("Session {}@{} closed", s.getUsername(), s.getIoSession().getRemoteAddress());
                 // Fire 'close' event
-                final ArrayList<SessionListener> l =
-                        new ArrayList<SessionListener>(listeners);
-
+                final ArrayList<SessionListener> l = new ArrayList<SessionListener>(listeners);
                 for (SessionListener sl : l) {
                     sl.sessionClosed(s);
                 }

Modified: mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSession.java
URL: http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSession.java?rev=1434652&r1=1434651&r2=1434652&view=diff
==============================================================================
--- mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSession.java (original)
+++ mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSession.java Thu Jan 17 13:02:32 2013
@@ -61,7 +61,6 @@ public class ServerSession extends Abstr
 
     private Future authTimerFuture;
     private Future idleTimerFuture;
-    private State state = State.ReceiveKexInit;
     private int maxAuthRequests = 20;
     private int nbAuthRequests;
     private int authTimeout = 10 * 60 * 1000; // 10 minutes in milliseconds
@@ -76,10 +75,6 @@ public class ServerSession extends Abstr
 
     private List<NamedFactory<UserAuth>> userAuthFactories;
 
-    private enum State {
-        ReceiveKexInit, Kex, ReceiveNewKeys, WaitingUserAuth, UserAuth, Running, Unknown
-    }
-
     public ServerSession(FactoryManager server, IoSession ioSession) throws Exception {
         super(server, ioSession);
         maxAuthRequests = getIntProperty(ServerFactoryManager.MAX_AUTH_REQUESTS, maxAuthRequests);
@@ -150,7 +145,7 @@ public class ServerSession extends Abstr
                 log.debug("Received SSH_MSG_IGNORE");
                 break;
             default:
-                switch (state) {
+                switch (getState()) {
                     case ReceiveKexInit:
                         if (cmd != SshConstants.Message.SSH_MSG_KEXINIT) {
                             log.warn("Ignoring command " + cmd + " while waiting for " + SshConstants.Message.SSH_MSG_KEXINIT);
@@ -161,13 +156,13 @@ public class ServerSession extends Abstr
                         negociate();
                         kex = NamedFactory.Utils.create(factoryManager.getKeyExchangeFactories(), negociated[SshConstants.PROPOSAL_KEX_ALGS]);
                         kex.init(this, serverVersion.getBytes(), clientVersion.getBytes(), I_S, I_C);
-                        state = State.Kex;
+                        setState(State.Kex);
                         break;
                     case Kex:
                         buffer.rpos(buffer.rpos() - 1);
                         if (kex.next(buffer)) {
                             sendNewKeys();
-                            state = State.ReceiveNewKeys;
+                            setState(State.ReceiveNewKeys);
                         }
                         break;
                     case ReceiveNewKeys:
@@ -177,10 +172,10 @@ public class ServerSession extends Abstr
                         }
                         log.debug("Received SSH_MSG_NEWKEYS");
                         receiveNewKeys(true);
-                        state = State.WaitingUserAuth;
+                        setState(State.WaitForAuth);
                         scheduleAuthTimer();
                         break;
-                    case WaitingUserAuth:
+                    case WaitForAuth:
                         if (cmd != SshConstants.Message.SSH_MSG_SERVICE_REQUEST) {
                             log.debug("Expecting a {}, but received {}", SshConstants.Message.SSH_MSG_SERVICE_REQUEST, cmd);
                             notImplemented();
@@ -208,7 +203,7 @@ public class ServerSession extends Abstr
                         scheduleIdleTimer();
                         break;
                     default:
-                        throw new IllegalStateException("Unsupported state: " + state);
+                        throw new IllegalStateException("Unsupported state: " + getState());
                 }
         }
     }
@@ -364,14 +359,14 @@ public class ServerSession extends Abstr
     }
 
     private void userAuth(Buffer buffer, SshConstants.Message cmd) throws Exception {
-        if (state == State.WaitingUserAuth) {
+        if (getState() == State.WaitForAuth) {
             log.debug("Accepting user authentication request");
             buffer = createBuffer(SshConstants.Message.SSH_MSG_SERVICE_ACCEPT, 0);
             buffer.putString("ssh-userauth");
             writePacket(buffer);
             userAuthFactories = new ArrayList<NamedFactory<UserAuth>>(getServerFactoryManager().getUserAuthFactories());
             log.debug("Authorized authentication methods: {}", NamedFactory.Utils.getNames(userAuthFactories));
-            state = State.UserAuth;
+            setState(State.UserAuth);
         } else {
             if (nbAuthRequests++ > maxAuthRequests) {
                 throw new SshException(SshConstants.SSH2_DISCONNECT_PROTOCOL_ERROR, "Too may authentication failures");
@@ -463,7 +458,7 @@ public class ServerSession extends Abstr
 
                 buffer = createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_SUCCESS, 0);
                 writePacket(buffer);
-                state = State.Running;
+                setState(State.Running);
                 this.authed = true;
                 this.username = username;
                 unscheduleAuthTimer();