/** * Constructor for the <code>SocketWriter</code> object. This is * used to create a writer that can write packets to the socket * in such a way that it write either asynchronously or block * the calling thread until such time as the packets are written. * * @param socket this is the pipeline that this writes to * @param reactor this is the writer used to scheduler writes * @param threshold this is the threshold for asynchronous buffers */ public SocketController(Socket socket, Reactor reactor, int threshold) throws IOException { this.writer = new SocketWriter(socket, threshold); this.flusher = new SocketFlusher(socket, reactor, writer); }
/** * This is used to cancel the operation if it has timed out. * If the delegate is waiting too long to flush the contents * of the buffers to the underlying transport then the socket * is closed and the flusher times out to avoid deadlock. */ public void cancel() { try { writer.abort(); }catch(Exception cause){ trace.trace(ERROR, cause); } } }
/** * 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 void close() throws IOException { flusher.close(); writer.close(); } }
/** * This method is used to flush all of the queued buffers to * the client. This method will not block but will simply flush * any data to the underlying transport. Internally the data * will be queued for delivery to the connected entity. */ public void flush() throws IOException { boolean done = writer.flush(); // returns true only if everything is delivered if(!done) { flusher.flush(); // here we will block for an op write event if the buffer contains a reference } }
/** * This is used to perform the drain of the pending buffer * queue. This will drain each pending queue if the socket is * write ready. If the socket is not write ready the operation * is enqueued for selection and this returns. This ensures * that all the data will eventually be delivered. */ public void run() { try { writer.execute(); } catch(Exception cause) { trace.trace(ERROR, cause); cancel(); } }
/** * This method is used to deliver the provided buffer of bytes to * the underlying transport. This will not modify the data that * is to be written, this will simply queue the buffers in the * order that they are provided. * * @param buffer this is the array of bytes to send to the client */ public void write(ByteBuffer buffer) throws IOException { boolean done = writer.write(buffer); // returns true if we can buffer if(!done) { flusher.flush(); // we could not fully write or buffer the data so we must flush } }
/** * This is used to perform the drain of the pending buffer * queue. This will drain each pending queue if the socket is * write ready. If the socket is not write ready the operation * is enqueued for selection and this returns. This ensures * that all the data will eventually be delivered. */ public void run() { try { writer.execute(); } catch(Exception cause) { trace.trace(ERROR, cause); cancel(); } }
/** * Constructor for the <code>SocketBufferWriter</code> object. This * is used to create a writer that can write buffers to the socket * in such a way that it write either asynchronously or block * the calling thread until such time as the buffers are written. * * @param socket this is the pipeline that this writes to * @param reactor this is the writer used to scheduler writes * @param buffer this is the initial size of the output buffer * @param threshold this is the maximum size of the buffer */ public SocketBufferWriter(Socket socket, Reactor reactor, int buffer, int threshold) throws IOException { this.writer = new SocketBuffer(socket, buffer, threshold); this.flusher = new SocketFlusher(writer, socket, reactor); }
/** * This method is used to flush all of the queued buffers to * the client. This method will not block but will simply flush * any data to the underlying transport. Internally the data * will be queued for delivery to the connected entity. */ public void flush() throws IOException { boolean done = writer.flush(); // returns true only if everything is delivered if(!done) { flusher.flush(); // here we will block for an op write event if the buffer contains a reference } }
/** * 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 void close() throws IOException { flusher.close(); writer.close(); } }
/** * This is used to cancel the operation if it has timed out. * If the delegate is waiting too long to flush the contents * of the buffers to the underlying transport then the socket * is closed and the flusher times out to avoid deadlock. */ public void cancel() { try { writer.abort(); }catch(Exception cause){ trace.trace(ERROR, cause); } } }
/** * Constructor for the <code>SocketBufferWriter</code> object. This * is used to create a writer that can write buffers to the socket * in such a way that it write either asynchronously or block * the calling thread until such time as the buffers are written. * * @param socket this is the pipeline that this writes to * @param reactor this is the writer used to scheduler writes * @param buffer this is the initial size of the output buffer * @param threshold this is the maximum size of the buffer */ public SocketBufferWriter(Socket socket, Reactor reactor, int buffer, int threshold) throws IOException { this.writer = new SocketBuffer(socket, buffer, threshold); this.flusher = new SocketFlusher(writer, socket, reactor); }
/** * This method is used to deliver the provided buffer of bytes to * the underlying transport. This will not modify the data that * is to be written, this will simply queue the buffers in the * order that they are provided. * * @param buffer this is the array of bytes to send to the client */ public void write(ByteBuffer buffer) throws IOException { boolean done = writer.write(buffer); // returns true if we can buffer if(!done) { flusher.flush(); // we could not fully write or buffer the data so we must flush } }