Le 4/23/14 12:00 PM, Vladimir Kravets a écrit :
> Hi all again,
>
> Thanks a lot for idea and I'm successfully rewrote my code to use Mina3.
Cool !
>
> But I faced with flow which implementation wasn't easy.
> And it's related to internal realization of cummulative buffer (it's not
> exists in the mina3, thus I'm tried to write own). I try to do it and it
> wasn't work... =(

The internal IoBuffer is an array of ByteBuffer. It will 'cumulate' all
the read data from the socket, and you'll see the result as a single
ByteBuffer (ie, all the operation done on the IoBuffer will be the same
than the ByteBuffer operations, except that they will be applied on
many). I don't think you need to implement your own cumulative buffer.


> After investigation code of mina I found that the same buffer passed to the
> decoder in case if previous call of decoder successfully return data object
> (not null!)
Not sure I see what you mean here... Can you be a bit more explicit ?

>
> Thus for commulative buffer I need to have logic which is check if passed
> buffer was already added to commulative buffer or not.
I don't think it's necessary. The logic should be (assuming you are
using a decoder) :

- when the decoder is called, check that you have enough data to decode
a full message
- if so, decode a message. It will be pushed into the chain
  - this process will be exectuted until no more message can be decoded
- otherwise (if we don't have a full message) we will wait for more data
to come

This is the way the ProtocolCodecFilter works :

    public void messageReceived(IoSession session, Object in,
ReadFilterChainController controller) {
        LOGGER.debug("Processing a MESSAGE_RECEIVED for session {}",
session);

        DECODING_STATE state = getDecodingState(session);

        // Loop until the decoder cannot decode more
        MESSAGE msg;
        try {
            while (((msg = decoder.decode((ENCODED) in, state)) != null)) {
                controller.callReadNextFilter(msg);
            }
        } catch (ProtocolDecoderException e) {
            LOGGER.debug("decoding exception : ", e);
            throw e;
        }
    }

Note that you may want to deal with the decoding in your handler. If so,
you have to take care of a few things :
- if you don't have enough data to create a full message, then the
buffer should accumulate the data (which is done by the IoBuffer)
- you may have more than one message, so you have to iterate (check the
above code)

>
> As for me from one point it's logical, from another point we need have
> heavy logic to prevent duplication during union two buffers.

Be sure to flip the IoBuffer when you have processed a message. This is
critical.

>
> I agree that issue can be in my code/logic, but I'm only want to show some
> of end user flows which can be appeared. =)
>
> My implementation of "comulative buffer" you can find here:
> https://github.com/vkravets/Fido4Java/blob/5b1e0f0b91c928e01047a2604925f8964e7cbff6/binkp-mina3/src/main/java/org/fidonet/binkp/codec/BinkDataDecoder.java

Sounds perfectly fine. You are correctly dealing with the fact that the
incoming buffer might not contain all the needed data, the buffer is
stored into the context, and you carefully reset the positon in this case.

This is exactly what a decoder should do !
>
> It will be great if someone comments or show me another way how it can be
> implementing. Or improve such user case in MINA3 before release.

I think your code is fine, and there is little we can add in MINA to
fulfill your need.

-- 
Regards,
Cordialement,
Emmanuel Lécharny
www.iktek.com 

Reply via email to