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  
21  package org.apache.mina.examples.echoserver;
22  
23  import java.net.InetSocketAddress;
24  import java.net.SocketAddress;
25  import java.nio.ByteBuffer;
26  
27  import org.apache.mina.api.AbstractIoHandler;
28  import org.apache.mina.api.IoSession;
29  import org.apache.mina.filter.logging.LoggingFilter;
30  import org.apache.mina.transport.nio.NioTcpServer;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  /**
35   * A simple TCP server, write back to the client every received messages.
36   * 
37   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
38   * 
39   */
40  public class NioEchoServer {
41  
42      static final private Logger LOG = LoggerFactory.getLogger(NioEchoServer.class);
43  
44      public static void main(final String[] args) {
45          LOG.info("starting echo server");
46  
47          final NioTcpServer acceptor = new NioTcpServer();
48  
49          // create the filter chain for this service
50          acceptor.setFilters(new LoggingFilter("LoggingFilter1"));
51  
52          acceptor.setIoHandler(new AbstractIoHandler() {
53              @Override
54              public void sessionOpened(final IoSession session) {
55                  LOG.info("session opened {}", session);
56  
57                  final String welcomeStr = "welcome\n";
58                  final ByteBuffer bf = ByteBuffer.allocate(welcomeStr.length());
59                  bf.put(welcomeStr.getBytes());
60                  bf.flip();
61                  session.write(bf);
62              }
63  
64              @Override
65              public void messageReceived(IoSession session, Object message) {
66                  if (message instanceof ByteBuffer) {
67                      LOG.info("echoing");
68                      session.write(message);
69                  }
70              }
71          });
72          try {
73              final SocketAddress address = new InetSocketAddress(9999);
74              acceptor.bind(address);
75              LOG.debug("Running the server for 25 sec");
76              Thread.sleep(25000);
77              LOG.debug("Unbinding the TCP port");
78              acceptor.unbind();
79          } catch (final InterruptedException e) {
80              LOG.error("Interrupted exception", e);
81          }
82      }
83  }