+
package edu.berkeley.fleet.interpreter;
import edu.berkeley.fleet.api.*;
import edu.berkeley.fleet.two.*;
import java.util.*;
import java.io.*;
-/** a ship, which belongs to a fleet and which may have many ports */
+/** a ship, which belongs to a fleet and which may have many docks */
abstract class InterpreterShip extends FleetTwoShip {
+ LinkedHashMap<String,InterpreterDock> docks = new LinkedHashMap<String,InterpreterDock>();
+
/** You should instantiate a bunch of Inboxes and Outboxes in your constructor */
public InterpreterShip(Interpreter fleet, ShipDescription sd) {
super(fleet, sd);
}
- private HashMap<String,InterpreterDock> ports = new HashMap<String,InterpreterDock>();
- public Iterator<Dock> iterator() { return (Iterator<Dock>)(Object)ports.values().iterator(); }
+ public Iterator<Dock> iterator() {
+ return (Iterator<Dock>)(Object)docks.values().iterator();
+ }
/**
* Override this method, check inboxes for the data you need, and
public abstract void service();
public final void _service() {
- for(InterpreterDock p : ports.values()) p.service();
- service();
- }
- protected void addDock(String name, InterpreterDock port) {
- ports.put(name, port);
+ // service all the docks
+ for(InterpreterDock p : docks.values()) p.service();
+
+ // flushing logic (must come between dock servicing and subclass)
+ boolean someflushing = false;
+ boolean allflushing = true;
+ boolean someempty = false;
+ for(InterpreterDock d : docks.values()) {
+ if (!d.isInputDock()) continue;
+ if (d.flushing) someflushing = true;
+ else allflushing = false;
+ if (!d.flushing && !d.dataReadyForShip) someempty = true;
+ }
+ if (allflushing && someflushing) {
+ for(InterpreterDock d : docks.values())
+ if (d.isInputDock())
+ d.flushing = false;
+ return;
+ } else if (someflushing && !someempty) {
+ for(InterpreterDock d : docks.values())
+ if (d.isInputDock() && !d.flushing && d.dataReadyForShip)
+ d.dataReadyForShip = false;
+ }
+
+ // now pass control to the subclass
+ service();
}
public void reset() {
- for(InterpreterDock p : ports.values())
+ for(InterpreterDock p : docks.values())
p.reset();
}