001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.mina.example.tennis; 021 022import org.apache.mina.core.future.ConnectFuture; 023import org.apache.mina.core.service.IoAcceptor; 024import org.apache.mina.core.session.IoSession; 025import org.apache.mina.transport.vmpipe.VmPipeAcceptor; 026import org.apache.mina.transport.vmpipe.VmPipeAddress; 027import org.apache.mina.transport.vmpipe.VmPipeConnector; 028 029/** 030 * (<b>Entry point</b>) An 'in-VM pipe' example which simulates a tennis game 031 * between client and server. 032 * <ol> 033 * <li>Client connects to server</li> 034 * <li>At first, client sends {@link TennisBall} with TTL value '10'.</li> 035 * <li>Received side (either server or client) decreases the TTL value of the 036 * received ball, and returns it to remote peer.</li> 037 * <li>Who gets the ball with 0 TTL loses.</li> 038 * </ol> 039 * 040 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 041 */ 042public class Main { 043 044 public static void main(String[] args) throws Exception { 045 IoAcceptor acceptor = new VmPipeAcceptor(); 046 VmPipeAddress address = new VmPipeAddress(8080); 047 048 // Set up server 049 acceptor.setHandler(new TennisPlayer()); 050 acceptor.bind(address); 051 052 // Connect to the server. 053 VmPipeConnector connector = new VmPipeConnector(); 054 connector.setHandler(new TennisPlayer()); 055 ConnectFuture future = connector.connect(address); 056 future.awaitUninterruptibly(); 057 IoSession session = future.getSession(); 058 059 // Send the first ping message 060 session.write(new TennisBall(10)); 061 062 // Wait until the match ends. 063 session.getCloseFuture().awaitUninterruptibly(); 064 065 acceptor.unbind(); 066 } 067}