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.tennis;
    21
    22  import org.apache.mina.common.ConnectFuture;
    23  import org.apache.mina.common.IoAcceptor;
    24  import org.apache.mina.common.IoSession;
    25  import org.apache.mina.transport.vmpipe.VmPipeAcceptor;
    26  import org.apache.mina.transport.vmpipe.VmPipeAddress;
    27  import org.apache.mina.transport.vmpipe.VmPipeConnector;
    28
    29  /**
    30   * (<b>Entry point</b>) An 'in-VM pipe' example which simulates a tennis game
    31   * between client and server.
    32   * <ol>
    33   *   <li>Client connects to server</li>
    34   *   <li>At first, client sends {@link TennisBall} with TTL value '10'.</li>
    35   *   <li>Received side (either server or client) decreases the TTL value of the
    36   *     received ball, and returns it to remote peer.</li>
    37   *   <li>Who gets the ball with 0 TTL loses.</li>
    38   * </ol>
    39   *
    40   * @author The Apache Directory Project (mina-dev@directory.apache.org)
    41   * @version $Rev: 555855 $, $Date: 2007-07-13 05:19:00 +0200 (Fri, 13 Jul 2007) $
    42   */
    43  public class Main {
    44
    45      public static void main(String[] args) throws Exception {
    46          IoAcceptor acceptor = new VmPipeAcceptor();
    47          VmPipeAddress address = new VmPipeAddress(8080);
    48
    49          // Set up server
    50          acceptor.bind(address, new TennisPlayer());
    51
    52          // Connect to the server.
    53          VmPipeConnector connector = new VmPipeConnector();
    54          ConnectFuture future = connector.connect(address, new TennisPlayer());
    55          future.join();
    56          IoSession session = future.getSession();
    57
    58          // Send the first ping message
    59          session.write(new TennisBall(10));
    60
    61          // Wait until the match ends.
    62          session.getCloseFuture().join();
    63
    64          acceptor.unbind(address);
    65      }
    66  }