01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
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.IdleStatus;
23: import org.apache.mina.common.IoBuffer;
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.filter.ssl.SslFilter;
28: import org.slf4j.Logger;
29: import org.slf4j.LoggerFactory;
30:
31: /**
32: * {@link IoHandler} implementation for echo server.
33: *
34: * @author The Apache MINA Project (dev@mina.apache.org)
35: * @version $Rev: 616100 $, $Date: 2008-01-28 15:58:32 -0700 (Mon, 28 Jan 2008) $,
36: */
37: public class EchoProtocolHandler extends IoHandlerAdapter {
38: private final Logger logger = LoggerFactory.getLogger(getClass());
39:
40: @Override
41: public void sessionCreated(IoSession session) {
42: session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
43:
44: // We're going to use SSL negotiation notification.
45: session.setAttribute(SslFilter.USE_NOTIFICATION);
46: }
47:
48: @Override
49: public void sessionClosed(IoSession session) throws Exception {
50: logger.info("CLOSED");
51: }
52:
53: @Override
54: public void sessionOpened(IoSession session) throws Exception {
55: logger.info("OPENED");
56: }
57:
58: @Override
59: public void sessionIdle(IoSession session, IdleStatus status) {
60: logger.info("*** IDLE #"
61: + session.getIdleCount(IdleStatus.BOTH_IDLE) + " ***");
62: }
63:
64: @Override
65: public void exceptionCaught(IoSession session, Throwable cause) {
66: session.close();
67: }
68:
69: @Override
70: public void messageReceived(IoSession session, Object message)
71: throws Exception {
72: // Write the received data back to remote peer
73: session.write(((IoBuffer) message).duplicate());
74: }
75: }
|