View Javadoc

1   /*
2    * @(#) $Id: Main.java 355016 2005-12-08 07:00:30Z trustin $
3    */
4   package org.apache.mina.examples.tennis;
5   
6   import org.apache.mina.common.ConnectFuture;
7   import org.apache.mina.common.IoSession;
8   import org.apache.mina.common.TransportType;
9   import org.apache.mina.registry.Service;
10  import org.apache.mina.registry.ServiceRegistry;
11  import org.apache.mina.registry.SimpleServiceRegistry;
12  import org.apache.mina.transport.vmpipe.VmPipeAddress;
13  import org.apache.mina.transport.vmpipe.VmPipeConnector;
14  
15  /***
16   * (<b>Entry point</b>) An 'in-VM pipe' example which simulates a tennis game
17   * between client and server.
18   * <ol>
19   *   <li>Client connects to server</li>
20   *   <li>At first, client sends {@link TennisBall} with TTL value '10'.</li>
21   *   <li>Received side (either server or client) decreases the TTL value of the
22   *     received ball, and returns it to remote peer.</li>
23   *   <li>Who gets the ball with 0 TTL loses.</li>
24   * </ol> 
25   * 
26   * @author The Apache Directory Project (dev@directory.apache.org)
27   * @version $Rev: 355016 $, $Date: 2005-12-08 16:00:30 +0900 (Thu, 08 Dec 2005) $
28   */
29  public class Main
30  {
31  
32      public static void main( String[] args ) throws Exception
33      {
34          ServiceRegistry registry = new SimpleServiceRegistry();
35  
36          VmPipeAddress address = new VmPipeAddress( 8080 );
37  
38          // Set up server
39          Service service = new Service( "tennis", TransportType.VM_PIPE, address );
40          registry.bind( service, new TennisPlayer() );
41  
42          // Connect to the server.
43          VmPipeConnector connector = new VmPipeConnector();
44          ConnectFuture future = connector.connect( address,
45                                                    new TennisPlayer() );
46          future.join();
47          IoSession session = future.getSession();
48  
49          // Send the first ping message
50          session.write( new TennisBall( 10 ) );
51  
52          // Wait until the match ends.
53          session.getCloseFuture().join();
54          
55          registry.unbind( service );
56      }
57  }