1 package edu.berkeley.fleet;
3 import static edu.berkeley.fleet.Instruction.IgnoreCopyTake.*;
6 public class InboxPort extends InstructionPort {
8 public InboxPort(Ship ship, String name) {
11 // default standing move
12 addInstruction(new Instruction(null,
22 // data in from the switch fabric
23 private Queue<Integer> dataIn = new LinkedList<Integer>();
24 public void addData(int data) { dataIn.add(data); }
25 public void addToken() { throw new RuntimeException("inboxes do not accept tokens!"); }
27 // data out to the ship
28 private boolean empty = true;
30 private Port tokenDestination;
32 public boolean empty() { return empty; }
35 throw new RuntimeException("called remove() on an empty() inbox");
37 if (tokenDestination != null) {
38 Log.token(this, tokenDestination);
39 tokenDestination.addToken();
44 public boolean service(Instruction instruction) {
46 // if data is stuck on dataOut, wait for it to go somewhere
47 if (!empty) return false;
49 // if no instruction waiting, do nothing
50 if (instruction == null) return false;
52 // check firing conditions
53 if (instruction.trigger)
54 throw new RuntimeException("you cannot send a triggered instruction to an inbox!");
55 if (instruction.dataIn != IGNORE && dataIn.size()==0) return false;
57 // consume inbound data+token
59 switch(instruction.dataIn) {
61 dataOut = dataIn.peek();
64 dataOut = dataIn.remove();
68 if (instruction.dataOut) {
71 tokenDestination = instruction.destination.resolve(getShip().getFleet());
73 } else if (instruction.ack) {
74 Port p = instruction.destination.resolve(getShip().getFleet());