package modules.consensus;

import utils.*;
import modules.gradient.*;
import modules.region.*;
import simulator.*;
import java.util.*;

public class ConsensusFringe extends ConsensusRegion {
  Tuple gid; int border;
  public ConsensusFringe(Object id,int border,PortedProcessor pp) {
    super(id); this.border=border;
    gid=new Tuple(id,Symbol.GetSymbol("fringe"));
    ActiveGradient ag = (ActiveGradient)pp.getModule("ActiveGradient");
    ag.sendGradient(makeG(pp));
    //ag.latchGradient(gid);
  }
  Gradient makeG(PortedProcessor pp) {
    Consensus cs = (Consensus)pp.getModule("Consensus");
    VersionedData d = cs.getConsensus(pid);
    if(d==null) d=new VersionedData();
    return new VersionedGradient(gid,Consensus.type,null,
                                 new DistancePropagation(border),
                                 d.getQuality(),d.getVersion());
  }

  public boolean isMember(PortedProcessor pp) {
    boolean full = super.isMember(pp);
    ActiveGradient ag = (ActiveGradient)pp.getModule("ActiveGradient");
    if(full) {
      ag.sendGradient(makeG(pp));
      return true;
    } else {
      Gradient g = (Gradient)ag.get(gid);
      if(g==null || g.isDead()) return false;
      else return true;
    }
  }
}
