package simulator;

import utils.*;
import java.util.*;

public class PerfectCommunicationsModel implements CommunicationsModel {

  private EmbeddedNetwork net = null;
  private double delay = 0.1;

  public PerfectCommunicationsModel() {}
  public PerfectCommunicationsModel(int d) {delay = d;}
  public void setDelay(double d) { delay = d; }
  public void setNetwork(EmbeddedNetwork net) { this.net = net; }

  public Collection broadcast(SimulatorEvent e) {
    // send a delayed copy to each neighbor
    Vector bcast = new Vector();
    Enumeration en = net.getNeighbors(e.proc);
    while(en.hasMoreElements()) {
      Processor p = (Processor)en.nextElement();
      bcast.add(new SimulatorEvent(p,e.time+delay,e.name,e.data));
    }
    return bcast;
  }

  public SimulatorEvent pointtopoint(SimulatorEvent e, Processor t) {
    // send a delayed copy to the specified neighbor
    if(net.isNeighbor(e.proc,t))
      return new SimulatorEvent(t,e.time+delay,e.name,e.data);
    else
      return null;
  }

  public Object receive(SimulatorEvent e) {
    return e.data;
  }

  public boolean carrierSense(double time, double x, double y) {
    return false;
  }
}
