01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
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 MINA Project (dev@mina.apache.org)
41: * @version $Rev: 600463 $, $Date: 2007-12-03 03:02:19 -0700 (Mon, 03 Dec 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.setHandler(new TennisPlayer());
51: acceptor.bind(address);
52:
53: // Connect to the server.
54: VmPipeConnector connector = new VmPipeConnector();
55: connector.setHandler(new TennisPlayer());
56: ConnectFuture future = connector.connect(address);
57: future.awaitUninterruptibly();
58: IoSession session = future.getSession();
59:
60: // Send the first ping message
61: session.write(new TennisBall(10));
62:
63: // Wait until the match ends.
64: session.getCloseFuture().awaitUninterruptibly();
65:
66: acceptor.unbind();
67: }
68: }
|