donaldp     01/03/29 20:08:03

  Modified:    src/conf avalon-demo-assembly.xml
               src/manifest cornerstone.mf
  Added:       src/java/org/apache/cornerstone/blocks/packet Acceptor.java
                        DefaultPacketManager.java
                        DefaultPacketManager.xinfo
               src/java/org/apache/cornerstone/services/packet
                        PacketHandler.java PacketHandlerFactory.java
                        PacketManager.java
  Log:
  Created a basic packet handler.
  
  Acts like a conneciton handler but instead is associated with Datagram 
packets/sockets.
  
  Revision  Changes    Path
  1.4       +5 -1      jakarta-avalon-cornerstone/src/conf/avalon-demo-assembly.xml
  
  Index: avalon-demo-assembly.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-cornerstone/src/conf/avalon-demo-assembly.xml,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- avalon-demo-assembly.xml  2001/03/16 13:16:43     1.3
  +++ avalon-demo-assembly.xml  2001/03/30 04:08:02     1.4
  @@ -42,7 +42,11 @@
         </repositories>
       </configuration>
     </block>
  -  
  +   
  +  <!-- The Packet Manager block -->
  +  <block class="org.apache.cornerstone.blocks.packet.DefaultPacketManager" 
  +         name="packet-manager" />
  + 
     <!-- The Connection Manager block -->
     <block class="org.apache.cornerstone.blocks.connection.DefaultConnectionManager" 
            name="connections" />
  
  
  
  1.1                  
jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/blocks/packet/Acceptor.java
  
  Index: Acceptor.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   */
  package org.apache.cornerstone.blocks.packet;
  
  import java.io.IOException;
  import java.net.DatagramSocket;
  import java.net.DatagramPacket;
  import java.util.ArrayList;
  import java.util.Iterator;
  import org.apache.avalon.AbstractLoggable;
  import org.apache.avalon.Component;
  import org.apache.avalon.util.thread.ThreadPool;
  import org.apache.cornerstone.services.packet.PacketHandler;
  import org.apache.cornerstone.services.packet.PacketHandlerFactory;
  
  /**
   * Support class for the DefaultPacketManager. 
   * This manages an individual DatagramSocket.
   * 
   * @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
   */
  class Acceptor
      extends AbstractLoggable
      implements Component, Runnable
  {
      protected final DatagramSocket          m_datagramSocket;
      protected final PacketHandlerFactory    m_handlerFactory;
      protected final ThreadPool              m_threadPool;
      protected final ArrayList               m_runners          = new ArrayList();
  
      protected Thread                           m_thread;
  
      public Acceptor( final DatagramSocket datagramSocket, 
                       final PacketHandlerFactory handlerFactory,
                       final ThreadPool threadPool )
      {
          m_datagramSocket = datagramSocket;
          m_handlerFactory = handlerFactory;
          m_threadPool = threadPool;
      }
  
      public void dispose()
          throws Exception
      {
          if( null != m_thread )
          {
              m_thread.interrupt();
              m_thread.join( /* 1000 ??? */ );
              m_thread = null;
          }
  
          final Iterator runners = m_runners.iterator();
          while( runners.hasNext() )
          {
              final PacketHandlerRunner runner = (PacketHandlerRunner)runners.next();
              runner.dispose();
          }
  
          m_runners.clear();
      }
  
      public void run()
      {
          m_thread = Thread.currentThread();
          DatagramPacket packet = null;
          
          try
          {
              final int size = m_datagramSocket.getReceiveBufferSize();
              final byte[] buffer = new byte[ size ];
              packet = new DatagramPacket( buffer, size );
          }
          catch( final IOException ioe )
          {
              getLogger().error( "Failed to get receive buffer size for datagram 
socket", 
                                 ioe );
          }
  
          while( !Thread.interrupted() )
          {
              try
              {
                  m_datagramSocket.receive( packet );
                  final PacketHandler handler = m_handlerFactory.createPacketHandler();
                  final PacketHandlerRunner runner = 
                      new PacketHandlerRunner( packet, m_runners, handler );
                  setupLogger( runner );
                  m_threadPool.execute( runner );
              }
              catch( final IOException ioe )
              {
                  getLogger().error( "Exception accepting connection", ioe );
              }
              catch( final Exception e )
              {
                  getLogger().error( "Exception executing runner", e );
              }
          }
      }
  }
   
  class PacketHandlerRunner
      extends AbstractLoggable
      implements Runnable, Component
  {
      protected DatagramPacket  m_packet;
      protected Thread          m_thread;
      protected ArrayList       m_runners;
      protected PacketHandler   m_handler;
  
      PacketHandlerRunner( final DatagramPacket packet, 
                           final ArrayList runners, 
                           final PacketHandler handler )
      {
          m_packet = packet;
          m_runners = runners;
          m_handler = handler;
      }
  
      public void dispose()
          throws Exception
      {
          if( null != m_thread )
          {
              m_thread.interrupt();
              m_thread.join( /* 1000 ??? */ );
              m_thread = null;
          }
      }
          
      public void run()
      {
          try
          {
              m_thread = Thread.currentThread();
              m_runners.add( this );
  
              if( getLogger().isDebugEnabled() )
              {
                  getLogger().debug( "Starting connection on " + m_packet );
              }
  
              setupLogger( m_handler );
              m_handler.handlePacket( m_packet ); 
  
              if( getLogger().isDebugEnabled() )
              {
                  getLogger().debug( "Ending connection on " + m_packet );
              }
          }
          catch( final Exception e )
          {
              getLogger().warn( "Error handling packet", e );
          }
          finally
          {
              m_runners.remove( this );
          }
      }
  }
  
  
  
  1.1                  
jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/blocks/packet/DefaultPacketManager.java
  
  Index: DefaultPacketManager.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   */
  package org.apache.cornerstone.blocks.packet;
  
  import java.io.IOException;
  import java.net.DatagramSocket;
  import java.util.HashMap;
  import java.util.Iterator;
  import org.apache.avalon.AbstractLoggable;
  import org.apache.avalon.Context;
  import org.apache.avalon.Contextualizable;
  import org.apache.avalon.Disposable;
  import org.apache.avalon.util.thread.ThreadPool;
  import org.apache.cornerstone.services.packet.PacketHandler;
  import org.apache.cornerstone.services.packet.PacketHandlerFactory;
  import org.apache.cornerstone.services.packet.PacketManager;
  import org.apache.phoenix.Block;
  import org.apache.phoenix.BlockContext;
  
  /**
   * This is the service through which PacketManagement occurs.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
   */
  public class DefaultPacketManager
      extends AbstractLoggable
      implements Block, PacketManager, Contextualizable, Disposable
  {
      protected BlockContext        m_context;
      protected HashMap             m_acceptors        = new HashMap();
  
      public void contextualize( final Context context )
      {
          m_context = (BlockContext)context;
      }
      
      public void dispose()
          throws Exception
      {
          final Iterator names = ((HashMap)m_acceptors.clone()).keySet().iterator();
          while( names.hasNext() )
          {
              final String name = (String)names.next();
              disconnect( name );
          }
      }
  
      /**
       * Start managing a DatagramSocket.
       * Management involves accepting packets and farming them out to threads 
       * from pool to be handled.
       *
       * @param name the name of acceptor
       * @param socket the DatagramSocket from which to 
       * @param handlerFactory the factory from which to aquire handlers
       * @param threadPool the thread pool to use
       * @exception Exception if an error occurs
       */
      public synchronized void connect( final String name, 
                                        final DatagramSocket socket,
                                        final PacketHandlerFactory handlerFactory,
                                        final ThreadPool threadPool )
          throws Exception
      {
          if( null != m_acceptors.get( name ) )
          {
              throw new IllegalArgumentException( "Acceptor already exists with name " 
+ 
                                                  name );
          }
  
          final Acceptor acceptor = new Acceptor( socket, handlerFactory, threadPool );
          setupLogger( acceptor );
          m_acceptors.put( name, acceptor );
          threadPool.execute( acceptor );
      }
      
      /**
       * Start managing a DatagramSocket. 
       * This is similar to other connect method except that it uses default thread 
pool.
       *
       * @param name the name of DatagramSocket
       * @param socket the DatagramSocket from which to 
       * @param handlerFactory the factory from which to aquire handlers
       * @exception Exception if an error occurs
       */
      public synchronized void connect( final String name, 
                                        final DatagramSocket socket, 
                                        final PacketHandlerFactory handlerFactory )
          throws Exception
      {
          connect( name, socket, handlerFactory, m_context.getDefaultThreadPool() );
      }
      
      /**
       * This shuts down all handlers and socket, waiting for each to gracefully 
shutdown.
       *
       * @param name the name of packet
       * @exception Exception if an error occurs
       */
      public synchronized void disconnect( final String name )
          throws Exception
      {
          disconnect( name, false );
      }
      
      /**
       * This shuts down all handlers and socket. 
       * If tearDown is true then it will forcefully shutdown all acceptors and try 
       * to return as soon as possible. Otherwise it will behave the same as 
       * void disconnect( String name );
       *
       * @param name the name of acceptor
       * @param tearDown if true will forcefully tear down all handlers
       * @exception Exception if an error occurs
       */
      public synchronized void disconnect( final String name, final boolean tearDown )
          throws Exception
      {
          final Acceptor acceptor = (Acceptor)m_acceptors.remove( name );
          if( null == acceptor )
          {
              throw new IllegalArgumentException( "No such acceptor with name " +
                                                  name );
          }
  
          //TODO: Stop ignoring tearDown
          acceptor.dispose();
      }
  }
  
  
  
  1.1                  
jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/blocks/packet/DefaultPacketManager.xinfo
  
  Index: DefaultPacketManager.xinfo
  ===================================================================
  <?xml version="1.0"?>
  
  <blockinfo>
  
    <meta>
  
      <contributors>
        <author name="Peter Donald" email="[EMAIL PROTECTED]"/>
      </contributors>
  
    </meta>
  
    <!-- services that are offered by this block -->
    <services>
      <service name="org.apache.cornerstone.services.packet.PacketManager" 
version="1.0" />
    </services>
  
  </blockinfo>
  
  
  
  1.1                  
jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/services/packet/PacketHandler.java
  
  Index: PacketHandler.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   */
  package org.apache.cornerstone.services.packet;
  
  import java.io.IOException;
  import java.net.DatagramPacket;
  import java.net.ProtocolException;
  import org.apache.avalon.Component;
  
  /**
   * This interface is the way in which handlers are created.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
   */
  public interface PacketHandler
      extends Component
  {
      /**
       * Handle a datgram packet.
       * This handler is responsible for processing packets as they occur.
       *
       * @param packet the packet
       * @exception IOException if an error reading from socket occurs
       * @exception ProtocolException if an error handling connection occurs
       */
      void handlePacket( DatagramPacket packet ) 
          throws IOException, ProtocolException;
  }
  
  
  
  
  1.1                  
jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/services/packet/PacketHandlerFactory.java
  
  Index: PacketHandlerFactory.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   */
  package org.apache.cornerstone.services.packet;
  
  /**
   * This interface is the way in which handlers are created.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
   */
  public interface PacketHandlerFactory
  {
      /**
       * Construct an appropriate PacketHandler.
       *
       * @return the new PacketHandler
       * @exception Exception if an error occurs
       */
      PacketHandler createPacketHandler()
          throws Exception;
  }
  
  
  
  
  1.1                  
jakarta-avalon-cornerstone/src/java/org/apache/cornerstone/services/packet/PacketManager.java
  
  Index: PacketManager.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   */
  package org.apache.cornerstone.services.packet;
  
  import java.net.DatagramSocket;
  import org.apache.avalon.util.thread.ThreadPool;
  import org.apache.phoenix.Service;
  
  /**
   * This is the service through which Datagram ConnectionManagement occurs.
   * 
   * @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
   */
  public interface PacketManager
      extends Service
  {
      /**
       * Start managing a connection.
       * Management involves accepting connections and farming them out to threads 
       * from pool to be handled.
       *
       * @param name the name of connection
       * @param socket the ServerSocket from which to 
       * @param handlerFactory the factory from which to aquire handlers
       * @param threadPool the thread pool to use
       * @exception Exception if an error occurs
       */
      void connect( String name, 
                    DatagramSocket socket,
                    PacketHandlerFactory handlerFactory,
                    ThreadPool threadPool )
          throws Exception;
  
      /**
       * Start managing a connection. 
       * This is similar to other connect method except that it uses default thread 
pool.
       *
       * @param name the name of connection
       * @param socket the DatagramSocket from which to aquire packets
       * @param handlerFactory the factory from which to aquire handlers
       * @exception Exception if an error occurs
       */
      void connect( String name, 
                    DatagramSocket socket, 
                    PacketHandlerFactory handlerFactory )
          throws Exception;
  
      /**
       * This shuts down all handlers and socket, waiting for each to gracefully 
shutdown.
       *
       * @param name the name of connection
       * @exception Exception if an error occurs
       */
      void disconnect( String name )
          throws Exception;
  
      /**
       * This shuts down all handlers and socket. 
       * If tearDown is true then it will forcefully shutdown all connections and try 
       * to return as soon as possible. Otherwise it will behave the same as 
       * void disconnect( String name );
       *
       * @param name the name of connection
       * @param tearDown if true will forcefully tear down all handlers
       * @exception Exception if an error occurs
       */
      void disconnect( String name, boolean tearDown )
          throws Exception;
  }
  
  
  
  1.4       +4 -1      jakarta-avalon-cornerstone/src/manifest/cornerstone.mf
  
  Index: cornerstone.mf
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-cornerstone/src/manifest/cornerstone.mf,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- cornerstone.mf    2001/03/13 04:52:01     1.3
  +++ cornerstone.mf    2001/03/30 04:08:03     1.4
  @@ -1,5 +1,5 @@
   Manifest-Version: 1.0
  -Created-By: Apache Avalon Project
  +Created-By: Jakarta Apache Avalon/Phoenix Project
   
   Name: org/apache/cornerstone/blocks/masterstore/RepositoryManager.class
   Avalon-Block: true
  @@ -11,4 +11,7 @@
   Avalon-Block: true
   
   Name: org/apache/cornerstone/blocks/connection/DefaultConnectionManager.class
  +Avalon-Block: true
  +
  +Name: org/apache/cornerstone/blocks/packet/DefaultPacketManager.class
   Avalon-Block: true
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to