Package de.netseeker.ejoe.io

Examples of de.netseeker.ejoe.io.DataChannel


    private Object read( SerializeAdapter adapter ) throws IOException
    {
        ByteBufferInputStream in = null;
        ByteBuffer dataBuf = null;
        Object result = null;
        DataChannel dataChannel = DataChannel.getInstance( this._senderInfo );

        try
        {
            if ( !this._senderInfo.hasWaitingBuffer() )
            {
                int length = dataChannel.readHeader( this._senderInfo, EJConstants.EJOE_CONNECTION_TIMEOUT );

                // maybe the DataChannel signals that it has already read
                // partial data
                if ( this._senderInfo.hasWaitingBuffer() )
                {
                    dataBuf = this._senderInfo.getWaitingBuffer();
                }
                else
                {
                    dataBuf = ByteBufferAllocator.allocate( length );
                }

                log.log( Level.FINE, "Going to read client request with length: " + length );
            }
            else
            {
                dataBuf = this._senderInfo.getWaitingBuffer();
            }

            if ( dataBuf.hasRemaining() )
            {
                DataChannel.nonBlockingRead( this._senderInfo.getChannel(), dataBuf );
            }

            dataBuf.flip();

            if ( log.isLoggable( Level.FINE ) )
            {
                byte[] tmp = new byte[dataBuf.remaining()];
                dataBuf.get( tmp );
                dataBuf.position( 0 );
                log.log( Level.FINE, "Client request read:\n" + new String( tmp, EJConstants.EJOE_DEFAULT_CHARSET ) );
            }

            if ( dataBuf.hasRemaining() )
            {
                try
                {
                    // usual way: deserialize using a adapter
                    if ( !this._senderInfo.isDirect() )
                    {
                        dataBuf = dataChannel.decode( dataBuf );
                        in = new ByteBufferInputStream( dataBuf );
                        result = deserialize( adapter, in );
                    }
                    // direct mode: don't deserialize, just copy and return the read
                    // ByteBuffer
                    else
                    {
                        // copy the bytebuffer
                        ByteBuffer tmp = ByteBufferAllocator.allocate( dataBuf.remaining() );
                        tmp.put( dataBuf );
                        tmp.flip();
                        result = dataChannel.decode( tmp );
                    }
                }
                catch ( Throwable t )
                {
                    throw new RemoteException( "Error while preprocessing request!", t );
View Full Code Here


     */
    private void send( ConnectionHeader serverInfo, ConnectionHeader clientInfo, SelectionKey selKey, Object obj )
            throws IOException
    {
        ByteBuffer dataBuf = (ByteBuffer) selKey.attachment();
        DataChannel dataChannel = DataChannel.getInstance( serverInfo );
        ByteBufferOutputStream out = null;

        try
        {
            if ( dataBuf == null )
            {
                // usual way: serialize using a adapter
                if ( !clientInfo.isDirect() )
                {
                    out = new ByteBufferOutputStream();
                    serialize( out, obj, clientInfo );
                    dataBuf = out.getBackingBuffer();
                }
                // direct mode: just use the attachement
                else
                {
                    dataBuf = (ByteBuffer) obj;
                }

                if ( dataBuf.position() > 0 )
                {
                    dataBuf.flip();
                }

                if ( log.isLoggable( Level.FINE ) )
                {
                    byte[] tmp = new byte[dataBuf.remaining()];
                    dataBuf.get( tmp );
                    dataBuf.position( 0 );
                    log.log( Level.FINE, "Going to send request..."
                            + new String( tmp, EJConstants.EJOE_DEFAULT_CHARSET ) );
                }

                dataChannel.writeHeader( serverInfo, dataBuf, this._connectionTimeout );
            }

            dataChannel.nonBlockingWrite( _channel, dataBuf );
            log.log( Level.FINE, "Request sent." );
        }
        finally
        {
            IOUtil.closeQuiet( out );
View Full Code Here

     * @throws IOException
     */
    private Object receive( ConnectionHeader clientInfo ) throws IOException
    {
        Object result = null;
        DataChannel dataChannel = DataChannel.getInstance( _serverInfo );
        ByteBuffer dataBuf = clientInfo.getWaitingBuffer();
        ByteBufferInputStream in = null;

        try
        {
            if ( dataBuf == null )
            {
                int length = dataChannel.readHeader( _serverInfo, this._connectionTimeout );
                // server signals a null result?
                if ( length == 0 )
                {
                    return null;
                }
View Full Code Here

        ByteBufferOutputStream out = null;
        ByteBuffer dataBuf = null;
        SerializeAdapter adapter = this._receiverInfo.getAdapterName() != null ? AdapterFactory
                .createAdapter( this._receiverInfo.getAdapterName() ) : null;
        WritableByteChannel channel = this._receiverInfo.getChannel();
        DataChannel dataChannel = DataChannel.getInstance( this._receiverInfo );

        try
        {
            if ( this._receiverInfo.hasAttachment() )
            {
                // usual way: convert the serialized object to a ByteBuffer
                if ( !this._receiverInfo.isDirect() )
                {
                    out = new ByteBufferOutputStream();
                    serialize( adapter, out );
                    dataBuf = out.getBackingBuffer();
                }
                // direct mode: just use the attachement
                else
                {
                    ByteBuffer tmp = (ByteBuffer) this._receiverInfo.getAttachment();
                    if ( tmp.position() > 0 )
                    {
                        tmp.flip();
                    }
                    dataBuf = tmp.duplicate();
                    ByteBufferAllocator.collect( tmp );
                }

                if ( dataBuf.position() > 0 )
                {
                    dataBuf.flip();
                }

                this._receiverInfo.releaseAttachment();

                dataChannel.writeHeader( this._receiverInfo, dataBuf, EJConstants.EJOE_CONNECTION_TIMEOUT );

                if ( log.isLoggable( Level.FINE ) )
                {
                    byte[] tmp = new byte[dataBuf.remaining()];
                    dataBuf.get( tmp );
                    dataBuf.position( 0 );
                    log.log( Level.FINE, "Going to send server response:\n"
                            + new String( tmp, EJConstants.EJOE_DEFAULT_CHARSET ) );
                }

                dataChannel.nonBlockingWrite( channel, dataBuf );
            }
            else if ( this._receiverInfo.hasWaitingBuffer() )
            {
                dataBuf = this._receiverInfo.getWaitingBuffer();
                dataChannel.nonBlockingWrite( channel, dataBuf );
            }
            // seems that we have no answer for the client
            else
            {
                dataChannel.writeHeader( this._receiverInfo, null, EJConstants.EJOE_CONNECTION_TIMEOUT );
            }

            log.log( Level.FINE, "Server response sent." );
        }
        finally
View Full Code Here

TOP

Related Classes of de.netseeker.ejoe.io.DataChannel

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.