1 20 package org.apache.mina.example.echoserver; 21 22 import org.apache.mina.common.ByteBuffer; 23 import org.apache.mina.common.IdleStatus; 24 import org.apache.mina.common.IoHandler; 25 import org.apache.mina.common.IoHandlerAdapter; 26 import org.apache.mina.common.IoSession; 27 import org.apache.mina.common.TransportType; 28 import org.apache.mina.filter.SSLFilter; 29 import org.apache.mina.transport.socket.nio.SocketSessionConfig; 30 import org.slf4j.Logger; 31 import org.slf4j.LoggerFactory; 32 33 39 public class EchoProtocolHandler extends IoHandlerAdapter { 40 private static final Logger log = LoggerFactory 41 .getLogger(EchoProtocolHandler.class); 42 43 public void sessionCreated(IoSession session) { 44 if (session.getTransportType() == TransportType.SOCKET) { 45 ((SocketSessionConfig) session.getConfig()) 46 .setReceiveBufferSize(2048); 47 } 48 49 session.setIdleTime(IdleStatus.BOTH_IDLE, 10); 50 51 session.setAttribute(SSLFilter.USE_NOTIFICATION); 53 } 54 55 public void sessionIdle(IoSession session, IdleStatus status) { 56 log.info("*** IDLE #" + session.getIdleCount(IdleStatus.BOTH_IDLE) 57 + " ***"); 58 } 59 60 public void exceptionCaught(IoSession session, Throwable cause) { 61 cause.printStackTrace(); 62 session.close(); 63 } 64 65 public void messageReceived(IoSession session, Object message) 66 throws Exception { 67 if (!(message instanceof ByteBuffer)) { 68 return; 69 } 70 71 ByteBuffer rb = (ByteBuffer) message; 72 ByteBuffer wb = ByteBuffer.allocate(rb.remaining()); 74 wb.put(rb); 75 wb.flip(); 76 session.write(wb); 77 } 78 } | Popular Tags |