001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.example.proxy.telnet;
021
022import java.io.BufferedReader;
023import java.io.InputStreamReader;
024
025import org.apache.mina.core.session.IoSession;
026import org.apache.mina.proxy.AbstractProxyIoHandler;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030/**
031 * TelnetSessionHandler.java - Telnet session handler.
032 * 
033 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
034 * @since MINA 2.0.0-M3
035 */
036public class TelnetSessionHandler extends AbstractProxyIoHandler {
037    private final static Logger logger = LoggerFactory
038            .getLogger(TelnetSessionHandler.class);
039
040    /**
041     * Default constructor.
042     */
043    public TelnetSessionHandler() {
044    }
045
046    /**
047     * {@inheritDoc} 
048     */
049    @Override
050    public void sessionCreated(IoSession session) throws Exception {
051        logger.debug("CLIENT - Session created: " + session);
052    }
053
054    /**
055     * {@inheritDoc} 
056     */
057    @Override
058    public void proxySessionOpened(IoSession session) throws Exception {
059        logger.debug("CLIENT - Session opened: " + session);
060        final IoSession _session = session;
061        // Enter typing loop
062        new Thread(new Runnable() {
063            public void run() {
064                InputStreamReader isr = new InputStreamReader(System.in);
065                BufferedReader br = new BufferedReader(isr);
066
067                while (!_session.isClosing()) {
068                    try {
069                        String line = br.readLine();
070                        if (line != null) {
071                            _session.write(line);
072                        }
073                    } catch (Exception e) {
074                        break;
075                    }
076                }
077
078                _session.close(true);
079            }
080
081        }).start();
082    }
083
084    /**
085     * {@inheritDoc} 
086     */
087    @Override
088    public void messageReceived(IoSession session, Object message) {
089        //System.out.println((String) message);
090    }
091
092    /**
093     * {@inheritDoc} 
094     */
095    @Override
096    public void sessionClosed(IoSession session) throws Exception {
097        logger.debug("CLIENT - Session closed");
098    }
099
100    /**
101     * {@inheritDoc} 
102     */
103    @Override
104    public void exceptionCaught(IoSession session, Throwable cause) {
105        logger.debug("CLIENT - Exception caught");
106        //cause.printStackTrace();
107        session.close(true);
108    }
109}