Hi,
Thanks for such a quick reply :)

On 5/30/05, Trustin Lee <[EMAIL PROTECTED]> 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

Reply via email to