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 The Apache MINA Project (dev@mina.apache.org)
34   * @version $Rev$, $Date$
35   * @since MINA 2.0.0-M3
36   */
37  public class TelnetSessionHandler extends AbstractProxyIoHandler {
38      private final static Logger logger = LoggerFactory
39              .getLogger(TelnetSessionHandler.class);
40  
41      /**
42       * Default constructor.
43       */
44      public TelnetSessionHandler() {
45      }
46  
47      /**
48       * {@inheritDoc} 
49       */
50      @Override
51      public void sessionCreated(IoSession session) throws Exception {
52          logger.debug("CLIENT - Session created: " + session);
53      }
54  
55      /**
56       * {@inheritDoc} 
57       */
58      @Override
59      public void proxySessionOpened(IoSession session) throws Exception {
60          logger.debug("CLIENT - Session opened: " + session);
61          final IoSession _session = session;
62          // Enter typing loop
63          new Thread(new Runnable() {
64              public void run() {
65                  InputStreamReader isr = new InputStreamReader(System.in);
66                  BufferedReader br = new BufferedReader(isr);
67  
68                  while (!_session.isClosing()) {
69                      try {
70                          String line = br.readLine();
71                          if (line != null) {
72                              _session.write(line);
73                          }
74                      } catch (Exception e) {
75                          break;
76                      }
77                  }
78  
79                  _session.close(true);
80              }
81  
82          }).start();
83      }
84  
85      /**
86       * {@inheritDoc} 
87       */
88      @Override
89      public void messageReceived(IoSession session, Object message) {
90          //System.out.println((String) message);
91      }
92  
93      /**
94       * {@inheritDoc} 
95       */
96      @Override
97      public void sessionClosed(IoSession session) throws Exception {
98          logger.debug("CLIENT - Session closed");
99      }
100 
101     /**
102      * {@inheritDoc} 
103      */
104     @Override
105     public void exceptionCaught(IoSession session, Throwable cause) {
106         logger.debug("CLIENT - Exception caught");
107         //cause.printStackTrace();
108         session.close(true);
109     }
110 }