
// ProducerAgent.java
// Michele Loreti and the jSpace Developers, 2017
// Modified by Andrew Davison, ad@coe.psu.ac.th, May 2023

/* Put numItems items into the space. Each tuple has the
   form ("ITEM", <item name>, <item number>)

  Usage:
    > compile.bat ProducerAgent.java

    Part of the application made up of:
       * ProducerConsumer.java
       * ProducerAgent.java
       * ConsumerAgent.java

  Other version: 
    * RemoteProducerConsumer.java
*/


import org.jspace.ActualField;
import org.jspace.Space;


public class ProducerAgent implements Runnable
{
  private Space space;
  private String itemNm;
  private int id, numItems;

  public ProducerAgent(int id, String name, Space space, int n)
  {
    this.id = id;
    this.space = space;
    itemNm = name;
    numItems = n;
  }


  public void run()
  {
    try {
      for (int i=0; i < numItems; i++ ) {
        System.out.println(SpaceUtils.spaces(id*3) +
              "PRODUCER " + id + ": " + itemNm + " " + i);
        SpaceUtils.pause(200);   // delay a bit between production
        space.put("ITEM", itemNm, i);
      }
      System.out.println(SpaceUtils.spaces(id*3) + 
                      "PRODUCER " + id + " DONE");
    }
    catch (InterruptedException e) 
    {  System.out.println(SpaceUtils.spaces(id*3) +
                      "PRODUCER  " + id + " error: " + e); }
  } /* run */


}  // end of ProducerAgent class
