package net.catpad.infobus.test.test2;

import net.catpad.infobus.CannotExecuteCommQueueItemException;
import net.catpad.infobus.CommQueueItem;
import net.catpad.infobus.ServiceId;
import net.catpad.infobus.ServiceLauncher;


public class PassToNextCommQueueItem extends CommQueueItem {

  int next;
  
  public PassToNextCommQueueItem(ServiceLauncher serviceLauncher, int next) {
    super(serviceLauncher);
    
    this.next = next;
  }

  @Override
  public void execute(ServiceId serviceId)
      throws CannotExecuteCommQueueItemException, InterruptedException {
    
    System.out.println("In the context of " + serviceId.toString());
    
    Thread.sleep(50);
    
    ServiceId nextId = null;
    
    switch (next) {
      case 1: nextId = InfoBusTest2.SERVICE_1; break;
      case 2: nextId = InfoBusTest2.SERVICE_2; break;
      case 3: nextId = InfoBusTest2.SERVICE_3; break;
    }
    
    int nextService = next+1;
    if (nextService == 4nextService = 1;
    
    serviceLauncher.getInfoBus().postItem(nextId, 
        new PassToNextCommQueueItem(serviceLauncher, nextService));
    
    //////////////////////////////////////////////////////////////////////////////////
    // Uncomment the next line to see how the service failover architecture works --->
        //////////////////////////////////////////////////////////////////////////////////
    // throw new NullPointerException();
  }

}