811c02b9eef158d7e21275d647f422953a0b4734
[fleet.git] / src / edu / berkeley / fleet / interpreter / InterpreterShip.java
1 package edu.berkeley.fleet.interpreter;
2 import edu.berkeley.fleet.api.*;
3 import edu.berkeley.fleet.two.*;
4 import java.util.*;
5 import java.io.*;
6
7 /** a ship, which belongs to a fleet and which may have many docks */
8 abstract class InterpreterShip extends FleetTwoShip {
9         
10     LinkedHashMap<String,InterpreterDock> docks = new LinkedHashMap<String,InterpreterDock>();
11
12     /** You should instantiate a bunch of Inboxes and Outboxes in your constructor */
13     public InterpreterShip(Interpreter fleet, ShipDescription sd) {
14         super(fleet, sd);
15     }
16
17     public Iterator<Dock> iterator() {
18         return (Iterator<Dock>)(Object)docks.values().iterator();
19     }
20
21     /**
22      *  Override this method, check inboxes for the data you need, and
23      *  if you find it, deposit results in an outbox; we'll take care
24      *  of the rest.
25      */
26     public abstract void service();
27
28     public final void _service() {
29
30         // service all the docks
31         for(InterpreterDock p : docks.values()) p.service();
32
33         // flushing logic (must come between dock servicing and subclass)
34         boolean someflushing = false;
35         boolean allflushing = true;
36         boolean someempty = false;
37         for(InterpreterDock d : docks.values()) {
38             if (!d.isInputDock()) continue;
39             if (d.flushing) someflushing = true;
40             else allflushing = false;
41             if (!d.flushing && !d.dataReadyForShip) someempty = true;
42         }
43         if (allflushing && someflushing) {
44             for(InterpreterDock d : docks.values())
45                 if (d.isInputDock())
46                     d.flushing = false;
47             return;
48         } else if (someflushing && !someempty) {
49             for(InterpreterDock d : docks.values())
50                 if (d.isInputDock() && !d.flushing && d.dataReadyForShip) {
51                     System.out.println("FLUSH AT " + this);
52                     d.dataReadyForShip = false;
53                 }
54         }
55
56         // now pass control to the subclass
57         service();
58     }
59
60     public void reset() {
61         for(InterpreterDock p : docks.values())
62             p.reset();
63     }
64
65     public Interpreter getInterpreter() { return (Interpreter)getFleet(); }
66 }