directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r718288 - in /directory/apacheds/branches/apacheds-mina2/protocol-ntp/src/main/java/org/apache/directory/server/ntp: NtpServer.java protocol/NtpProtocolHandler.java
Date Mon, 17 Nov 2008 17:44:28 GMT
Author: elecharny
Date: Mon Nov 17 09:44:27 2008
New Revision: 718288

URL: http://svn.apache.org/viewvc?rev=718288&view=rev
Log:
Make the NTP server working again.

Modified:
    directory/apacheds/branches/apacheds-mina2/protocol-ntp/src/main/java/org/apache/directory/server/ntp/NtpServer.java
    directory/apacheds/branches/apacheds-mina2/protocol-ntp/src/main/java/org/apache/directory/server/ntp/protocol/NtpProtocolHandler.java

Modified: directory/apacheds/branches/apacheds-mina2/protocol-ntp/src/main/java/org/apache/directory/server/ntp/NtpServer.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-mina2/protocol-ntp/src/main/java/org/apache/directory/server/ntp/NtpServer.java?rev=718288&r1=718287&r2=718288&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-mina2/protocol-ntp/src/main/java/org/apache/directory/server/ntp/NtpServer.java
(original)
+++ directory/apacheds/branches/apacheds-mina2/protocol-ntp/src/main/java/org/apache/directory/server/ntp/NtpServer.java
Mon Nov 17 09:44:27 2008
@@ -20,8 +20,17 @@
 package org.apache.directory.server.ntp;
 
 
+import org.apache.directory.server.ntp.protocol.NtpProtocolCodecFactory;
 import org.apache.directory.server.ntp.protocol.NtpProtocolHandler;
 import org.apache.directory.server.protocol.shared.AbstractProtocolService;
+import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
+import org.apache.mina.core.service.IoHandler;
+import org.apache.mina.filter.codec.ProtocolCodecFilter;
+import org.apache.mina.transport.socket.DatagramAcceptor;
+import org.apache.mina.transport.socket.DatagramSessionConfig;
+import org.apache.mina.transport.socket.SocketAcceptor;
+import org.apache.mina.transport.socket.nio.NioDatagramAcceptor;
+import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
 
 import java.io.IOException;
 import java.net.InetSocketAddress;
@@ -64,23 +73,63 @@
 
     
     /**
+     * Start the NTPServer. We initialize the Datagram and Socket, if necessary.
+     * 
+     * Note that we don't have any filter in the chain, everything is done
+     * in the handler.
      * @throws IOException if there are issues binding
      */
     public void start() throws IOException
     {
-        //If appropriate, the udp and tcp servers could be enabled with boolean flags.
-        if ( getDatagramAcceptor() != null )
+        IoHandler ntpProtocolHandler = new NtpProtocolHandler();
+        
+        // Create the chain for the NTP server
+        DefaultIoFilterChainBuilder ntpChain = new DefaultIoFilterChainBuilder();
+        ntpChain.addLast( "codec", new ProtocolCodecFilter( NtpProtocolCodecFactory.getInstance()
) );
+        
+        if ( getUdpPort() > 0 )
         {
-            getDatagramAcceptor().setHandler( new NtpProtocolHandler() );
-            getDatagramAcceptor().bind( new InetSocketAddress( getIpPort() ) );
+            // We have to create a DatagramAcceptor
+            DatagramAcceptor acceptor = new  NioDatagramAcceptor();
+            setDatagramAcceptor( (NioDatagramAcceptor)acceptor );
+        
+            // Set the handler
+            acceptor.setHandler( ntpProtocolHandler );
+    
+            // Allow the port to be reused even if the socket is in TIME_WAIT state
+            ((DatagramSessionConfig)acceptor.getSessionConfig()).setReuseAddress( true );
+    
+            // Inject the chain
+            acceptor.setFilterChainBuilder( ntpChain );
+                
+            // Start the listener
+            acceptor.bind( new InetSocketAddress( getUdpPort() ) );
         }
-
-        if ( getSocketAcceptor() != null )
+        
+        if ( getTcpPort() > 0 )
         {
-            getSocketAcceptor().setReuseAddress( true );
-            getSocketAcceptor().setCloseOnDeactivation( false );
-            getSocketAcceptor().setHandler( new NtpProtocolHandler() );
-            getSocketAcceptor().bind( new InetSocketAddress( getIpPort() ) );
+            // It's a SocketAcceptor
+            SocketAcceptor acceptor = new NioSocketAcceptor();
+            
+            // Set the handler
+            acceptor.setHandler( ntpProtocolHandler );
+
+            // Disable the disconnection of the clients on unbind
+            acceptor.setCloseOnDeactivation( false );
+            
+            // Allow the port to be reused even if the socket is in TIME_WAIT state
+            acceptor.setReuseAddress( true );
+            
+            // No Nagle's algorithm
+            acceptor.getSessionConfig().setTcpNoDelay( true );
+            
+            // Inject the chain
+            acceptor.setFilterChainBuilder( ntpChain );
+
+            setSocketAcceptor( acceptor );
+            
+            // Start the listener
+            acceptor.bind( new InetSocketAddress( getTcpPort() ) );
         }
     }
 
@@ -97,4 +146,27 @@
             getSocketAcceptor().unbind( new InetSocketAddress( getIpPort() ));
         }
     }
+    
+    
+    /**
+     * @see Object#toString()
+     */
+    public String toString()
+    {
+        StringBuilder sb = new StringBuilder();
+        
+        sb.append( "NTPServer[" ).append( getServiceName() ).append( "] :" ).append( '\n'
);
+        
+        if ( getUdpPort() > 0 )
+        {
+            sb.append( "  Listening on UDP:" ).append( getUdpPort() ).append( '\n' );
+        }
+
+        if ( getTcpPort() > 0 )
+        {
+            sb.append( "  Listening on TCP:" ).append( getTcpPort() ).append( '\n' );
+        }
+        
+        return sb.toString();
+    }
 }

Modified: directory/apacheds/branches/apacheds-mina2/protocol-ntp/src/main/java/org/apache/directory/server/ntp/protocol/NtpProtocolHandler.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-mina2/protocol-ntp/src/main/java/org/apache/directory/server/ntp/protocol/NtpProtocolHandler.java?rev=718288&r1=718287&r2=718288&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-mina2/protocol-ntp/src/main/java/org/apache/directory/server/ntp/protocol/NtpProtocolHandler.java
(original)
+++ directory/apacheds/branches/apacheds-mina2/protocol-ntp/src/main/java/org/apache/directory/server/ntp/protocol/NtpProtocolHandler.java
Mon Nov 17 09:44:27 2008
@@ -24,8 +24,7 @@
 import org.apache.directory.server.ntp.NtpService;
 import org.apache.directory.server.ntp.messages.NtpMessage;
 import org.apache.directory.server.ntp.service.NtpServiceImpl;
-import org.apache.mina.core.service.IoHandler;
-import org.apache.mina.core.session.IdleStatus;
+import org.apache.mina.core.service.IoHandlerAdapter;
 import org.apache.mina.core.session.IoSession;
 import org.apache.mina.filter.codec.ProtocolCodecFilter;
 import org.slf4j.Logger;
@@ -36,7 +35,7 @@
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$, $Date$
  */
-public class NtpProtocolHandler implements IoHandler
+public class NtpProtocolHandler extends IoHandlerAdapter
 {
     /** the log for this class */
     private static final Logger log = LoggerFactory.getLogger( NtpProtocolHandler.class );
@@ -44,6 +43,9 @@
     private NtpService ntpService = new NtpServiceImpl();
 
 
+    /**
+     * {@inheritDoc}
+     */
     public void sessionCreated( IoSession session ) throws Exception
     {
         if ( log.isDebugEnabled() )
@@ -51,10 +53,14 @@
             log.debug( "{} CREATED", session.getRemoteAddress() );
         }
 
-        session.getFilterChain().addFirst( "codec", new ProtocolCodecFilter( NtpProtocolCodecFactory.getInstance()
) );
+        // Inject the Codec filter in the chain 
+        //session.getFilterChain().addFirst( "codec", new ProtocolCodecFilter( NtpProtocolCodecFactory.getInstance()
) );
     }
 
 
+    /**
+     * {@inheritDoc}
+     *
     public void sessionOpened( IoSession session )
     {
         if ( log.isDebugEnabled() )
@@ -64,6 +70,9 @@
     }
 
 
+    /**
+     * {@inheritDoc}
+     */
     public void sessionClosed( IoSession session )
     {
         if ( log.isDebugEnabled() )
@@ -73,6 +82,9 @@
     }
 
 
+    /**
+     * {@inheritDoc}
+     *
     public void sessionIdle( IoSession session, IdleStatus status )
     {
         if ( log.isDebugEnabled() )
@@ -82,6 +94,9 @@
     }
 
 
+    /**
+     * {@inheritDoc}
+     */
     public void exceptionCaught( IoSession session, Throwable cause )
     {
         log.error( session.getRemoteAddress() + " EXCEPTION", cause );
@@ -89,6 +104,9 @@
     }
 
 
+    /**
+     * {@inheritDoc}
+     */
     public void messageReceived( IoSession session, Object message )
     {
         if ( log.isDebugEnabled() )
@@ -102,11 +120,14 @@
     }
 
 
+    /**
+     * {@inheritDoc}
+     *
     public void messageSent( IoSession session, Object message )
     {
         if ( log.isDebugEnabled() )
         {
             log.debug( "{} SENT:  {}", session.getRemoteAddress(), message );
         }
-    }
+    }*/
 }



Mime
View raw message