All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.simpleframework.transport.SocketWriter Maven / Gradle / Ivy

/*
 * SocketWriter.java February 2008
 *
 * Copyright (C) 2008, Niall Gallagher 
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General 
 * Public License along with this library; if not, write to the 
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
 * Boston, MA  02111-1307  USA
 */

package org.simpleframework.transport;

import java.nio.channels.SocketChannel;

/**
 * The SocketWriter object is used to coalesce the
 * packets to be written in to a minimum size. Also this will queue
 * the packets to be written in the order they are provided to that
 * if the contents of the packets can not be fully written they 
 * can be flushed in the correct order.
 * 
 * @author Niall Gallagher
 * 
 * @see org.simpleframework.transport.SegmentBuilder
 */
class SocketWriter implements Writer {
   
   /**
    * This is the manager used to compress the packets.
    */
   private SegmentBuilder builder;
   
   /**
    * The socket channel that the byte contents are written to.
    */
   private SocketChannel channel;
   
   /**
    * This is used to determine whether the socket is closed.
    */
   private boolean closed;
    
   /**
    * Constructor for the SocketWriter object. This
    * is used to wrap the socket in an object that will queue and
    * coalesce the packets written. It ensures that the packets
    * that are sent are of a minimum size for performance.
    * 
    * @param socket this is the pipeline instance this wraps
    */
   public SocketWriter(Socket socket) {
      this(socket, 1024);
   }
    
   /**
    * Constructor for the SocketWriter object. This
    * is used to wrap the socket in an object that will queue and
    * coalesce the packets written. It ensures that the packets
    * that are sent are of a minimum size for performance.
    * 
    * @param socket this is the pipeline instance this wraps
    * @param size this is the minimum size for the packets written
    */
   public SocketWriter(Socket socket, int size) {
      this.builder = new SegmentBuilder(size);
      this.channel = socket.getChannel();
   }
   
   /**
    * This provides the socket for the writer. Providing this 
    * enables a Reactor to be used to determine when
    * the writer is write ready and thus when the writer can
    * be flushed if it contains packets that have not been written.
    * 
    * @return this returns the socket associated with this
    */
   public synchronized SocketChannel getChannel() {
      return channel;
   }

   /**
    * This is used to write the packets to the writer which will
    * be either written to the underlying socket or queued until
    * such time as the socket is write ready. This will return true
    * if the packet has been written to the underlying transport.
    * 
    * @param packet this is the packet that is the be written
    * 
    * @return true if the packet has been written to the transport
    */
   public synchronized boolean write(Packet packet) throws Exception {
      Packet compress = builder.build(packet); 
      
      if(compress == null) {
         return true;
      }
      return flush(); 
   }
   
   /**
    * This is used to flush all queued packets to the underlying
    * socket. If all of the queued packets have been fully written
    * then this returns true, otherwise this will return false.
    * 
    * @return true if all queued packets are flushed to the socket
    */   
   public synchronized boolean flush() throws Exception {
      int count = builder.length(); 
      
      while(count > 0) {
         Packet packet = builder.build(); 
         
         if(packet != null) { 
            int size = send(packet); 
            
            if(size < 0) {
               throw new TransportException("Socket is closed");
            }
            if(size == 0) { 
               break;
            }
            count -= size;
         }
      }
      return count <= 0; 
   }
   
   /**
    * This is used to send the packets to the socket. This attempts
    * to write the provided packet to the underlying socket, if 
    * all of the bytes are written the the packet is closed. This
    * will return the number of bytes that have been written.
    * 
    * @param packet this is the packet that is the be sent
    * 
    * @return the number of bytes written to the underlying socket
    */
   private synchronized int send(Packet packet) throws Exception {
      int size = packet.write(channel);      
      int left = packet.length();
      
      if(left == 0) {
         packet.close();
      }
      return size;
   }

   /**
    * This is used to close the writer and the underlying socket.
    * If a close is performed on the writer then no more bytes 
    * can be read from or written to the writer and the client 
    * will receive a connection close on their side.
    */  
   public synchronized void close() throws Exception {
      if(!closed) {    
         closed = true;
         builder.close();
         channel.socket().shutdownOutput(); // send FIN ACK
         channel.close();
      }
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy