
// PingPong.java
// Michele Loreti and the jSpace Developers, 2017
// Modified by Andrew Davison, ad@coe.psu.ac.th, May 2023

/* Models two-way communication between PingAgent
   and PongAgent threads using two spaces called PingSpace
   and PongSpace.

   PingAgent reads 'ping' tuples from the PingSpace, which
   are placed there by PongAgent.

   PongAgent reads 'pong' tuples from the PongSpace, which
   are placed there by PingAgent.

   An agent only writes a tuple after it has read one of its
   tuples. Deadlock is avoided by having PongAgent start by writing
   a ping to the PingSpace.

   There's no real need for two spaces here since the ping and
   pong tuples could all be placed in a single space.

  Original example at:
    https://github.com/pSpaces/jSpace/tree/master/examples/PingPong

  Usage:
    > compile.bat PingPong.java
    > run.bat PingPong

    Part of the application made up of:
       * PingPong.java
       * PingAgent.java
       * PongAgent.java

  Other versions: 
    * PingPongConn.java
    * PingPongKeep.java
*/


import org.jspace.*;


public class PingPong
{

  public static void main( String[] args) 
                      throws InterruptedException
  {
    // Space ping = new SequentialSpace();
    // Space pong = new SequentialSpace();
    Space ping = new VisSpace("Ping Space");
    Space pong = new VisSpace("Pong Space");

    Thread t1 = new Thread( new PingAgent(ping, pong), "PingAgent");
    Thread t2 = new Thread( new PongAgent(ping, pong), "PongAgent");
    t1.start();
    t2.start();

    t1.join();
    t2.join();
  } /* main */

}  // end of PingPong class
