package net.catpad.infobus.test.test2;

import java.util.logging.Logger;

import net.catpad.infobus.ServiceId;
import net.catpad.infobus.ServiceLauncher;
import net.catpad.infobus.ServiceThreadAlreadyRunningException;

/**
 * In this example three service threads will pass one item to one another:
 * service 1 -> service 2
 * service 2 -> service 3
 * service 3 -> service 1
 * (note that the item instances are in fact different).
 *
 * There is only one communication queue item class in this case: PassToNextCommQueueItem.
 * In its constructor it will receive the number of the next service to pass it to.
 
 @author Michael Gertelman
 *
 */
public class InfoBusTest2 {
  
  public static final ServiceId SERVICE_1   = new ServiceId(1"Service Thread 1");
  public static final ServiceId SERVICE_2   = new ServiceId(2"Service Thread 2");
  public static final ServiceId SERVICE_3   = new ServiceId(3"Service Thread 3");
  
  public static void main(String[] args) {
        
    // Just in case, we will not use any logger in this example
    ServiceLauncher launcher = new ServiceLauncher();
    
    try {
      
      launcher.startService(SERVICE_1);
      launcher.startService(SERVICE_2);
      launcher.startService(SERVICE_3);
      
    catch (ServiceThreadAlreadyRunningException e) {
      System.err.println(e.getMessage());
    }
    
    // Start the chain reaction by posting an item to SERVICE_1
    // and telling it to pass an item to service #2.
    // Note the second parameter of the item's constructor
    launcher.getInfoBus().postItem(InfoBusTest2.SERVICE_1, 
        new PassToNextCommQueueItem(launcher, 2));
    
    try {
      Thread.sleep(10000);
    catch (InterruptedException e) {
      e.printStackTrace();
    }
    
    launcher.shutdownGracefully();
    //launcher.shutdownImmediately();

    System.out.println("EXITED");    
  }
}