You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@directory.apache.org by Sander Aiaots <ai...@gmail.com> on 2005/05/30 16:42:02 UTC

[mina] ByteBuffer and MessageDecoder/MessageEncoders

Hi,

I have a question about MessageDecoder/MessageEncoder, 
For example I create client, what is associated with MessageEncodert
that transforms String into bytes and one other Object also into
bytes.
So I send these two objects away: sess.write(myobject);//22 bytes
sess.write("12345342711"); //11 bytes
But MessageDecoder decode is called once with ByteBuffer containing 33
bytes of data?
Why isn't decode called twice with 22 bytes of data, and then 11?
Or sometimes, I saw that, decode is called twice, but both times It
contains 33 bytes of data.
Or am I misunderstood, that TCP actually is like stream, so I must
build mechanism how-to separate messages? Or is there some kind
implementations exist in MINA already?

On decode I read bytes out: byte[] buf = new byte[in.remaining()];
in.get(buf, in.position(), in.remaining()); So why I am getting this
ByteBuffer twice with same data?

Hopefully my question was understandable.
Best regards,
Sander Aiaots

Re: [mina] ByteBuffer and MessageDecoder/MessageEncoders

Posted by Sander Aiaots <ai...@gmail.com>.
Hi Trustin!

> > It seems that, this 0.7.2-SNAPSHOT works much better :) Even
> > messageRecived event was fired. 
> > But I already have another question, if I call sess.close(); from
> > messageRecved() then no sessionClosed event is fired, neither client
> > or server side?
>  
>   
> Really? Could you give me some test code that reproduces this problem?
>  

Well, acutally it seems to be my own problem, I hoped sessionClosed
event from MessageHandler, but it it was invoked in
DemuxingProtocolHandler, but it would sound reasonable if
sessionClosed is also invoked from MessageHandler what is registred
with registerMessageType.

Best regards,
Sander.

Re: [mina] ByteBuffer and MessageDecoder/MessageEncoders

Posted by Trustin Lee <tr...@gmail.com>.
Hi Sander,

2005/5/31, Sander Aiaots <ai...@gmail.com>: 
> 
> Hi Trustin!
> 
> It seems that, this 0.7.2-SNAPSHOT works much better :) Even
> messageRecived event was fired.
> But I already have another question, if I call sess.close(); from
> messageRecved() then no sessionClosed event is fired, neither client
> or server side?

 Really? Could you give me some test code that reproduces this problem?

And one more question, Dose MINA provides some mechanism to implement
> login, like it is in SSH first user needs to auth himself and then go
> on with other activities.

 Not yet though we have SSL filter. We have a plan to implement SASL filter, 
but we're not even close to it.
 Thanks,
Trustin
-- 
what we call human nature is actually human habit
--
http://gleamynode.net/

Re: [mina] ByteBuffer and MessageDecoder/MessageEncoders

Posted by Sander Aiaots <ai...@gmail.com>.
Hi,
Thanks for such a quick reply :)

On 5/30/05, Trustin Lee <tr...@gmail.com> wrote:
> Hi Sander,
>   
> You can get 33 bytes even if you wrote it separately.  That is nature of
> TCP/IP.  Your MesageDecoder should be able to recognize the end of message
> such as specifying the length of message in itself. 
>   
So It clear with that :)
>   
> I didn't get it what you're talking.  Could you please explain again? 
Well, I didn't quite catch how these ByteBuffers and Demuxing stuff
are working, anyway I can't understand why is decode called twice for
one sess.write("test");
And other problem is, that messageRecived() is not called at all ?

Test program output in screen:
begin decode>java.nio.DirectByteBuffer[pos=0 lim=4 cap=16]
end decode>java.nio.DirectByteBuffer[pos=4 lim=4 cap=16]
begin decode>java.nio.DirectByteBuffer[pos=0 lim=4 cap=16]
end decode>java.nio.DirectByteBuffer[pos=4 lim=4 cap=16]

Code siis somting like that:
// in main method:
Service service = new Service("testtt", TransportType.SOCKET, 8888);
		registry.bind(service, new TestProvider());
		IoProtocolConnector con = new IoProtocolConnector(new SocketConnector());
		ProtocolSession sess = con.connect(new
InetSocketAddress("localhost",8888), new TestProvider());
		sess.write("test");

// protocol provider
public class TestProvider implements ProtocolProvider {
	DemuxingProtocolCodecFactory factory = null;
	public ProtocolCodecFactory getCodecFactory() {
		if(factory == null) {
			factory = new DemuxingProtocolCodecFactory();
			factory.register(new TestEncoder());
			factory.register(new TestDecoder());
		}
		return factory;
	}
	public ProtocolHandler getHandler() {
		return new TestProtocolHandler();
	}
}

public class TestMessageHandler implements MessageHandler {
	public void messageReceived(ProtocolSession arg0, Object arg1) {
		System.out.println(arg1);
	}
}

public class TestEncoder implements MessageEncoder {
	HashSet types;
	public Set getMessageTypes() {
		if(types == null) {
			types = new HashSet(1);
			types.add(String.class);
		}
		return types;
	}
	public void encode(ProtocolSession arg0, Object arg1,
ProtocolEncoderOutput arg2) throws ProtocolViolationException {
		if(arg1 instanceof String) {
			try {
				arg2.write(ByteBuffer.wrap(arg1.toString().getBytes("utf-8")));
			} catch (UnsupportedEncodingException e) {
			}
		}else {
			throw new ProtocolViolationException("Stringidega tegeleme");
		}

	}
}

public class TestDecoder implements MessageDecoder {
	public MessageDecoderResult decodable(ProtocolSession arg0, ByteBuffer arg1) {
		return OK;
	}
	public MessageDecoderResult decode(ProtocolSession sess, ByteBuffer data,
			ProtocolDecoderOutput out) throws ProtocolViolationException {
		System.out.println("begin decode>"+data);
		byte[] buf = new byte[data.remaining()];
		data.get(buf, data.position(), data.remaining());
		try {
			out.write(new String(buf, "utf-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		System.out.println("end decode>"+data);
		return OK;
	}
}

Best regareds,
Sander Aiaots

Re: [mina] ByteBuffer and MessageDecoder/MessageEncoders

Posted by Trustin Lee <tr...@gmail.com>.
Hi Sander,

2005/5/30, Sander Aiaots aiaots@gmail.com: 
> 
> Or am I misunderstood, that TCP actually is like stream, so I must
> build mechanism how-to separate messages? Or is there some kind
> implementations exist in MINA already?

 You can get 33 bytes even if you wrote it separately. That is nature of 
TCP/IP. Your MesageDecoder should be able to recognize the end of message 
such as specifying the length of message in itself.
 
> On decode I read bytes out: byte[] buf = new byte[in.remaining()];
> in.get(buf, in.position(), in.remaining()); So why I am getting this
> ByteBuffer twice with same data?

 I didn't get it what you're talking. Could you please explain again?
 Thanks,
Trustin
-- 
what we call human nature is actually human habit
--
http://gleamynode.net/