cvs commit: spice/sandbox/mesnet/src/java/org/realityforge/mesnet MesnetSelectorEventHandler.java Session.java

Peter Donald <[email protected]> Tue, 04 Nov 2003 20:08:36 -0800
Newsgroups gmane.comp.java.spice.cvs
Message-ID <[email protected]>
donaldp     03/11/04 20:08:36

  Added:       sandbox/mesnet/src/java/org/realityforge/mesnet
                        MesnetSelectorEventHandler.java Session.java
  Log:
  Start to check in mesnet stuff so can work on from home
  
  Revision  Changes    Path
  1.1                  spice/sandbox/mesnet/src/java/org/realityforge/mesnet/MesnetSelectorEventHandler.java
  
  Index: MesnetSelectorEventHandler.java
  ===================================================================
  package org.realityforge.mesnet;
  
  import java.io.IOException;
  import java.nio.ByteBuffer;
  import java.nio.channels.SelectionKey;
  import java.util.LinkedList;
  import org.realityforge.sca.selector.SelectorEventHandler;
  
  /**
   * @author <a href="mailto:peter at realityforge.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2003/11/05 04:08:36 $
   */
  public class MesnetSelectorEventHandler
      implements SelectorEventHandler
  {
      public void handleSelectorEvent( final SelectionKey key,
                                       final Object userData )
      {
          final Session session = (Session)userData;
          handleSessionEvent( key, session );
      }
  
      public void handleSessionEvent( final SelectionKey key,
                                      final Session session )
      {
          if( key.isReadable() )
          {
              performRead( session );
          }
  
          if( key.isValid() && key.isWritable() )
          {
              performWrite( session );
          }
      }
  
      void performRead( final Session session )
      {
          try
          {
              final ByteBuffer buffer = session.getReadBuffer();
              final int count = session.getChannel().read( buffer );
              if( -1 == count )
              {
                  endConnection( session );
                  return;
              }
              //TODO: Notify Monitor that data received
              final long now = System.currentTimeMillis();
              session.setLastReadTime( now );
              final short messageSize = buffer.getShort( 0 );
              final int limit = buffer.limit();
              if( limit + 2 >= messageSize )
              {
                  final byte[] data = new byte[ messageSize ];
                  buffer.clear();
                  buffer.getShort();
                  buffer.get( data, 0, messageSize );
                  buffer.compact();
                  //TODO: Notify Monitor that message received
                  final LinkedList queue = session.getReadQueue();
                  synchronized( queue )
                  {
                      queue.addLast( data );
                  }
              }
          }
          catch( final IOException e )
          {
              endConnection( session );
              return;
          }
      }
  
      void performWrite( final Session session )
      {
          final LinkedList queue = session.getWriteQueue();
          synchronized( queue )
          {
              if( queue.size() > 0 )
              {
                  final byte[] data = (byte[])queue.removeFirst();
                  final ByteBuffer buffer = session.getWriteBuffer();
                  buffer.clear();
                  buffer.put( data );
                  try
                  {
                      //TODO: Notify Monitor that data being written
                      final int count = session.getChannel().write( buffer );
                      final long now = System.currentTimeMillis();
                      session.setLastWriteTime( now );
                      if( count != data.length )
                      {
                          // TODO: Notify monitor as this means the channels
                          // write buffer is not big enough for messages?
                          endConnection( session );
                      }
                  }
                  catch( final IOException e )
                  {
                      queue.addFirst( data );
                      endConnection( session );
                  }
              }
          }
      }
  
      void endConnection( final Session session )
      {
          //TODO: Notify ending connection
          session.getKey().cancel();
          try
          {
              session.getChannel().close();
          }
          catch( IOException e )
          {
              // TODO: Note error closing channel
          }
          session.setKey( null );
          session.setChannel( null );
          //TODO: Attempt to resestablish connection with client
          //or wait till they connect?
      }
  
  }
  
  
  
  1.1                  spice/sandbox/mesnet/src/java/org/realityforge/mesnet/Session.java
  
  Index: Session.java
  ===================================================================
  package org.realityforge.mesnet;
  
  import java.util.LinkedList;
  import java.nio.ByteBuffer;
  import java.nio.channels.SocketChannel;
  import java.nio.channels.SelectionKey;
  
  /**
   * @author <a href="mailto:peter at realityforge.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2003/11/05 04:08:36 $
   */
  public class Session
  {
      private final LinkedList _readQueue = new LinkedList();
      private final LinkedList _writeQueue = new LinkedList();
      private final int _sessionID;
      private final int _sessionAuth;
      private final ByteBuffer _readBuffer;
      private final ByteBuffer _writeBuffer;
      private SocketChannel _channel;
      private SelectionKey _key;
      private long _lastReadTime;
      private long _lastWriteTime;
  
      public Session( final int sessionID,
                      final int sessionAuth,
                      final int maxReadSize,
                      final int maxWriteSize )
      {
          _sessionID = sessionID;
          _sessionAuth = sessionAuth;
          _readBuffer = ByteBuffer.allocateDirect( maxReadSize );
          _writeBuffer = ByteBuffer.allocateDirect( maxWriteSize );
      }
  
      public SocketChannel getChannel()
      {
          return _channel;
      }
  
      public void setChannel( final SocketChannel channel )
      {
          _channel = channel;
      }
  
      public SelectionKey getKey()
      {
          return _key;
      }
  
      public void setKey( final SelectionKey key )
      {
          _key = key;
      }
  
      public long getLastReadTime()
      {
          return _lastReadTime;
      }
  
      public void setLastReadTime( final long lastReadTime )
      {
          _lastReadTime = lastReadTime;
      }
  
      public long getLastWriteTime()
      {
          return _lastWriteTime;
      }
  
      public void setLastWriteTime( final long lastWriteTime )
      {
          _lastWriteTime = lastWriteTime;
      }
  
      public int getSessionID()
      {
          return _sessionID;
      }
  
      public int getSessionAuth()
      {
          return _sessionAuth;
      }
  
      public ByteBuffer getReadBuffer()
      {
          return _readBuffer;
      }
  
      public ByteBuffer getWriteBuffer()
      {
          return _writeBuffer;
      }
  
      public LinkedList getWriteQueue()
      {
          return _writeQueue;
      }
  
      public LinkedList getReadQueue()
      {
          return _readQueue;
      }
  }
  
  
  


-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive?  Does it
help you create better code?   SHARE THE LOVE, and help us help
YOU!  Click Here: http://sourceforge.net/donate/