f8525c8f20c822c649096a8dea1f36c0657be604
[fleet.git] / src / edu / berkeley / fleet / interpreter / InterpreterBenkoBox.java
1 package edu.berkeley.fleet.interpreter;
2 import edu.berkeley.fleet.api.*;
3 import edu.berkeley.fleet.api.BenkoBox;
4 import java.util.*;
5
6 /** anything that has a destination address on the switch fabric */
7 public abstract class InterpreterBenkoBox extends BenkoBox implements InterpreterDestination {
8         
9     private final String name;
10     private final InterpreterShip ship;
11     private final Destination[] ports;
12     private final int addr = max_addr++;
13         
14     public InterpreterBenkoBox(InterpreterShip ship, String name, String[] ports) {
15         this.ship = ship;
16         this.name = name;
17         this.ports = new Destination[ports.length];
18         for(int i=0; i<ports.length; i++)
19             this.ports[i] =
20                 ports[i].equals("")
21                 ? this
22                 : new InterpreterBenkoBoxDestination(ports[i]);
23     }
24
25     public Iterable<Destination> getDestinations() {
26         HashSet<Destination> ret = new HashSet<Destination>();
27         for(Destination d : ports) ret.add(d);
28         return ret;
29     }
30
31     /** adds the included datum to the port from the switch fabric  side */
32     public abstract void addDataFromFabric(Packet packet);
33
34     abstract void service();
35
36     abstract void   shutdown();
37
38     public   Ship   getShip()                  { return ship; }
39     public   Fleet  getFleet()                 { return getShip().getFleet(); }
40     public   String toString()                 { return ship+"."+name; }
41     public   String getName()                  { return name; }
42     public   int    getInstructionFifoLength() { return 4; }
43     
44     Interpreter getInterpreter() { return ((InterpreterShip)getShip()).getInterpreter(); }
45
46     public long getDestAddr() { return addr; }
47
48     private static int max_addr;
49
50     private class InterpreterBenkoBoxDestination implements InterpreterDestination {
51         public String name;
52         public long addr = max_addr++;
53         public InterpreterBenkoBoxDestination(String name)          { this.name = name; }
54         public String getDestinationName()               { return name; }
55         public Ship getShip()                    { return InterpreterBenkoBox.this.getShip(); }
56         public void addDataFromFabric(Packet packet) { InterpreterBenkoBox.this.addDataFromFabric(packet); }
57         public String toString()                 { return getShip()+"."+getName(); }
58         public long getDestAddr() { return addr; }
59     }
60 }