package net.catpad.infobus.test.test1;

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 three items to one another:
 * service 1 -> service 2
 * service 2 -> service 3
 * service 3 -> service 1
 
 * Accordingly, there are three communication queue items passed between them:
 * PassTo1CommQueueItem, PassTo2CommQueueItem, PassTo3CommQueueItem.
 
 @author Michael Gertelman
 *
 */
public class InfoBusTest1 {

  public static Logger logger = Logger.getAnonymousLogger();
  
  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) {
        
    ServiceLauncher launcher = new ServiceLauncher(logger);
    
    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
    launcher.getInfoBus().postItem(InfoBusTest1.SERVICE_1, 
        new PassTo1CommQueueItem(launcher));
    
    try {
      Thread.sleep(10000);
    catch (InterruptedException e) {
      e.printStackTrace();
    }
    
    launcher.shutdownGracefully();
    //launcher.shutdownImmediately();

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