cvs commit: spice/sandbox/packet/src/java/org/realityforge/packet/protocol ProtocolWriter.java

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

  Added:       sandbox/packet/src/java/org/realityforge/packet/protocol
                        ProtocolWriter.java
  Log:
  Start to code up protocol writer
  
  Revision  Changes    Path
  1.1                  spice/sandbox/packet/src/java/org/realityforge/packet/protocol/ProtocolWriter.java
  
  Index: ProtocolWriter.java
  ===================================================================
  /*
   * Copyright (C) The Spice Group. All rights reserved.
   *
   * This software is published under the terms of the Spice
   * Software License version 1.1, a copy of which has been included
   * with this distribution in the LICENSE.txt file.
   */
  package org.realityforge.packet.protocol;
  
  import java.io.IOException;
  import java.nio.BufferOverflowException;
  import java.nio.ByteBuffer;
  import java.nio.channels.WritableByteChannel;
  
  /**
   * A helper class for writing writing protocol to channel.
   * Note that this class is single threaded as it uses a shared
   * message buffer.
   *
   * @author <a href="mailto:peter at realityforge.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2003/11/12 01:05:58 $
   */
  class ProtocolWriter
  {
      /**
       * The sizeof the connected message.
       *
       * @see ProtocolConstants#S2C_CONNECTED
       */
      static final int SIZEOF_CONNECTED_MSG = 1 + 2 + 2;
  
      /**
       * The maximum size of any control message.
       */
      static final int MAX_CONTROL_SIZE =
          Math.max( ProtocolConstants.MAGIC.length, SIZEOF_CONNECTED_MSG );
  
      /**
       * Internal message buffer used to send control messages.
       */
      private final ByteBuffer m_message =
          ByteBuffer.allocateDirect( MAX_CONTROL_SIZE );
  
      /**
       * Send a single byte control message to channel.
       *
       * @param sessionID the SessionID
       * @param sessionAuth the SessionAuth
       * @param channel the channel
       * @throws IOException if error writing to channel
       * @throws BufferOverflowException if can not write
       *         whole control to channel
       */
      void sendConnectedMessage( final short sessionID,
                                 final short sessionAuth,
                                 final WritableByteChannel channel )
          throws IOException
      {
          m_message.clear();
          m_message.put( ProtocolConstants.S2C_CONNECTED );
          m_message.putShort( sessionID );
          m_message.putShort( sessionAuth );
          forceWriteMessage( m_message, channel );
      }
  
      /**
       * Send a single byte control message to channel.
       *
       * @param control the control code
       * @param channel the channel
       * @throws IOException if error writing to channel
       * @throws BufferOverflowException if can not write
       *         whole control to channel
       */
      void sendControlMessage( final byte control,
                               final WritableByteChannel channel )
          throws IOException
      {
          m_message.clear();
          m_message.put( control );
          forceWriteMessage( m_message, channel );
      }
  
      /**
       * Write message to specified channel.
       * If channel can not be written to then a
       * BufferOverflowException is thrown.
       *
       * @param message the message
       * @param channel the channel
       * @throws IOException if error writing to channel
       * @throws BufferOverflowException if can not write
       *         whole message to channel
       */
      void forceWriteMessage( final ByteBuffer message,
                              final WritableByteChannel channel )
          throws IOException
      {
          final int available = message.remaining();
          final int writeCount = channel.write( message );
          if( available != writeCount )
          {
              throw new BufferOverflowException();
          }
      }
  }
  
  
  


-------------------------------------------------------
This SF.Net email sponsored by: ApacheCon 2003,
16-19 November in Las Vegas. Learn firsthand the latest
developments in Apache, PHP, Perl, XML, Java, MySQL,
WebDAV, and more! http://www.apachecon.com/