* change its icon back to white.
*/
public void fire() throws IllegalActionException {
super.fire();
if (input.hasToken(0)) {
RecordToken in = (RecordToken) input.get(0);
double data = ((DoubleToken) in.get("data")).doubleValue();
String destination = ((StringToken) in.get("destination"))
.stringValue();
String routeTo = ((StringToken) in.get("routeTo")).stringValue();
int hops = ((IntToken) in.get("hops")).intValue();
/*System.out.println(getName() + " receive a event with : " + "\n"
+ "destination = " + destination + "\n"
+ "routeTo = " + routeTo + "\n"
+ "hops = " + hops);
*/
if (getName().equals(destination)) {
// Change the color of the icon to red.
_circle2.fillColor.setToken("{1.0, 0.0, 0.1, 0.7}");
//_isRed = true;
test.send(0, new IntToken(hops + 1));
//Call fireAt to set the color back to white after the delay time.
Director director = getDirector();
double delayTime = ((DoubleToken) delay.getToken())
.doubleValue();
Time time = director.getModelTime().add(delayTime);
director.fireAt(this, time);
} else if (getName().equals(routeTo) || (hops == 0)) {
// Change the color of the icon to green.
_circle2.fillColor.setToken("{0.0, 1.0, 0.0, 1.0}");
CompositeEntity container = (CompositeEntity) getContainer();
Entity destNode = container.getEntity(destination);
Locatable destLocation = (Locatable) destNode.getAttribute(
"_location", Locatable.class);
Locatable myLocation = (Locatable) this.getAttribute(
"_location", Locatable.class);
if ((destLocation == null) || (myLocation == null)) {
throw new IllegalActionException(
"Cannot determine location for node "
+ destNode.getName() + ".");
}
Iterator nodes = _connectedNodes.iterator();
double minDistance = _distanceBetween(destLocation, myLocation);
String to = " ";
boolean multi = ((BooleanToken) doublePath.getToken())
.booleanValue();
double nextMinDistance = _distanceBetween(destLocation,
myLocation);
String to2 = " ";
while (nodes.hasNext()) {
Entity node = (Entity) nodes.next();
Locatable location = (Locatable) node.getAttribute(
"_location", Locatable.class);
if (location == null) {
throw new IllegalActionException(
"Cannot determine location for node "
+ node.getName() + ".");
}
double d = _distanceBetween(destLocation, location);
if (multi) {
if (d < minDistance) {
nextMinDistance = minDistance;
to2 = to;
minDistance = d;
to = node.getName();
} else if (d < nextMinDistance) {
nextMinDistance = d;
to2 = node.getName();
}
} else {
if (d < minDistance) {
minDistance = d;
to = node.getName();
}
}
}
// Request refiring after a certain amount of time specified
// by the <i>delay<i> parameter.
Director director = getDirector();
Token[] values = { new DoubleToken(data),
new StringToken(destination), new StringToken(to),
new IntToken(hops + 1) };
double delayTime = ((DoubleToken) delay.getToken())
.doubleValue();
Time time = director.getModelTime().add(delayTime);
if (_receptions == null) {
_receptions = new HashMap();
}
Double timeDouble = Double.valueOf(time.getDoubleValue());
String[] labels = { "data", "destination", "routeTo", "hops" };
RecordToken result = new RecordToken(labels, values);
_receptions.put(timeDouble, result);
director.fireAt(this, time);
if (multi) {
Token[] values2 = { new DoubleToken(data),
new StringToken(destination), new StringToken(to2),
new IntToken(hops + 1) };
if (_receptions == null) {
_receptions = new HashMap();
}
RecordToken result2 = new RecordToken(labels, values2);
_receptions.put(timeDouble, result2);
director.fireAt(this, time.add(delayTime));
}
//output.send(0, result);
}
} else {
if (_receptions != null) {
// We may be getting fired because of an impending event.
double currentTimeValue = getDirector().getModelTime()
.getDoubleValue();
Double timeDouble = Double.valueOf(currentTimeValue);
RecordToken reception = (RecordToken) _receptions
.get(timeDouble);
if (reception != null) {
// The time matches a pending reception.
_receptions.remove(reception);