add Packet.toString()
[fleet.git] / src / edu / berkeley / fleet / interpreter / Packet.java
index 3042201..d1c292f 100644 (file)
@@ -1,29 +1,66 @@
 package edu.berkeley.fleet.interpreter;
-import java.io.*;
-import java.util.*;
-import java.util.concurrent.*;
-import java.lang.reflect.*;
-import edu.berkeley.fleet.*;
-import edu.berkeley.sbp.util.ANSI;
-import edu.berkeley.fleet.doc.*;
 import edu.berkeley.fleet.api.*;
-import edu.berkeley.fleet.ies44.*;
+import edu.berkeley.sbp.util.ANSI;
 
 class Packet {
 
-    Interpreter interpreter;
-    long        value;
-    InterpreterDestination destination;
+    private final InterpreterPath path;
+    private final BitVector value;
+    private final boolean isToken;
 
-    public Packet(Interpreter interpreter, InterpreterPump source, long value, InterpreterDestination destination) {
-        Log.data(value+"", source, (Destination)destination);
-        this.interpreter = interpreter;
+    public Packet(InterpreterPath path, BitVector value, boolean isToken) {
         this.value = value;
-        this.destination = destination;
+        this.path = path;
+        this.isToken = isToken;
+        this.value.setImmutable();
     }
 
     public void send() {
-        destination.addDataFromFabric(this);
+        Log.packet(this);
+        ((InterpreterDestination)path.getDestination()).addDataFromFabric(this);
+    }
+
+    public Destination getDestination() {
+        return path.getDestination();
+    }
+
+    public Dock getSource() {
+        return path.getSource();
+    }
+
+    public BitVector getSignal() {
+        return path.getSignal();
+    }
+
+    public BitVector getValue() {
+        return value;
+    }
+
+    public boolean isToken() {
+        return isToken;
+    }
+
+    public String toString() {
+        Packet p = this;
+        BitVector data = p.getValue();
+        Dock source = p.getSource();
+        Destination dest = p.getDestination();
+        if (dest.getDock().getInstructionDestination()==dest) {
+            if (p.isToken()) {
+                return (ANSI.yellow(ANSI.bold(" torpedo: ")) + (source + " -> " + ANSI.yellow(ANSI.bold(dest+""))));
+            } else {
+                Instruction d = source.getShip().getFleet().decodeInstruction(data, source);
+                return (ANSI.red("dispatch: " + d));
+            }
+        } else {
+            if (p.isToken()) {
+                return (ANSI.blue(ANSI.bold("   token: ")) + (source + " -> " + ANSI.blue(ANSI.bold(dest+""))));
+            } else {
+                return (ANSI.cyan("    data: "+data) +
+                               (source==null ? "" :
+                                (" : " + source))+(" -> "+ANSI.cyan(""+dest)));
+            }
+        }
     }
 
-}
\ No newline at end of file
+}