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.core.future.ConnectFuture;
23 import org.apache.mina.core.service.IoAcceptor;
24 import org.apache.mina.core.session.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 <a href="http://mina.apache.org">Apache MINA Project</a>
41 */
42 public class Main {
43
44 public static void main(String[] args) throws Exception {
45 IoAcceptor acceptor = new VmPipeAcceptor();
46 VmPipeAddress address = new VmPipeAddress(8080);
47
48 // Set up server
49 acceptor.setHandler(new TennisPlayer());
50 acceptor.bind(address);
51
52 // Connect to the server.
53 VmPipeConnector connector = new VmPipeConnector();
54 connector.setHandler(new TennisPlayer());
55 ConnectFuture future = connector.connect(address);
56 future.awaitUninterruptibly();
57 IoSession session = future.getSession();
58
59 // Send the first ping message
60 session.write(new TennisBall(10));
61
62 // Wait until the match ends.
63 session.getCloseFuture().awaitUninterruptibly();
64
65 acceptor.unbind();
66 }
67 }