Java Code Examples for org.apache.mina.core.write.WriteRequest#getDestination()

The following examples show how to use org.apache.mina.core.write.WriteRequest#getDestination() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HackedProtocolCodecFilter.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
public ProtocolEncoderOutputImpl(IoSession session, NextFilter nextFilter, WriteRequest writeRequest) {
    this.session = session;
    this.nextFilter = nextFilter;

    // Only store the destination, not the full WriteRequest.
    destination = writeRequest.getDestination();
}
 
Example 2
Source File: ProtocolCodecFilter.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
public ProtocolEncoderOutputImpl(IoSession session, NextFilter nextFilter, WriteRequest writeRequest) {
    this.session = session;
    this.nextFilter = nextFilter;

    // Only store the destination, not the full WriteRequest.
    destination = writeRequest.getDestination();
}
 
Example 3
Source File: ProtocolCodecFilter.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
    Object message = writeRequest.getMessage();

    // Bypass the encoding if the message is contained in a IoBuffer,
    // as it has already been encoded before
    if ((message instanceof IoBuffer) || (message instanceof FileRegion)) {
        nextFilter.filterWrite(session, writeRequest);
        return;
    }

    // Get the encoder in the session
    ProtocolEncoder encoder = factory.getEncoder(session);

    ProtocolEncoderOutput encoderOut = getEncoderOut(session, nextFilter, writeRequest);

    if (encoder == null) {
        throw new ProtocolEncoderException("The encoder is null for the session " + session);
    }

    if (encoderOut == null) {
        throw new ProtocolEncoderException("The encoderOut is null for the session " + session);
    }

    try {
        // Now we can try to encode the response
        encoder.encode(session, message, encoderOut);

        // Send it directly
        Queue<Object> bufferQueue = ((AbstractProtocolEncoderOutput) encoderOut).getMessageQueue();

        // Write all the encoded messages now
        while (!bufferQueue.isEmpty()) {
            Object encodedMessage = bufferQueue.poll();

            if (encodedMessage == null) {
                break;
            }

            // Flush only when the buffer has remaining.
            if (!(encodedMessage instanceof IoBuffer) || ((IoBuffer) encodedMessage).hasRemaining()) {
                SocketAddress destination = writeRequest.getDestination();
                WriteRequest encodedWriteRequest = new EncodedWriteRequest(encodedMessage, null, destination);

                nextFilter.filterWrite(session, encodedWriteRequest);
            }
        }

        // Call the next filter
        nextFilter.filterWrite(session, new MessageWriteRequest(writeRequest));
    } catch (Throwable t) {
        ProtocolEncoderException pee;

        // Generate the correct exception
        if (t instanceof ProtocolEncoderException) {
            pee = (ProtocolEncoderException) t;
        } else {
            pee = new ProtocolEncoderException(t);
        }

        throw pee;
    }
}
 
Example 4
Source File: AbstractPollingConnectionlessIoAcceptor.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
private boolean flush( S session, long currentTime ) throws Exception
{
    final WriteRequestQueue writeRequestQueue = session.getWriteRequestQueue();
    final int maxWrittenBytes = session.getConfig().getMaxReadBufferSize()
        + ( session.getConfig().getMaxReadBufferSize() >>> 1 );

    int writtenBytes = 0;

    try
    {
        for ( ;; )
        {
            WriteRequest req = session.getCurrentWriteRequest();

            if ( req == null )
            {
                req = writeRequestQueue.poll( session );

                if ( req == null )
                {
                    setInterestedInWrite( session, false );
                    break;
                }

                session.setCurrentWriteRequest( req );
            }

            IoBuffer buf = ( IoBuffer ) req.getMessage();

            if ( buf.remaining() == 0 )
            {
                // Clear and fire event
                session.setCurrentWriteRequest( null );
                buf.reset();
                session.getFilterChain().fireMessageSent( req );
                continue;
            }

            SocketAddress destination = req.getDestination();

            if ( destination == null )
            {
                destination = session.getRemoteAddress();
            }

            int localWrittenBytes = send( session, buf, destination );

            if ( ( localWrittenBytes == 0 ) || ( writtenBytes >= maxWrittenBytes ) )
            {
                // Kernel buffer is full or wrote too much
                setInterestedInWrite( session, true );

                return false;
            }
            else
            {
                setInterestedInWrite( session, false );

                // Clear and fire event
                session.setCurrentWriteRequest( null );
                writtenBytes += localWrittenBytes;
                buf.reset();
                session.getFilterChain().fireMessageSent( req );
            }
        }
    }
    finally
    {
        session.increaseWrittenBytes( writtenBytes, currentTime );
    }

    return true;
}