
// PingPongConn.java
// Michele Loreti and the jSpace Developers, 2017
// Modified by Andrew Davison, ad@coe.psu.ac.th, May 2023

/* The same code as PingPongKeep.java, but using the "conn"
   protocol rather than "keep". From the user's
   point of view there's no difference.

  Usage:
    > compile.bat PingPongConn.java
    > run.bat PingPongConn

    Part of the application made up of:
       * PingPongConn.java
       * PingAgent.java
       * PongAgent.java

  Other versions: 
    * PingPong.java
    * PingPongKeep.java
*/

import java.io.*;
import java.net.*;

import org.jspace.*;


public class PingPongConn
{
  private final static String SPACE_URI = "tcp://127.0.0.1:9001/?conn";

  private final static String PING_URI =
                              "tcp://127.0.0.1:9001/PingSpace?conn";

  private final static String PONG_URI = 
                              "tcp://127.0.0.1:9001/PongSpace?conn";
                         // or use locahost, as in PingPongKeep.java;
     // make sure the space names match those in SpaceRepository.add()


  public static void main( String[] argv ) 
                 throws InterruptedException,
                        UnknownHostException, IOException
  {
    // create one reporitory with two spaces
    SpaceRepository repository = new SpaceRepository();
    repository.addGate(SPACE_URI);
    repository.add("PingSpace", new SequentialSpace());
    repository.add("PongSpace", new SequentialSpace());

    Thread t1 = new Thread( new PingAgent(
                                 new RemoteSpace(PING_URI),
                                 new RemoteSpace(PONG_URI)));
    Thread t2 = new Thread( new PongAgent(
                                new RemoteSpace(PING_URI),
                                new RemoteSpace(PONG_URI)));
        /* Note: each thread creates its own references to the
           two remote spaces. This ensues they each havd their own
           gate to the space. 
        */

    t1.start();
    t2.start();

    SpaceUtils.waitTerminated(t1, t2);
    repository.shutDown();
  } /* main */

}  // end of PingPongConn class
