added am10 inbox/outbox support
[fleet.git] / src / edu / berkeley / fleet / TokenOutPort.java
1 package edu.berkeley.fleet;
2
3 /** a port which outputs only tokens */
4 public class TokenOutPort extends InstructionPort {
5
6     private boolean empty = true;
7
8     public TokenOutPort(Ship ship, String name) {
9         super(ship, name);
10     }
11
12     public boolean empty() {
13         return empty;
14     }
15
16     public void addToken() {
17         if (!empty)
18             throw new RuntimeException("tried to add data to a non-empty tokenoutbox!  you have a buggy ship!");
19         empty = false;
20     }
21
22     public boolean service(Instruction instruction) {
23
24         // if no instruction waiting, do nothing
25         if (instruction == null) return false;
26
27         // check firing conditions
28         if (instruction.trigger)
29             throw new RuntimeException("you cannot send triggered instructions to token-output ports");
30
31         switch(instruction.dataIn) {
32             case COPY:
33                 break;
34             case TAKE:
35                 empty = true;
36                 break;
37         }
38         
39         Port port = instruction.destination.resolve(getShip().getFleet());
40         Log.token(this, port);
41         port.addToken();
42         return true;
43     }
44
45     public void addData(int data) {
46         throw new RuntimeException("invalid!");
47     }
48 }