Package org.glassfish.grizzly.asyncqueue

Examples of org.glassfish.grizzly.asyncqueue.AsyncWriteQueueRecord


        final ArrayList<BufferArray> savedBufferStates = queueRecord.savedBufferStates;
       
        for (final Iterator<AsyncWriteQueueRecord> it = queue.iterator();
                it.hasNext() && totalRemaining > 0; ) {
           
            final AsyncWriteQueueRecord record = it.next();
           
            if (record.isEmptyRecord()) {
                savedBufferStates.add(null);
                continue;
            }
           
            final Buffer message = record.getMessage();
            final int pos = message.position();
           
            final int messageRemaining = message.remaining();

            final BufferArray bufferArray =
View Full Code Here


        queueRecord.size -= written;
       
        final Connection connection = queueRecord.getConnection();
        final Deque<AsyncWriteQueueRecord> queue = queueRecord.queue;
       
        AsyncWriteQueueRecord record;
        while (remainder > 0) {
            record = queue.peekFirst();
           
            assert record != null;
           
            if (record.isEmptyRecord()) {
                queue.removeFirst();
                record.notifyCompleteAndRecycle();
                written += EMPTY_RECORD_SPACE_VALUE;
                continue;
            }

            final WriteResult firstResult = record.getCurrentResult();
            final Buffer firstMessage = record.getMessage();
            final long firstMessageRemaining =
                    record.getInitialMessageSize() - firstResult.getWrittenSize();

            if (remainder >= firstMessageRemaining) {
                remainder -= firstMessageRemaining;
                queue.removeFirst();
                firstResult.setWrittenSize(record.getInitialMessageSize());
                firstMessage.position(firstMessage.limit());
               
                ((TCPNIOConnection) connection).onWrite(firstMessage, firstMessageRemaining);
                               
                record.notifyCompleteAndRecycle();
            } else {
                firstMessage.position(firstMessage.position() + remainder);
                firstResult.setWrittenSize(
                        firstResult.getWrittenSize() + remainder);
               
                ((TCPNIOConnection) connection).onWrite(firstMessage, remainder);
                return written;
            }
        }

        while ((record = queue.peekFirst()) != null && record.isEmptyRecord()) {
            queue.removeFirst();
            record.notifyCompleteAndRecycle();
            written += EMPTY_RECORD_SPACE_VALUE;
        }

        return written;
    }
View Full Code Here

       
        if (queueSize == 0) {
            return null;
        }
       
        final AsyncWriteQueueRecord currentRecord = writeTaskQueue.poll();

        if (currentRecord == null ||
                !canBeAggregated(currentRecord) ||
                queueSize == currentRecord.remaining()) {
            return currentRecord;
        }
       
        AsyncWriteQueueRecord nextRecord = checkAndGetNextRecord(writeTaskQueue);
       
        if (nextRecord == null) {
            return currentRecord;
        }
       
View Full Code Here

    }

    private static AsyncWriteQueueRecord checkAndGetNextRecord(
            final TaskQueue<AsyncWriteQueueRecord> writeTaskQueue) {

        final AsyncWriteQueueRecord nextRecord = writeTaskQueue.getQueue().poll();
        if (nextRecord == null) {
            return null;
        } else if (!canBeAggregated(nextRecord)) {
            final NIOConnection connection = (NIOConnection) nextRecord.getConnection();
            offerToTaskQueue(connection, nextRecord, writeTaskQueue);
            return null;
        }

        return nextRecord;
View Full Code Here

        public void notifyCompleteAndRecycle() {
        }

        @Override
        public void notifyFailure(final Throwable e) {
            AsyncWriteQueueRecord record;
            while ((record = queue.poll()) != null) {
                record.notifyFailure(e);
            }
        }
View Full Code Here

       

        final NIOConnection nioConnection = (NIOConnection) connection;

        // create and initialize the write queue record
        final AsyncWriteQueueRecord queueRecord = createRecord(
                nioConnection, message, completionHandler,
                dstAddress, pushBackHandler,
                !message.hasRemaining() || message.isExternal());


        if (nioConnection == null) {
            queueRecord.notifyFailure(new IOException("Connection is null"));
            return;
        }

        if (!nioConnection.isOpen()) {
            onWriteFailure(nioConnection, queueRecord,
                    nioConnection.getCloseReason().getCause());
            return;
        }

        // Get connection async write queue
        final TaskQueue<AsyncWriteQueueRecord> writeTaskQueue =
                nioConnection.getAsyncWriteQueue();

        final boolean isEmptyRecord = queueRecord.isEmptyRecord();
        final int messageSize = message.remaining();
        // For empty buffer reserve 1 byte space
        final int bytesToReserve = isEmptyRecord ?
                 EMPTY_RECORD_SPACE_VALUE : messageSize;

        final int pendingBytes = writeTaskQueue.reserveSpace(bytesToReserve);
        final boolean isCurrent = (pendingBytes == bytesToReserve);

        final boolean isLogFine = LOGGER.isLoggable(Level.FINEST);

        if (isLogFine) {
            doFineLog("AsyncQueueWriter.write connection={0} record={1} directWrite={2}",
                    nioConnection, queueRecord, isCurrent);
        }

        final Reentrant reentrants = Reentrant.getWriteReentrant();

        try {
            if (!reentrants.inc()) {
                // Max number of reentrants is reached

                queueRecord.setMessage(
                        cloneRecordIfNeeded(nioConnection, cloner, message));

                if (isCurrent) { //current but not finished.
                    writeTaskQueue.setCurrentElement(queueRecord);
                    nioConnection.simulateIOEvent(IOEvent.WRITE);
                } else {
                    writeTaskQueue.offer(queueRecord);
                }

                return;
            }

            if (isCurrent && isAllowDirectWrite) {

                // If we can write directly - do it w/o creating queue record (simple)
                final int written = messageSize > 0 ?
                        (int) write0(nioConnection, queueRecord) :
                        0;

                final boolean isFinished = queueRecord.isFinished();

                final int bytesToRelease = !isEmptyRecord ?
                        written :
                        (isFinished ? EMPTY_RECORD_SPACE_VALUE : 0);

                final boolean isQueueEmpty =
                        (writeTaskQueue.releaseSpaceAndNotify(bytesToRelease) == 0);

                if (isFinished) {
                    queueRecord.notifyCompleteAndRecycle();
                    if (!isQueueEmpty) {
                        nioConnection.simulateIOEvent(IOEvent.WRITE);
                    }
                    return;
                }
            }

            queueRecord.setMessage(
                    cloneRecordIfNeeded(nioConnection, cloner, message));

            if (isCurrent) { //current but not finished.
                writeTaskQueue.setCurrentElement(queueRecord);
                onReadyToWrite(nioConnection);
View Full Code Here

       
        int bytesReleased = 0;
       
        boolean done = true;
       
        AsyncWriteQueueRecord queueRecord = null;
        try {
            while ((queueRecord = aggregate(writeTaskQueue)) != null) {
                if (isLogFine) {
                    doFineLog("AsyncQueueWriter.processAsync doWrite"
                            + "connection={0} record={1}",
                            nioConnection, queueRecord);
                }                

                final int written = queueRecord.remaining() > 0
                        ? (int) write0(nioConnection, queueRecord)
                        : 0;
               
                done = queueRecord.isFinished();
               
                final int bytesToRelease = !queueRecord.isEmptyRecord()
                        ? written
                        : (done ? EMPTY_RECORD_SPACE_VALUE : 0);

                bytesReleased += bytesToRelease;

                if (done) {
                    finishQueueRecord(nioConnection, queueRecord);
                } else { // if there is still some data in current message
                    queueRecord.notifyIncomplete();
                    writeTaskQueue.setCurrentElement(queueRecord);
                    if (isLogFine) {
                        doFineLog("AsyncQueueWriter.processAsync onReadyToWrite "
                                + "connection={0} peekRecord={1}",
                                nioConnection, queueRecord);
View Full Code Here

        dstByteBuffer.limit(0);

        for (final Iterator<AsyncWriteQueueRecord> it = queue.iterator();
                it.hasNext() && dstBufferRemaining > 0; ) {
           
            final AsyncWriteQueueRecord record = it.next();
           
            if (record.isEmptyRecord()) continue;
           
            final Buffer message = record.getMessage();
            final int oldPos = message.position();
            final int oldLim = message.limit();
           
            final int remaining = message.remaining();
           
View Full Code Here

        queueRecord.size -= written;
       
        final Connection connection = queueRecord.getConnection();
        final Deque<AsyncWriteQueueRecord> queue = queueRecord.queue;
       
        AsyncWriteQueueRecord record;
        while (remainder > 0) {
            record = queue.peekFirst();
           
            assert record != null;
           
            if (record.isEmptyRecord()) {
                queue.removeFirst();
                record.notifyCompleteAndRecycle();
                written += EMPTY_RECORD_SPACE_VALUE;
                continue;
            }

            final WriteResult firstResult = record.getCurrentResult();
            final Buffer firstMessage = record.getMessage();
            final long firstMessageRemaining =
                    record.getInitialMessageSize() - firstResult.getWrittenSize();

            if (remainder >= firstMessageRemaining) {
                remainder -= firstMessageRemaining;
                queue.removeFirst();
                firstResult.setWrittenSize(record.getInitialMessageSize());
                firstMessage.position(firstMessage.limit());
               
                ((TCPNIOConnection) connection).onWrite(firstMessage, firstMessageRemaining);
                               
                record.notifyCompleteAndRecycle();
            } else {
                firstMessage.position(firstMessage.position() + remainder);
                firstResult.setWrittenSize(
                        firstResult.getWrittenSize() + remainder);
               
                ((TCPNIOConnection) connection).onWrite(firstMessage, remainder);
                return written;
            }
        }

        while ((record = queue.peekFirst()) != null && record.isEmptyRecord()) {
            queue.removeFirst();
            record.notifyCompleteAndRecycle();
            written += EMPTY_RECORD_SPACE_VALUE;
        }

        return written;
    }
View Full Code Here

       
        if (queueSize == 0) {
            return null;
        }
       
        final AsyncWriteQueueRecord currentRecord =
                writeTaskQueue.obtainCurrentElementAndReserve();

        if (currentRecord == null ||
                !canBeAggregated(currentRecord) ||
                queueSize == currentRecord.remaining()) {
            return currentRecord;
        }
       
        AsyncWriteQueueRecord nextRecord = checkAndGetNextRecord(writeTaskQueue);
       
        if (nextRecord == null) {
            return currentRecord;
        }
       
View Full Code Here

TOP

Related Classes of org.glassfish.grizzly.asyncqueue.AsyncWriteQueueRecord

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.