Thank you very much Hieu !It does works!
----- Original Message ----- From: "Hieu Phan Thanh" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Thursday, October 12, 2006 10:59 AM
Subject: FW: I cann't work with ProtocolCodecFilter normally,asking for your
help!!


Hello, I checked your code. The "decodable" could be invoked however the
"decodeBody" didn't. I think there is something wrong in your
"decodable" method. I tried to replace the code in "decodable" method:
=======================================
in.position(oldPos);
 if (sPos > -1 && ePos > -1)
 {
  if (sPos < ePos)
  {
   return MessageDecoderResult.OK;
  }
 }
 return MessageDecoderResult.NEED_DATA;
======================================

=========>>>>> INTO:

=======================================
 return MessageDecoderResult.OK;
======================================

=> the result: the "decodeBody" is called.

Please:
- check the "decodable" once again more carefully.
- how about the class "ProtocolCodecFactory" and "IoHandler" classes.
I could help you more if you send over all the project source-code.
BTW, I think that it would be better if you work with the "sumup"
example more closely. You will find some of experience.

Hope it could help you.
Hieu Phan.

Below information is my test code based on yours:
------------------------------
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.OK;
}

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);
}
------------------------------
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 session,
ProtocolDecoderOutput out)
throws Exception {
}

}
------------------------------
public class ServerProtocolCodecFactory extends
DemuxingProtocolCodecFactory {

public ServerProtocolCodecFactory() {
super.register(StringDecoder.class);
}

}
------------------------------
public class ServerSessionHandler extends IoHandlerAdapter
{
   public void sessionOpened( IoSession session )
   {
       // set idle time to 60 seconds
       session.setIdleTime( IdleStatus.BOTH_IDLE, 60 );

       // initial sum is zero
       session.setAttachment( new Integer( 0 ) );
   }

   public void messageReceived( IoSession session, Object message )
   {
   SessionLog.info( session, "RCVD: " + message);
   }

   public void sessionIdle( IoSession session, IdleStatus status )
   {
       SessionLog.info( session, "Disconnecting the idle." );
       // disconnect an idle client
       session.close();
   }

   public void exceptionCaught( IoSession session, Throwable cause )
   {
   SessionLog.error(session, "EXCEPTIOIN: ", cause);
       // close the connection on exceptional situation
       session.close();
   }
}
------------------------------
public class Server
{
   private static final int SERVER_PORT = 8080;
   // Set this to false to use object serialization instead of custom
codec.
   private static final boolean USE_CUSTOM_CODEC = true;

   public static void main( String[] args ) throws Throwable
   {
       IoAcceptor acceptor = new SocketAcceptor();

       // Prepare the service configuration.
       SocketAcceptorConfig cfg = new SocketAcceptorConfig();
       cfg.setReuseAddress( true );
       if( USE_CUSTOM_CODEC )
       {
           cfg.getFilterChain().addLast(
                   "codec",
                   new ProtocolCodecFilter( new
ServerProtocolCodecFactory(  ) ) );
       }
       else
       {
           cfg.getFilterChain().addLast(
                   "codec",
                   new ProtocolCodecFilter( new
ObjectSerializationCodecFactory() ) );
       }
       cfg.getFilterChain().addLast( "logger", new LoggingFilter() );

       acceptor.bind(
               new InetSocketAddress( SERVER_PORT ),
               new ServerSessionHandler( ), cfg );

       System.out.println( "Listening on port " + SERVER_PORT );
   }
}
------------------------------


Reply via email to