View Javadoc

    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.proxy.telnet;
    21
    22  import java.io.BufferedReader;
    23  import java.io.InputStreamReader;
    24
    25  import org.apache.mina.core.session.IoSession;
    26  import org.apache.mina.proxy.AbstractProxyIoHandler;
    27  import org.slf4j.Logger;
    28  import org.slf4j.LoggerFactory;
    29
    30  /**
    31   * TelnetSessionHandler.java - Telnet session handler.
    32   *
    33   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    34   * @since MINA 2.0.0-M3
    35   */
    36  public class TelnetSessionHandler extends AbstractProxyIoHandler {
    37      private final static Logger logger = LoggerFactory
    38              .getLogger(TelnetSessionHandler.class);
    39
    40      /**
    41       * Default constructor.
    42       */
    43      public TelnetSessionHandler() {
    44      }
    45
    46      /**
    47       * {@inheritDoc}
    48       */
    49      @Override
    50      public void sessionCreated(IoSession session) throws Exception {
    51          logger.debug("CLIENT - Session created: " + session);
    52      }
    53
    54      /**
    55       * {@inheritDoc}
    56       */
    57      @Override
    58      public void proxySessionOpened(IoSession session) throws Exception {
    59          logger.debug("CLIENT - Session opened: " + session);
    60          final IoSession _session = session;
    61          // Enter typing loop
    62          new Thread(new Runnable() {
    63              public void run() {
    64                  InputStreamReader isr = new InputStreamReader(System.in);
    65                  BufferedReader br = new BufferedReader(isr);
    66
    67                  while (!_session.isClosing()) {
    68                      try {
    69                          String line = br.readLine();
    70                          if (line != null) {
    71                              _session.write(line);
    72                          }
    73                      } catch (Exception e) {
    74                          break;
    75                      }
    76                  }
    77
    78                  _session.close(true);
    79              }
    80
    81          }).start();
    82      }
    83
    84      /**
    85       * {@inheritDoc}
    86       */
    87      @Override
    88      public void messageReceived(IoSession session, Object message) {
    89          //System.out.println((String) message);
    90      }
    91
    92      /**
    93       * {@inheritDoc}
    94       */
    95      @Override
    96      public void sessionClosed(IoSession session) throws Exception {
    97          logger.debug("CLIENT - Session closed");
    98      }
    99
    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 }