View Javadoc

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