/*
 *  bondFaultInfoHandler.java
 *    @contains It handles the incoming messages that
 *      carry the event information. See README in
 *      this directory for detail.
 *    @author Kyungkoo Jun
 *    Bond group, CS, Purdue Univ.
 *    created on Nov 14, 2000
 *
 *     modified by Kyungkoo Jun, Dec 22, 2000
 *     -- add file logging capability to perform
 *        experiments that measure the overhead of
 *        the algorithms in the number of exchanged
 *        messages.
 *
 */

package bond.agent.FaultDetection;

import bond.core.*;
import java.util.*;

public class bondFaultInfoHandler
extends bondFaultDetectionExecutable
{

  String id;
  Integer value;
  Vector remains;
  String source;
  bondMessage msg;
  bondShadow bs;

  public bondFaultInfoHandler(String id, Integer v, Vector remains, 
			      String source, bondFaultStatus fs,
			      bondMessage msg, bondShadow bs ) {
    super(fs);
    this.id = id;
    this.source = source;
    this.remains = remains;
    this.msg = msg;
    this.bs = bs;
    value = v;
    go();
  }

  public bondFaultInfoHandler(String id, Integer v, Vector remains, 
			      String source, bondFaultStatus fs) {
    super(fs);
    this.id = id;
    this.source = source;
    this.remains = remains;
    value = v;
    go();
  }

  protected void splitAndSend() {

    if (remains == null || remains.size() == 0) {
    }
    else if (remains.size() == 1) {
      fs.spreadInfo((String)remains.elementAt(0), new Vector(), id);
    }
    else {
      Vector v1, v2;
      int sz = remains.size();
      int half = sz / 2;
      v1 = remains;
      v2 = (Vector)remains.clone();
      v2.subList(0, half).clear();
      v1.subList(half, sz).clear();
      
      String temp = (String)v1.remove(0);
      fs.spreadInfo(temp, v1, id);
      temp = (String)v2.remove(0);
      fs.spreadInfo(temp, v2, id);
    }
  }


  public void run() {

    Integer iv = (Integer)fs.getFaultStatus(id);
    
    if (iv == null || iv.compareTo(value) < 0) { // new info
      fs.updateFaultStatus(id, value);
    }
    else if (iv.compareTo(value) == 0) { // same info

    }else if (iv.compareTo(value) > 0) { // old info
      // notify with new info
      fs.spreadInfo(source, new Vector(), id);
    }

    splitAndSend();

    filelogger.log(fs.getMyID()+" ackInfo");

    //bs.say(msg, this);
    dir.unregister(this);
  }
}
