You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by guttikonda <di...@yahoo.com> on 2007/12/04 00:17:48 UTC
Problem sending message length in ByteBuffer
Hi
I am new to Mina.I have implemented a mina server to accept connections
from tcp/ip clients.The client only accepts network byte order messages with
message length.I figured that i can achieve it by using DataInputStream and
DataOutputStream readUTF() and writeUTF() methods respectively.
When receiving data from client i am successful in getting the correct one:
ByteBuffer rb = (ByteBuffer)msg;
reader = new DataInputStream(rb.asInputStream());
String inputMsg = reader.readUTF();
I want to achieve the same thing when sending the response.I have no idea
how i should wrap the DataOutputStream in my ByteBuffer to write it back to
the session.If i simply write my response string back it is not producing
the message length.Can anyone please help?
--
View this message in context: http://www.nabble.com/Problem-sending-message-length-in-ByteBuffer-tf4939850s16868.html#a14140681
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.
Re: Problem sending message length in ByteBuffer
Posted by Trustin Lee <tr...@gmail.com>.
Nice idea. Please go ahead! :D
On Dec 4, 2007 5:25 PM, Niklas Therning <ni...@trillian.se> wrote:
> +1
>
>
>
> Maarten Bosteels wrote:
> > Hello all,
> >
> > Right now we have the TextLineDecoder, which always works with a delimiter.
> > Wouldn't it be a good idea to add a PrefixedStringDecoder ?
> >
> > Something like this:
> >
> > public class PrefixedStringDecoder extends CumulativeProtocolDecoder {
> > ...
> > public PrefixedStringDecoder(int prefixLength, int maxDataLength,
> > Charset charset) {
> > this.prefixLength = prefixLength;
> > this.maxDataLength = maxDataLength;
> > this.charset = charset;
> > }
> >
> > protected boolean doDecode(IoSession session, ByteBuffer in,
> > ProtocolDecoderOutput out) throws Exception {
> > if (in.prefixedDataAvailable(prefixLength, maxDataLength)) {
> > String msg = in.getPrefixedString(prefixLength, charset.newDecoder());
> > out.write(msg);
> > return true;
> > } else {
> > return false;
> > }
> > }
> >
> > Maarten
> >
> > On Dec 4, 2007 8:51 AM, Niklas Therning <ni...@trillian.se> wrote:
> >
> >> guttikonda wrote:
> >>
> >>> Hi
> >>>
> >>> I am new to Mina.I have implemented a mina server to accept connections
> >>> from tcp/ip clients.The client only accepts network byte order messages with
> >>> message length.I figured that i can achieve it by using DataInputStream and
> >>> DataOutputStream readUTF() and writeUTF() methods respectively.
> >>>
> >>> When receiving data from client i am successful in getting the correct one:
> >>> ByteBuffer rb = (ByteBuffer)msg;
> >>> reader = new DataInputStream(rb.asInputStream());
> >>> String inputMsg = reader.readUTF();
> >>>
> >>> I want to achieve the same thing when sending the response.I have no idea
> >>> how i should wrap the DataOutputStream in my ByteBuffer to write it back to
> >>> the session.If i simply write my response string back it is not producing
> >>> the message length.Can anyone please help?
> >>>
> >>>
> >>>
> >>>
> >> Maybe something like
> >>
> >> ByteArrayOutputStream baos = new ByteArrayOutputStream();
> >> DataOutputStream writer = new DataOutputStream(baos);
> >> writer.writeUTF(msg);
> >> session.write(ByteBuffer.wrap(baos.toByteArray());
> >>
> >> would work?
> >>
> >> BUT, you couldn't you simply use ByteBuffer.getPrefixedString(...) and
> >> ByteBuffer.putPrefixedString(...) instead?
> >>
> >> AND, you should be aware that the network layer may decide to deliver
> >> your message in several packets. The result is that you cannot assume
> >> that a ByteBuffer passed in to your IoHandler.messageReceived() method
> >> contains a complete message. You should look into implementing a custom
> >> ProtocolDecoder. There's a tutorial on this. The beauty of using
> >> decoder/encoder is that you will only have to deal with Strings in your
> >> IoHandler.
> >>
> >> HTH
> >> Niklas
> >>
> >>
> >>
>
>
--
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6
Re: Problem sending message length in ByteBuffer
Posted by Niklas Therning <ni...@trillian.se>.
+1
Maarten Bosteels wrote:
> Hello all,
>
> Right now we have the TextLineDecoder, which always works with a delimiter.
> Wouldn't it be a good idea to add a PrefixedStringDecoder ?
>
> Something like this:
>
> public class PrefixedStringDecoder extends CumulativeProtocolDecoder {
> ...
> public PrefixedStringDecoder(int prefixLength, int maxDataLength,
> Charset charset) {
> this.prefixLength = prefixLength;
> this.maxDataLength = maxDataLength;
> this.charset = charset;
> }
>
> protected boolean doDecode(IoSession session, ByteBuffer in,
> ProtocolDecoderOutput out) throws Exception {
> if (in.prefixedDataAvailable(prefixLength, maxDataLength)) {
> String msg = in.getPrefixedString(prefixLength, charset.newDecoder());
> out.write(msg);
> return true;
> } else {
> return false;
> }
> }
>
> Maarten
>
> On Dec 4, 2007 8:51 AM, Niklas Therning <ni...@trillian.se> wrote:
>
>> guttikonda wrote:
>>
>>> Hi
>>>
>>> I am new to Mina.I have implemented a mina server to accept connections
>>> from tcp/ip clients.The client only accepts network byte order messages with
>>> message length.I figured that i can achieve it by using DataInputStream and
>>> DataOutputStream readUTF() and writeUTF() methods respectively.
>>>
>>> When receiving data from client i am successful in getting the correct one:
>>> ByteBuffer rb = (ByteBuffer)msg;
>>> reader = new DataInputStream(rb.asInputStream());
>>> String inputMsg = reader.readUTF();
>>>
>>> I want to achieve the same thing when sending the response.I have no idea
>>> how i should wrap the DataOutputStream in my ByteBuffer to write it back to
>>> the session.If i simply write my response string back it is not producing
>>> the message length.Can anyone please help?
>>>
>>>
>>>
>>>
>> Maybe something like
>>
>> ByteArrayOutputStream baos = new ByteArrayOutputStream();
>> DataOutputStream writer = new DataOutputStream(baos);
>> writer.writeUTF(msg);
>> session.write(ByteBuffer.wrap(baos.toByteArray());
>>
>> would work?
>>
>> BUT, you couldn't you simply use ByteBuffer.getPrefixedString(...) and
>> ByteBuffer.putPrefixedString(...) instead?
>>
>> AND, you should be aware that the network layer may decide to deliver
>> your message in several packets. The result is that you cannot assume
>> that a ByteBuffer passed in to your IoHandler.messageReceived() method
>> contains a complete message. You should look into implementing a custom
>> ProtocolDecoder. There's a tutorial on this. The beauty of using
>> decoder/encoder is that you will only have to deal with Strings in your
>> IoHandler.
>>
>> HTH
>> Niklas
>>
>>
>>
Re: Problem sending message length in ByteBuffer
Posted by Maarten Bosteels <mb...@gmail.com>.
Hello all,
Right now we have the TextLineDecoder, which always works with a delimiter.
Wouldn't it be a good idea to add a PrefixedStringDecoder ?
Something like this:
public class PrefixedStringDecoder extends CumulativeProtocolDecoder {
...
public PrefixedStringDecoder(int prefixLength, int maxDataLength,
Charset charset) {
this.prefixLength = prefixLength;
this.maxDataLength = maxDataLength;
this.charset = charset;
}
protected boolean doDecode(IoSession session, ByteBuffer in,
ProtocolDecoderOutput out) throws Exception {
if (in.prefixedDataAvailable(prefixLength, maxDataLength)) {
String msg = in.getPrefixedString(prefixLength, charset.newDecoder());
out.write(msg);
return true;
} else {
return false;
}
}
Maarten
On Dec 4, 2007 8:51 AM, Niklas Therning <ni...@trillian.se> wrote:
> guttikonda wrote:
> > Hi
> >
> > I am new to Mina.I have implemented a mina server to accept connections
> > from tcp/ip clients.The client only accepts network byte order messages with
> > message length.I figured that i can achieve it by using DataInputStream and
> > DataOutputStream readUTF() and writeUTF() methods respectively.
> >
> > When receiving data from client i am successful in getting the correct one:
> > ByteBuffer rb = (ByteBuffer)msg;
> > reader = new DataInputStream(rb.asInputStream());
> > String inputMsg = reader.readUTF();
> >
> > I want to achieve the same thing when sending the response.I have no idea
> > how i should wrap the DataOutputStream in my ByteBuffer to write it back to
> > the session.If i simply write my response string back it is not producing
> > the message length.Can anyone please help?
> >
> >
> >
> Maybe something like
>
> ByteArrayOutputStream baos = new ByteArrayOutputStream();
> DataOutputStream writer = new DataOutputStream(baos);
> writer.writeUTF(msg);
> session.write(ByteBuffer.wrap(baos.toByteArray());
>
> would work?
>
> BUT, you couldn't you simply use ByteBuffer.getPrefixedString(...) and
> ByteBuffer.putPrefixedString(...) instead?
>
> AND, you should be aware that the network layer may decide to deliver
> your message in several packets. The result is that you cannot assume
> that a ByteBuffer passed in to your IoHandler.messageReceived() method
> contains a complete message. You should look into implementing a custom
> ProtocolDecoder. There's a tutorial on this. The beauty of using
> decoder/encoder is that you will only have to deal with Strings in your
> IoHandler.
>
> HTH
> Niklas
>
>
Re: Problem sending message length in ByteBuffer
Posted by guttikonda <di...@yahoo.com>.
Thanks Marteen.Actually i tried to write one on my own and succeeded after
seeing your previous suggestion.But it was a little bit different when
compared to this one. I will try to change to this one and see how it goes.
Divya.
Maarten Bosteels wrote:
>
> Hi Guttikonda,
>
> I have added a PrefixedStringCodecFactory to the trunk.
> If you're using MINA 1.x, it should be easy to port.
>
> Maarten
>
> On Dec 4, 2007 7:26 PM, Niklas Therning <ni...@trillian.se> wrote:
>> guttikonda wrote:
>> > Thanks for all the suggestions.I tried using ByteArrayInputStream and
>> it
>> > worked.
>> > ByteArrayOutputStream baos = new ByteArrayOutputStream();
>> > DataOutputStream writer = new DataOutputStream(baos);
>> > writer.writeUTF(msg);
>> > session.write(ByteBuffer.wrap(baos.toByteArray());
>> >
>> > AND, about the quote saying that "the network layer may decide to
>> deliver
>> > your message in several packets. The result is that you cannot assume
>> > that a ByteBuffer passed in to your IoHandler.messageReceived() method
>> > contains a complete message".Is it always or when using
>> > ByteBuffer.getPrefixedString(...) method only? Please help.
>> >
>>
>> This is always the case. Please have a look at the Protocol Codec
>> tutorial:
>>
>> http://mina.apache.org/tutorial-on-protocolcodecfilter.html
>>
>> and the Reverser and SumUp examples:
>>
>> http://mina.apache.org/documentation.html#Documentation-Examples
>>
>> /Niklas
>>
>>
>
>
--
View this message in context: http://www.nabble.com/Problem-sending-message-length-in-ByteBuffer-tf4939850s16868.html#a14194926
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.
Re: Problem sending message length in ByteBuffer
Posted by Maarten Bosteels <mb...@gmail.com>.
Hi Guttikonda,
I have added a PrefixedStringCodecFactory to the trunk.
If you're using MINA 1.x, it should be easy to port.
Maarten
On Dec 4, 2007 7:26 PM, Niklas Therning <ni...@trillian.se> wrote:
> guttikonda wrote:
> > Thanks for all the suggestions.I tried using ByteArrayInputStream and it
> > worked.
> > ByteArrayOutputStream baos = new ByteArrayOutputStream();
> > DataOutputStream writer = new DataOutputStream(baos);
> > writer.writeUTF(msg);
> > session.write(ByteBuffer.wrap(baos.toByteArray());
> >
> > AND, about the quote saying that "the network layer may decide to deliver
> > your message in several packets. The result is that you cannot assume
> > that a ByteBuffer passed in to your IoHandler.messageReceived() method
> > contains a complete message".Is it always or when using
> > ByteBuffer.getPrefixedString(...) method only? Please help.
> >
>
> This is always the case. Please have a look at the Protocol Codec tutorial:
>
> http://mina.apache.org/tutorial-on-protocolcodecfilter.html
>
> and the Reverser and SumUp examples:
>
> http://mina.apache.org/documentation.html#Documentation-Examples
>
> /Niklas
>
>
Re: Problem sending message length in ByteBuffer
Posted by Niklas Therning <ni...@trillian.se>.
guttikonda wrote:
> Thanks for all the suggestions.I tried using ByteArrayInputStream and it
> worked.
> ByteArrayOutputStream baos = new ByteArrayOutputStream();
> DataOutputStream writer = new DataOutputStream(baos);
> writer.writeUTF(msg);
> session.write(ByteBuffer.wrap(baos.toByteArray());
>
> AND, about the quote saying that "the network layer may decide to deliver
> your message in several packets. The result is that you cannot assume
> that a ByteBuffer passed in to your IoHandler.messageReceived() method
> contains a complete message".Is it always or when using
> ByteBuffer.getPrefixedString(...) method only? Please help.
>
This is always the case. Please have a look at the Protocol Codec tutorial:
http://mina.apache.org/tutorial-on-protocolcodecfilter.html
and the Reverser and SumUp examples:
http://mina.apache.org/documentation.html#Documentation-Examples
/Niklas
Re: Problem sending message length in ByteBuffer
Posted by guttikonda <di...@yahoo.com>.
Thanks for all the suggestions.I tried using ByteArrayInputStream and it
worked.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream writer = new DataOutputStream(baos);
writer.writeUTF(msg);
session.write(ByteBuffer.wrap(baos.toByteArray());
AND, about the quote saying that "the network layer may decide to deliver
your message in several packets. The result is that you cannot assume
that a ByteBuffer passed in to your IoHandler.messageReceived() method
contains a complete message".Is it always or when using
ByteBuffer.getPrefixedString(...) method only? Please help.
Niklas Therning wrote:
>
> guttikonda wrote:
>> Hi
>>
>> I am new to Mina.I have implemented a mina server to accept
>> connections
>> from tcp/ip clients.The client only accepts network byte order messages
>> with
>> message length.I figured that i can achieve it by using DataInputStream
>> and
>> DataOutputStream readUTF() and writeUTF() methods respectively.
>>
>> When receiving data from client i am successful in getting the correct
>> one:
>> ByteBuffer rb = (ByteBuffer)msg;
>> reader = new DataInputStream(rb.asInputStream());
>> String inputMsg = reader.readUTF();
>>
>> I want to achieve the same thing when sending the response.I have no idea
>> how i should wrap the DataOutputStream in my ByteBuffer to write it back
>> to
>> the session.If i simply write my response string back it is not producing
>> the message length.Can anyone please help?
>>
>>
>>
> Maybe something like
>
> ByteArrayOutputStream baos = new ByteArrayOutputStream();
> DataOutputStream writer = new DataOutputStream(baos);
> writer.writeUTF(msg);
> session.write(ByteBuffer.wrap(baos.toByteArray());
>
> would work?
>
> BUT, you couldn't you simply use ByteBuffer.getPrefixedString(...) and
> ByteBuffer.putPrefixedString(...) instead?
>
> AND, you should be aware that the network layer may decide to deliver
> your message in several packets. The result is that you cannot assume
> that a ByteBuffer passed in to your IoHandler.messageReceived() method
> contains a complete message. You should look into implementing a custom
> ProtocolDecoder. There's a tutorial on this. The beauty of using
> decoder/encoder is that you will only have to deal with Strings in your
> IoHandler.
>
> HTH
> Niklas
>
>
>
> -----
> Niklas Therning
> www.spamdrain.net
>
--
View this message in context: http://www.nabble.com/Problem-sending-message-length-in-ByteBuffer-tf4939850s16868.html#a14152909
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.
Re: Problem sending message length in ByteBuffer
Posted by Niklas Therning <ni...@trillian.se>.
guttikonda wrote:
> Hi
>
> I am new to Mina.I have implemented a mina server to accept connections
> from tcp/ip clients.The client only accepts network byte order messages with
> message length.I figured that i can achieve it by using DataInputStream and
> DataOutputStream readUTF() and writeUTF() methods respectively.
>
> When receiving data from client i am successful in getting the correct one:
> ByteBuffer rb = (ByteBuffer)msg;
> reader = new DataInputStream(rb.asInputStream());
> String inputMsg = reader.readUTF();
>
> I want to achieve the same thing when sending the response.I have no idea
> how i should wrap the DataOutputStream in my ByteBuffer to write it back to
> the session.If i simply write my response string back it is not producing
> the message length.Can anyone please help?
>
>
>
Maybe something like
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream writer = new DataOutputStream(baos);
writer.writeUTF(msg);
session.write(ByteBuffer.wrap(baos.toByteArray());
would work?
BUT, you couldn't you simply use ByteBuffer.getPrefixedString(...) and
ByteBuffer.putPrefixedString(...) instead?
AND, you should be aware that the network layer may decide to deliver
your message in several packets. The result is that you cannot assume
that a ByteBuffer passed in to your IoHandler.messageReceived() method
contains a complete message. You should look into implementing a custom
ProtocolDecoder. There's a tutorial on this. The beauty of using
decoder/encoder is that you will only have to deal with Strings in your
IoHandler.
HTH
Niklas