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.examples.udpecho;
    21
    22  import java.net.InetSocketAddress;
    23  import java.net.SocketAddress;
    24  import java.nio.ByteBuffer;
    25
    26  import org.apache.mina.api.AbstractIoHandler;
    27  import org.apache.mina.api.IdleStatus;
    28  import org.apache.mina.api.IoFilter;
    29  import org.apache.mina.api.IoSession;
    30  import org.apache.mina.filter.logging.LoggingFilter;
    31  import org.apache.mina.filterchain.ReadFilterChainController;
    32  import org.apache.mina.filterchain.WriteFilterChainController;
    33  import org.apache.mina.session.WriteRequest;
    34  import org.apache.mina.transport.nio.NioUdpServer;
    35  import org.slf4j.Logger;
    36  import org.slf4j.LoggerFactory;
    37
    38  /**
    39   * A UDP base echo server sending back every datagram received
    40   *
    41   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    42   */
    43  public class NioUdpEchoServer {
    44      static final Logger LOG = LoggerFactory.getLogger(NioUdpEchoServer.class);
    45
    46      public static void main(final String[] args) {
    47          LOG.info("starting echo server");
    48
    49          final NioUdpServer server = new NioUdpServer();
    50
    51          // create the filter chain for this service
    52          server.setFilters(new LoggingFilter("LoggingFilter1"), new IoFilter() {
    53
    54              @Override
    55              public void sessionOpened(final IoSession session) {
    56                  LOG.info("session {} open", session);
    57              }
    58
    59              @Override
    60              public void sessionIdle(final IoSession session, final IdleStatus status) {
    61                  LOG.info("session {} idle", session);
    62              }
    63
    64              @Override
    65              public void sessionClosed(final IoSession session) {
    66                  LOG.info("session {} open", session);
    67              }
    68
    69              @Override
    70              public void messageWriting(final IoSession session, WriteRequest message,
    71                      final WriteFilterChainController controller) {
    72                  // we just push the message in the chain
    73                  controller.callWriteNextFilter(message);
    74              }
    75
    76              @Override
    77              public void messageReceived(final IoSession session, final Object message,
    78                      final ReadFilterChainController controller) {
    79
    80                  if (message instanceof ByteBuffer) {
    81                      LOG.info("echoing");
    82                      session.write(message);
    83                  }
    84              }
    85
    86              @Override
    87              public void messageSent(final IoSession session, final Object message) {
    88                  LOG.info("message {} sent", message);
    89              }
    90          });
    91
    92          server.setIoHandler(new AbstractIoHandler() {
    93              @Override
    94              public void sessionOpened(final IoSession session) {
    95                  LOG.info("session opened {}", session);
    96
    97                  final String welcomeStr = "welcome\n";
    98                  final ByteBuffer bf = ByteBuffer.allocate(welcomeStr.length());
    99                  bf.put(welcomeStr.getBytes());
    100                 bf.flip();
    101                 session.write(bf);
    102
    103             }
    104         });
    105
    106         try {
    107             final SocketAddress address = new InetSocketAddress(9999);
    108             server.bind(address);
    109             LOG.debug("Running the server for 25 sec");
    110             Thread.sleep(25000);
    111             LOG.debug("Unbinding the UDP port");
    112             server.unbind();
    113         } catch (final InterruptedException e) {
    114             LOG.error("Interrupted exception", e);
    115         }
    116     }
    117 }