mina-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mhe...@apache.org
Subject svn commit: r605069 - in /mina/trunk: core/ core/src/main/java/org/apache/mina/common/ core/src/main/java/org/apache/mina/transport/vmpipe/ core/src/test/java/org/apache/mina/transport/ transport-serial/src/main/java/org/apache/mina/transport/serial/
Date Tue, 18 Dec 2007 02:47:05 GMT
Author: mheath
Date: Mon Dec 17 18:47:03 2007
New Revision: 605069

URL: http://svn.apache.org/viewvc?rev=605069&view=rev
Log:
Fixed DIRMINA-495 - Add support for initializing the IoSession from the code that invokes
the IoConnector.connect(...) method.

Added:
    mina/trunk/core/src/main/java/org/apache/mina/common/IoSessionInitializer.java
Modified:
    mina/trunk/core/   (props changed)
    mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoConnector.java
    mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoService.java
    mina/trunk/core/src/main/java/org/apache/mina/common/AbstractPollingConnectionlessIoAcceptor.java
    mina/trunk/core/src/main/java/org/apache/mina/common/AbstractPollingIoAcceptor.java
    mina/trunk/core/src/main/java/org/apache/mina/common/AbstractPollingIoConnector.java
    mina/trunk/core/src/main/java/org/apache/mina/common/IoConnector.java
    mina/trunk/core/src/main/java/org/apache/mina/transport/vmpipe/VmPipeAcceptor.java
    mina/trunk/core/src/main/java/org/apache/mina/transport/vmpipe/VmPipeConnector.java
    mina/trunk/core/src/test/java/org/apache/mina/transport/AbstractConnectorTest.java
    mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialConnector.java

Propchange: mina/trunk/core/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Mon Dec 17 18:47:03 2007
@@ -10,3 +10,5 @@
 *.log
 .deployables
 .settings
+
+.clover

Modified: mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoConnector.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoConnector.java?rev=605069&r1=605068&r2=605069&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoConnector.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoConnector.java Mon Dec
17 18:47:03 2007
@@ -21,7 +21,6 @@
 
 import java.net.SocketAddress;
 
-
 /**
  * A base implementation of {@link IoConnector}.
  *
@@ -78,15 +77,34 @@
             throw new IllegalStateException("defaultRemoteAddress is not set.");
         }
         
-        return connect(defaultRemoteAddress, null);
+        return connect(defaultRemoteAddress, null, null);
+    }
+    
+    public ConnectFuture connect(IoSessionInitializer ioSessionInitializer) {
+        SocketAddress defaultRemoteAddress = getDefaultRemoteAddress();
+        if (defaultRemoteAddress == null) {
+            throw new IllegalStateException("defaultRemoteAddress is not set.");
+        }
+        
+        return connect(defaultRemoteAddress, null, ioSessionInitializer);
     }
 
     public final ConnectFuture connect(SocketAddress remoteAddress) {
-        return connect(remoteAddress, null);
+        return connect(remoteAddress, null, null);
+    }
+    
+    public ConnectFuture connect(SocketAddress remoteAddress,
+            IoSessionInitializer callback) {
+        return connect(remoteAddress, null, callback);
+    }
+    
+    public ConnectFuture connect(SocketAddress remoteAddress,
+            SocketAddress localAddress) {
+        return connect(remoteAddress, localAddress, null);
     }
 
     public final ConnectFuture connect(SocketAddress remoteAddress,
-            SocketAddress localAddress) {
+            SocketAddress localAddress, IoSessionInitializer ioSessionInitializer) {
         if (isDisposing()) {
             throw new IllegalStateException("Already disposed.");
         }
@@ -146,7 +164,7 @@
             }
         }
 
-        return connect0(remoteAddress, localAddress);
+        return connect0(remoteAddress, localAddress, ioSessionInitializer);
     }
 
     /**
@@ -155,7 +173,7 @@
      * @param localAddress <tt>null</tt> if no local address is specified
      */
     protected abstract ConnectFuture connect0(SocketAddress remoteAddress,
-            SocketAddress localAddress);
+            SocketAddress localAddress, IoSessionInitializer ioSessionInitializer);
 
     /**
      * Adds required internal attributes and {@link IoFutureListener}s

Modified: mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoService.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoService.java?rev=605069&r1=605068&r2=605069&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoService.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoService.java Mon Dec 17
18:47:03 2007
@@ -670,7 +670,7 @@
         return listeners;
     }
     
-    protected final void finishSessionInitialization(IoSession session, IoFuture future)
{
+    protected final void finishSessionInitialization(IoSession session, IoFuture future,
IoSessionInitializer ioSessionInitializer) {
         // Update lastIoTime if needed.
         if (getLastReadTime() == 0) {
             setLastReadTime(getActivationTime());
@@ -706,6 +706,10 @@
         if (future != null && future instanceof ConnectFuture) {
             // DefaultIoFilterChain will notify the future. (We support ConnectFuture only
for now).
             session.setAttribute(DefaultIoFilterChain.SESSION_OPENED_FUTURE, future);
+        }
+        
+        if (ioSessionInitializer != null) {
+            ioSessionInitializer.initSession(session);
         }
         
         finishSessionInitialization0(session, future);

Modified: mina/trunk/core/src/main/java/org/apache/mina/common/AbstractPollingConnectionlessIoAcceptor.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/common/AbstractPollingConnectionlessIoAcceptor.java?rev=605069&r1=605068&r2=605069&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/common/AbstractPollingConnectionlessIoAcceptor.java
(original)
+++ mina/trunk/core/src/main/java/org/apache/mina/common/AbstractPollingConnectionlessIoAcceptor.java
Mon Dec 17 18:47:03 2007
@@ -235,7 +235,7 @@
             session = newSession;
         }
 
-        finishSessionInitialization(session, null);
+        finishSessionInitialization(session, null, null);
 
         try {
             this.getFilterChainBuilder().buildFilterChain(session.getFilterChain());

Modified: mina/trunk/core/src/main/java/org/apache/mina/common/AbstractPollingIoAcceptor.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/common/AbstractPollingIoAcceptor.java?rev=605069&r1=605068&r2=605069&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/common/AbstractPollingIoAcceptor.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/common/AbstractPollingIoAcceptor.java Mon
Dec 17 18:47:03 2007
@@ -307,7 +307,7 @@
                     break;
                 }
                 
-                finishSessionInitialization(session, null);
+                finishSessionInitialization(session, null, null);
 
                 // add the session to the SocketIoProcessor
                 session.getProcessor().add(session);

Modified: mina/trunk/core/src/main/java/org/apache/mina/common/AbstractPollingIoConnector.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/common/AbstractPollingIoConnector.java?rev=605069&r1=605068&r2=605069&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/common/AbstractPollingIoConnector.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/common/AbstractPollingIoConnector.java Mon
Dec 17 18:47:03 2007
@@ -146,7 +146,7 @@
     @Override
     @SuppressWarnings("unchecked")
     protected final ConnectFuture connect0(SocketAddress remoteAddress,
-                                      SocketAddress localAddress) {
+                                      SocketAddress localAddress, IoSessionInitializer callback)
{
         H handle = null;
         boolean success = false;
         try {
@@ -154,7 +154,7 @@
             if (connect(handle, remoteAddress)) {
                 ConnectFuture future = new DefaultConnectFuture();
                 T session = newSession(processor, handle);
-                finishSessionInitialization(session, future);
+                finishSessionInitialization(session, future, callback);
                 // Forward the remaining process to the IoProcessor.
                 session.getProcessor().add(session);
                 success = true;
@@ -174,7 +174,7 @@
             }
         }
 
-        ConnectionRequest request = new ConnectionRequest(handle);
+        ConnectionRequest request = new ConnectionRequest(handle, callback);
         connectQueue.add(request);
         startupWorker();
         wakeup();
@@ -252,7 +252,7 @@
             try {
                 if (finishConnect(handle)) {
                     T session = newSession(processor, handle);
-                    finishSessionInitialization(session, entry);
+                    finishSessionInitialization(session, entry, entry.getSessionCallback());
                     // Forward the remaining process to the IoProcessor.
                     session.getProcessor().add(session);
                     nHandles ++;
@@ -346,8 +346,9 @@
     protected final class ConnectionRequest extends DefaultConnectFuture {
         private final H handle;
         private final long deadline;
+        private final IoSessionInitializer ioSessionInitializer;
 
-        public ConnectionRequest(H handle) {
+        public ConnectionRequest(H handle, IoSessionInitializer callback) {
             this.handle = handle;
             long timeout = getConnectTimeoutMillis();
             if (timeout <= 0L) {
@@ -355,6 +356,7 @@
             } else {
                 this.deadline = System.currentTimeMillis() + timeout;
             }
+            this.ioSessionInitializer = callback;
         }
 
         public H getHandle() {
@@ -365,6 +367,10 @@
             return deadline;
         }
 
+        public IoSessionInitializer getSessionCallback() {
+            return ioSessionInitializer;
+        }
+        
         @Override
         public void cancel() {
             super.cancel();

Modified: mina/trunk/core/src/main/java/org/apache/mina/common/IoConnector.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/common/IoConnector.java?rev=605069&r1=605068&r2=605069&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/common/IoConnector.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/common/IoConnector.java Mon Dec 17 18:47:03
2007
@@ -76,6 +76,18 @@
     ConnectFuture connect();
     
     /**
+     * Connects to the {@link #setDefaultRemoteAddress(SocketAddress) default
+     * remote address} and invokes the <code>sessionCallback</code> when the
+     * IoSession is created but before {@link IoHandler#sessionCreated(IoSession)}
+     * is invoked.
+     * 
+     * @param ioSessionInitializer  the callback to invoke when the {@link IoSession} object
is created
+     * 
+     * @throws IllegalStateException if no default remote address is set.
+     */
+    ConnectFuture connect(IoSessionInitializer ioSessionInitializer);
+    
+    /**
      * Connects to the specified remote address.
      *
      * @return the {@link ConnectFuture} instance which is completed when the
@@ -84,6 +96,19 @@
     ConnectFuture connect(SocketAddress remoteAddress);
 
     /**
+     * Connects to the specified remote address and invokes
+     * the <code>sessionCallback</code> when the IoSession is created but before
+     * {@link IoHandler#sessionCreated(IoSession)} is invoked.
+     * 
+     * @param remoteAddress  the remote address to connect to
+     * @param ioSessionInitializer  the callback to invoke when the {@link IoSession} object
is created
+     * 
+     * @return the {@link ConnectFuture} instance which is completed when the
+     *         connection attempt initiated by this call succeeds or fails.
+     */
+    ConnectFuture connect(SocketAddress remoteAddress, IoSessionInitializer ioSessionInitializer);
+
+    /**
      * Connects to the specified remote address binding to the specified local address.
      *
      * @return the {@link ConnectFuture} instance which is completed when the
@@ -91,4 +116,20 @@
      */
     ConnectFuture connect(SocketAddress remoteAddress,
             SocketAddress localAddress);
+    
+    /**
+     * Connects to the specified remote address binding to the specified local
+     * address and invokes the <code>sessionCallback</code> when the IoSession
+     * is created but before {@link IoHandler#sessionCreated(IoSession)} is
+     * invoked.
+     * 
+     * @param remoteAddress  the remote address to connect to
+     * @param localAddress  the local interface to bind to
+     * @param ioSessionInitializer  the callback to invoke when the {@link IoSession} object
is created
+     *
+     * @return the {@link ConnectFuture} instance which is completed when the
+     *         connection attempt initiated by this call succeeds or fails.
+     */
+    ConnectFuture connect(SocketAddress remoteAddress,
+            SocketAddress localAddress, IoSessionInitializer ioSessionInitializer);
 }

Added: mina/trunk/core/src/main/java/org/apache/mina/common/IoSessionInitializer.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/common/IoSessionInitializer.java?rev=605069&view=auto
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/common/IoSessionInitializer.java (added)
+++ mina/trunk/core/src/main/java/org/apache/mina/common/IoSessionInitializer.java Mon Dec
17 18:47:03 2007
@@ -0,0 +1,33 @@
+/*
+ *  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.common;
+
+/**
+ * Defines a callback for obtaining the {@link IoSession} during
+ * session initialization.
+ *
+ * @author The Apache MINA Project (dev@mina.apache.org)
+ * @version $Rev: 600461 $, $Date: 2007-12-03 02:55:52 -0700 (Mon, 03 Dec 2007) $
+ */
+public interface IoSessionInitializer {
+
+    void initSession(IoSession session);
+    
+}

Modified: mina/trunk/core/src/main/java/org/apache/mina/transport/vmpipe/VmPipeAcceptor.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/transport/vmpipe/VmPipeAcceptor.java?rev=605069&r1=605068&r2=605069&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/transport/vmpipe/VmPipeAcceptor.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/transport/vmpipe/VmPipeAcceptor.java Mon
Dec 17 18:47:03 2007
@@ -143,6 +143,6 @@
     }
 
     void doFinishSessionInitialization(IoSession session, IoFuture future) {
-        finishSessionInitialization(session, future);
+        finishSessionInitialization(session, future, null);
     }
 }

Modified: mina/trunk/core/src/main/java/org/apache/mina/transport/vmpipe/VmPipeConnector.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/transport/vmpipe/VmPipeConnector.java?rev=605069&r1=605068&r2=605069&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/transport/vmpipe/VmPipeConnector.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/transport/vmpipe/VmPipeConnector.java Mon
Dec 17 18:47:03 2007
@@ -22,6 +22,7 @@
 import java.io.IOException;
 import java.net.SocketAddress;
 import java.util.HashSet;
+import java.util.Map;
 import java.util.Set;
 
 import org.apache.mina.common.AbstractIoConnector;
@@ -33,6 +34,7 @@
 import org.apache.mina.common.IoFuture;
 import org.apache.mina.common.IoFutureListener;
 import org.apache.mina.common.IoHandler;
+import org.apache.mina.common.IoSessionInitializer;
 import org.apache.mina.common.TransportMetadata;
 
 /**
@@ -62,7 +64,7 @@
 
     @Override
     protected ConnectFuture connect0(SocketAddress remoteAddress,
-                                      SocketAddress localAddress) {
+                                      SocketAddress localAddress, IoSessionInitializer ioSessionInitializer)
{
         VmPipe entry = VmPipeAcceptor.boundHandlers.get(remoteAddress);
         if (entry == null) {
             return DefaultConnectFuture.newFailedFuture(new IOException(
@@ -82,7 +84,7 @@
         VmPipeSessionImpl localSession = new VmPipeSessionImpl(this,
                 getListeners(), actualLocalAddress, getHandler(), entry);
 
-        finishSessionInitialization(localSession, future);
+        finishSessionInitialization(localSession, future, ioSessionInitializer);
 
         // and reclaim the local address when the connection is closed.
         localSession.getCloseFuture().addListener(LOCAL_ADDRESS_RECLAIMER);

Modified: mina/trunk/core/src/test/java/org/apache/mina/transport/AbstractConnectorTest.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/test/java/org/apache/mina/transport/AbstractConnectorTest.java?rev=605069&r1=605068&r2=605069&view=diff
==============================================================================
--- mina/trunk/core/src/test/java/org/apache/mina/transport/AbstractConnectorTest.java (original)
+++ mina/trunk/core/src/test/java/org/apache/mina/transport/AbstractConnectorTest.java Mon
Dec 17 18:47:03 2007
@@ -20,6 +20,8 @@
 package org.apache.mina.transport;
 
 import java.net.InetSocketAddress;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
 
 import junit.framework.Assert;
 import junit.framework.TestCase;
@@ -30,6 +32,7 @@
 import org.apache.mina.common.IoHandlerAdapter;
 import org.apache.mina.common.IoSession;
 import org.apache.mina.common.RuntimeIoException;
+import org.apache.mina.common.IoSessionInitializer;
 import org.apache.mina.util.AvailablePortFinder;
 
 /**
@@ -116,5 +119,44 @@
         } finally {
             connector.dispose();
         }
+    }
+    
+    /**
+     * Test to make sure the SessionCallback gets invoked before IoHandler.sessionCreated.
+     */
+    public void testSessionCallbackInvocation() throws Exception {
+        final int callbackInvoked = 0;
+        final int sessionCreatedInvoked = 1;
+        final int sessionCreatedInvokedBeforeCallback = 2;
+        final boolean[] assertions = {false, false, false};
+        final CountDownLatch latch = new CountDownLatch(2);
+        
+        int port = AvailablePortFinder.getNextAvailable(1025);
+        IoAcceptor acceptor = createAcceptor();
+        acceptor.setHandler(new IoHandlerAdapter());
+        InetSocketAddress address = new InetSocketAddress(port);
+        acceptor.bind(address);
+
+        IoConnector connector = createConnector();
+        connector.setHandler(new IoHandlerAdapter() {
+           @Override
+            public void sessionCreated(IoSession session) throws Exception {
+                   assertions[sessionCreatedInvoked] = true;
+                   assertions[sessionCreatedInvokedBeforeCallback] = !assertions[callbackInvoked];
+                   latch.countDown();
+            } 
+        });
+        
+        connector.connect(address, new IoSessionInitializer() {
+            public void initSession(IoSession session) {
+                assertions[callbackInvoked] = true;
+                latch.countDown();
+            }
+        });
+        
+        assertTrue("Timed out waiting for callback and IoHandler.sessionCreated to be invoked",
latch.await(5, TimeUnit.SECONDS));
+        assertTrue("Callback was not invoked", assertions[callbackInvoked]);
+        assertTrue("IoHandler.sessionCreated was not invoked", assertions[sessionCreatedInvoked]);
+        assertFalse("IoHandler.sessionCreated was invoked before session callback", assertions[sessionCreatedInvokedBeforeCallback]);
     }
 }

Modified: mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialConnector.java
URL: http://svn.apache.org/viewvc/mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialConnector.java?rev=605069&r1=605068&r2=605069&view=diff
==============================================================================
--- mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialConnector.java
(original)
+++ mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialConnector.java
Mon Dec 17 18:47:03 2007
@@ -34,6 +34,7 @@
 import org.apache.mina.common.DefaultConnectFuture;
 import org.apache.mina.common.IoConnector;
 import org.apache.mina.common.IoFuture;
+import org.apache.mina.common.IoSessionInitializer;
 import org.apache.mina.common.TransportMetadata;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -54,7 +55,7 @@
 
     @Override
     protected ConnectFuture connect0(SocketAddress remoteAddress,
-            SocketAddress localAddress) {
+            SocketAddress localAddress, IoSessionInitializer ioSessionInitializer) {
 
         CommPortIdentifier portId;
         Enumeration<?> portList = CommPortIdentifier.getPortIdentifiers();
@@ -82,7 +83,7 @@
                         ConnectFuture future = new DefaultConnectFuture();
                         SerialSessionImpl session = new SerialSessionImpl(
                                 this, getListeners(), portAddress, serialPort);
-                        finishSessionInitialization(session, future);
+                        finishSessionInitialization(session, future, ioSessionInitializer);
                         session.start();
                         return future;
                     } catch (PortInUseException e) {



Mime
View raw message