You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@mina.apache.org by dan brownell <db...@hotmail.com> on 2010/05/28 13:27:17 UTC
PrefixedStringCodec header usage
Hi all,
I started writing my own prefix string codec
factory, then realised there was one already.
But I'm having
trouble working how to encode the prefix. The client program
doesn't use Mina, so I'm doing this:
BufferedWriter wr = new
BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String
myString = "Hello";
wr.write(myString.length());
wr.write(myString);
But this does not work. Meanwhile,
the PrefixedStringCodecFactory has a method,
setDecoderPrefixLength(prefixLength), and I'd assume that since I'm
sending an int, I'd want 4 bytes. I see in the source code that I need to do whatever ByteBuffer's putInt does, but I'm not sure what that is.
Could
someone please tell me how to prefix the String in this situation? It
will be a C++ socket sending it later, which is why I'm not using the
encoder.
Regards,
Daniel
_________________________________________________________________
The New Busy think 9 to 5 is a cute idea. Combine multiple calendars with Hotmail.
http://www.windowslive.com/campaign/thenewbusy?tile=multicalendar&ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_5
Re: PrefixedStringCodec header usage
Posted by Kiran Ayyagari <ka...@apache.org>.
hi Daniel,
On Fri, May 28, 2010 at 4:06 PM, dan brownell <db...@hotmail.com> wrote:
>
> Hi Kiran,
>
> Thanks for your reply. Your idea kind of worked.
> The thing is, it only works if I specift factory.setDecoderPrefixLength(1);
> and then it receives like 255 characters each time, which is unacceptable.
>
> If I specify factory.setDecoderPrefixLength(2) or (4), it gives me a dataLength error.
> My messages will probably be up to 100K, so I'll probably need all 4 bytes.
> Must I pad the byte[] with zeroes to fill 32 bits?
AFAIK you need not pad the integer bytes, writing an int directly to
the stream should be
enough in java, there must be something going on in the
encoder/decoder parts of code
not sure where cause I havn't used this codec
Kiran Ayyagari
RE: PrefixedStringCodec header usage
Posted by dan brownell <db...@hotmail.com>.
Hi Kiran,
Thanks for your reply. Your idea kind of worked.
The thing is, it only works if I specift factory.setDecoderPrefixLength(1);
and then it receives like 255 characters each time, which is unacceptable.
If I specify factory.setDecoderPrefixLength(2) or (4), it gives me a dataLength error.
My messages will probably be up to 100K, so I'll probably need all 4 bytes.
Must I pad the byte[] with zeroes to fill 32 bits?
I've never been very good with Java bit manipulation. Do you know how I'd do that?
Regards,
Daniel
> hmm, you need to write the length of the byte array of the string you
> are sending instead of writing the
> length of the string.
> byte[] data = myString.getBytes( "UTF-8" );
> wr.write(data.length);
> wr.write(myString);
>
> HTH
> Kiran Ayyagari
_________________________________________________________________
Hotmail is redefining busy with tools for the New Busy. Get more from your inbox.
http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_2
Re: PrefixedStringCodec header usage
Posted by Kiran Ayyagari <ka...@apache.org>.
> The issue is that I will be sending the string from a C++ socket, so I can't just use putPrefixedString(). When my test client uses the code I provided before, (i.e., wr.write(myString.length()); wr.write(myString);) I get the following at the INFO level, which is before any of my code in messageReceived() runs. Obviously the dataLength is over the top.
>
> INFO: RECEIVED: HeapBuffer[pos=0 lim=2048 cap=2048: 05 3C 3F 78 6D 6C 20 76 65 72 73 69 6F 6E 3D 27...]
> org.apache.mina.filter.codec.ProtocolDecoderException: org.apache.mina.core.buffer.BufferDataException: dataLength: 87834488 (Hexdump: 05 3C ...blah blah)
> at org.apache.mina.filter.codec.ProtocolCodecFilter.messageReceived(ProtocolCodecFilter.java:235)
>
> This suggests I am not emulating putPrefixedString correctly.
hmm, you need to write the length of the byte array of the string you
are sending instead of writing the
length of the string.
byte[] data = myString.getBytes( "UTF-8" );
wr.write(data.length);
wr.write(myString);
HTH
Kiran Ayyagari
Re: PrefixedStringCodec header usage
Posted by Emmanuel Lecharny <el...@gmail.com>.
On 5/28/10 1:56 PM, dan brownell wrote:
> Hi Emmanuel,
>
> I'm using Mina 2.0, RC1.
>
>
>> Can you provide your decoder code ?
>>
> Well, I gave up on my own decoder because I saw PrefixedStringCodecFactory already exists.
> All I'm doing currently is adding it to the filter.
>
Right, sorry !
> ProtocolCodecFactory factory = new PrefixedStringCodecFactory();
> chain.addLast("codec", new ProtocolCodecFilter(factory));
>
> The issue is that I will be sending the string from a C++ socket, so I can't just use putPrefixedString(). When my test client uses the code I provided before, (i.e., wr.write(myString.length()); wr.write(myString);) I get the following at the INFO level, which is before any of my code in messageReceived() runs. Obviously the dataLength is over the top.
>
> INFO: RECEIVED: HeapBuffer[pos=0 lim=2048 cap=2048: 05 3C 3F 78 6D 6C 20 76 65 72 73 69 6F 6E 3D 27...]
> org.apache.mina.filter.codec.ProtocolDecoderException: org.apache.mina.core.buffer.BufferDataException: dataLength: 87834488 (Hexdump: 05 3C ...blah blah)
> at org.apache.mina.filter.codec.ProtocolCodecFilter.messageReceived(ProtocolCodecFilter.java:235)
>
> This suggests I am not emulating putPrefixedString correctly.
>
When using the PrefixedStringCodecFactor constructor without any
parameter, it assumes that the message's length will be encoded as an
int, ie on 4 bytes. The received data seems to show that it's not the
case, as 05 3C 3F 78 can't be either an int, nor 5 can be the length of
the following string ("<?xml version='...").
I suggest first that you use a CumulativeProtocolDecoder to handle your
length-value messages , as it's shown in the ImageRequestDecoder class
(in mina-example, imagine sample). (It seems that the PrfixedString
codec might not handle correctly split messages (nit 100% sure, to be
checked).
Also try to check that the wr.write(length) really write 4 butes, not
only one. Also check that the bytes ordering are compatible as you write
them using C - you may have big-indian/little-indian issues)
--
Regards,
Cordialement,
Emmanuel Lécharny
www.nextury.com
RE: PrefixedStringCodec header usage
Posted by dan brownell <db...@hotmail.com>.
Hi Emmanuel,
I'm using Mina 2.0, RC1.
> Can you provide your decoder code ?
Well, I gave up on my own decoder because I saw PrefixedStringCodecFactory already exists.
All I'm doing currently is adding it to the filter.
ProtocolCodecFactory factory = new PrefixedStringCodecFactory();
chain.addLast("codec", new ProtocolCodecFilter(factory));
The issue is that I will be sending the string from a C++ socket, so I can't just use putPrefixedString(). When my test client uses the code I provided before, (i.e., wr.write(myString.length()); wr.write(myString);) I get the following at the INFO level, which is before any of my code in messageReceived() runs. Obviously the dataLength is over the top.
INFO: RECEIVED: HeapBuffer[pos=0 lim=2048 cap=2048: 05 3C 3F 78 6D 6C 20 76 65 72 73 69 6F 6E 3D 27...]
org.apache.mina.filter.codec.ProtocolDecoderException: org.apache.mina.core.buffer.BufferDataException: dataLength: 87834488 (Hexdump: 05 3C ...blah blah)
at org.apache.mina.filter.codec.ProtocolCodecFilter.messageReceived(ProtocolCodecFilter.java:235)
This suggests I am not emulating putPrefixedString correctly.
Regards,
Daniel
_________________________________________________________________
Hotmail is redefining busy with tools for the New Busy. Get more from your inbox.
http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_2
Re: PrefixedStringCodec header usage
Posted by Emmanuel Lecharny <el...@gmail.com>.
On 5/28/10 1:27 PM, dan brownell wrote:
> Hi all,
>
> I started writing my own prefix string codec
> factory, then realised there was one already.
>
MINA's version ?
> But I'm having
> trouble working how to encode the prefix. The client program
> doesn't use Mina, so I'm doing this:
>
> BufferedWriter wr = new
> BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
> String
> myString = "Hello";
> wr.write(myString.length());
> wr.write(myString);
>
> But this does not work. Meanwhile,
> the PrefixedStringCodecFactory has a method,
> setDecoderPrefixLength(prefixLength), and I'd assume that since I'm
> sending an int, I'd want 4 bytes. I see in the source code that I need to do whatever ByteBuffer's putInt does, but I'm not sure what that is.
>
Can you provide your decoder code ?
--
Regards,
Cordialement,
Emmanuel Lécharny
www.nextury.com