KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > mina > example > echoserver > EchoProtocolHandler


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

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 /**
34  * {@link IoHandler} implementation for echo server.
35  *
36  * @author The Apache Directory Project (mina-dev@directory.apache.org)
37  * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (금, 13 7월 2007) $,
38  */

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         // We're going to use SSL negotiation notification.
52
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 JavaDoc cause) {
61         cause.printStackTrace();
62         session.close();
63     }
64
65     public void messageReceived(IoSession session, Object JavaDoc message)
66             throws Exception JavaDoc {
67         if (!(message instanceof ByteBuffer)) {
68             return;
69         }
70
71         ByteBuffer rb = (ByteBuffer) message;
72         // Write the received data back to remote peer
73
ByteBuffer wb = ByteBuffer.allocate(rb.remaining());
74         wb.put(rb);
75         wb.flip();
76         session.write(wb);
77     }
78 }
Popular Tags