Thanks for all you guys' kind help,I have check my code again that I'm sure
that I had done it in the way you guys suggested.But it doesn't work yet
Now I'm pasting all my code
=========================AbstractStringDecoder ============================
public abstract class AbstractStringDecoder implements MessageDecoder
{
protected AbstractStringDecoder()
{
System.out.println("AbstractStringDecoder Constructor");
}
public MessageDecoderResult decodable(IoSession session, ByteBuffer in)
{
System.out.println("decodable");
int oldPos = in.position();
int sPos = -1;
int ePos = -1;
in.position(0);
while (in.hasRemaining())
{
byte b = in.get();
switch (b)
{
case '<':
sPos = in.position() - 1;
break;
case '>':
ePos = in.position() - 1;
break;
default:
}
}
in.position(oldPos);
if (sPos > -1 && ePos > -1)
{
if (sPos < ePos)
{
return MessageDecoderResult.OK;
}
}
return MessageDecoderResult.NEED_DATA;
}
public MessageDecoderResult decode(IoSession session, ByteBuffer in,
ProtocolDecoderOutput out) throws Exception
{
System.out.println("decode");
// Try to decode body
String m = decodeBody(session, in);
if (m == null)
{
return MessageDecoderResult.NEED_DATA;
}
out.write(m);
return MessageDecoderResult.OK;
}
/**
* @return <tt>null</tt> if the whole body is not read yet
*/
protected abstract String decodeBody(IoSession session, ByteBuffer in);
}
===============================================================
============================StringDecoder =======================
public class StringDecoder extends AbstractStringDecoder
{
public StringDecoder()
{
System.out.println("StringDecoder Constructor");
}
public String decodeBody(IoSession session, ByteBuffer in)
{
System.out.println("decodeBody");
Charset charset = Charset.forName("UTF-8");
CharsetDecoder decoder = charset.newDecoder();
try
{
String m = in.getString(decoder);
return m;
}
catch (Exception e)
{
return null;
}
}
public void dispose() throws Exception
{
}
public void finishDecode(IoSession arg0, ProtocolDecoderOutput arg1)
throws Exception
{
}
}
===============================================================
And EVERYTIME the server receives a message from client,only the
"AbstractStringDecoder Constructor"AND "StringDecoder Constructor"
were printed out, there are no more other console output
Thanks Advance
----- Original Message -----
From: "Hieu Phan Thanh" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Wednesday, October 11, 2006 5:48 PM
Subject: RE: I cann't work with ProtocolCodecFilter normally,asking for your
help!!
Hello,
I think you did not call the decodeBody() method in the decode() of the
AbstractClass.
Please review below:
===============================================
public MessageDecoderResult decode(IoSession session, ByteBuffer in,
ProtocolDecoderOutput out) throws Exception {
System.out.println("333");
decodeBody(session, in);
}
protected abstract String decodeBody(IoSession session, ByteBuffer in);
===================================================
Thanks & best regards,
Hieu Phan.
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Sent: Wednesday, October 11, 2006 4:20 PM
To: [email protected]
Subject: Re: I cann't work with ProtocolCodecFilter normally,asking for
your help!!
Jeroen Brattinga thank you for your instructions
But no matter MessageDecoderResult is OK or NOT,even the first
statement:System.out.println(some characters) had not be reached.It's so
confused!
----- Original Message -----
From: "Jeroen Brattinga" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Wednesday, October 11, 2006 5:11 PM
Subject: Re: I cann't work with ProtocolCodecFilter normally,asking for
your
help!!
> You should look closer to the SumUp example. But in a nutshell it comes
> down
> to this:
> - for every incoming data the decoder classes are called
> - for every decoder class the --decodable-- method is checked. If the
> return
> value is MessageDecoderResult.OK then the --decode-- method is called.
> - the --decodeBody-- method is never called from the framework (!), but
in
> the SumUp example it is called from the --decode-- method from the
> abstract
> class (to provide easier subclasses that only have to implement the
> --decodeBody-- method.
>
> In your case you have forgotten to return the MessageDecoderResult code
> and
> don't call the --decodeBody-- method from the --decode-- method.
>
>
> Jeroen Brattinga
>
> 2006/10/11, 凌晨 <[EMAIL PROTECTED]>:
>>
>> Hello Everyone:
>> I cann't work with ProtocolCodecFilter normally,asking for your help!!
>>
>> First the add filter in Main Class code segment:
>> public static void main(String[] args) throws Exception
>> {
>> acceptor = new SocketAcceptor();
>> IoAcceptorConfig config = new SocketAcceptorConfig();
>> ((SocketAcceptorConfig) config).setReuseAddress(true);
>> config.setDisconnectOnUnbind(true);
>> config.getFilterChain().addLast("codec",
>> new ProtocolCodecFilter(new ServerProtocolCodecFactory()));
>> acceptor.bind(new InetSocketAddress(PORT), new ProtocolHandler(),
>> config);
>> System.out.println("Listening on port " + PORT);
>> }
>>
>> Second The AbstractStringDecoder
>> public abstract class AbstractStringDecoder implements MessageDecoder
>> {
>> protected AbstractStringDecoder()
>> {
>> System.out.println("AbstractStringDecoder");
>> }
>>
>> public MessageDecoderResult decodable(IoSession session, ByteBuffer
>> in)
>> {
>> System.out.println("decodable");
>> }
>>
>> public MessageDecoderResult decode(IoSession session, ByteBuffer in,
>> ProtocolDecoderOutput out) throws Exception
>> {
>> System.out.println("333");
>> }
>>
>> protected abstract String decodeBody(IoSession session, ByteBuffer
>> in);
>> }
>>
>> The Third StringDecoder
>> public class StringDecoder extends AbstractStringDecoder
>> {
>> public StringDecoder()
>> {
>> System.out.println("decode");
>> }
>>
>> public String decodeBody(IoSession session, ByteBuffer in)
>> {
>> System.out.println("111");
>> }
>>
>> public void dispose() throws Exception
>> {
>> System.out.println("333");
>> }
>>
>> public void finishDecode(IoSession arg0, ProtocolDecoderOutput arg1)
>> throws Exception
>> {
>> System.out.println("222");
>> }
>> }
>>
>> Then the ServerProtocolCodecFactory
>> public class ServerProtocolCodecFactory extends
>> DemuxingProtocolCodecFactory
>> implements ProtocolCodecFactory
>> {
>> public ServerProtocolCodecFactory()
>> {
>> super.register(StringDecoder.class);
>> super.register(StringEncoder.class);
>> }
>> }
>> But When I send Message to the server,StringDecoder.decodeBody() never
be
>> invoked.I biult the server on MINA1.0 JDK1.5
>> Thanks In Advance!
>>
>>
>
>
> --
> Jeroen Brattinga
>