remove unnecessary method from Client.java
[fleet.git] / src / edu / berkeley / fleet / fpga / Client.java
1 package edu.berkeley.fleet.fpga;
2
3 import static edu.berkeley.fleet.util.BitManipulations.*;
4 import edu.berkeley.fleet.api.*;
5 import java.io.*;
6 import java.net.*;
7 import edu.berkeley.fleet.util.*;
8 import java.util.*;
9 import java.util.concurrent.*;
10 import static edu.berkeley.fleet.two.FleetTwoFleet.*;
11 import static edu.berkeley.fleet.api.Instruction.Set.*;
12 import static edu.berkeley.fleet.api.Predicate.*;
13 import static edu.berkeley.fleet.api.Instruction.*;
14
15
16 public class Client extends FleetProcess {
17
18     private Socket s;
19     private BlockingQueue<BitVector> queue = new LinkedBlockingQueue<BitVector>();
20
21     public static long signExtend(long val) {
22         if ((val & (1L << 36)) != 0)
23             val = val | (0xffffffffffffffffL << 36);
24         return val;
25     }
26                                     
27     public Fleet getFleet() { return fpga; }
28     public Dock getDebugInputDock() {
29         return fpga.getShip("Debug",0).getDock("in");
30     }
31     public BitVector readWord() {
32         if (isTerminated())
33             throw new RuntimeException("this fleet has been terminated");
34         try {
35             return queue.take();
36         } catch (InterruptedException e) { throw new RuntimeException(e); }
37     }
38
39     protected void _terminate() {
40         try {
41             s.close();
42         } catch (Exception e) { e.printStackTrace(); }
43     }
44
45     private Fpga fpga;
46     private DataOutputStream dos = null;
47
48     public void flush() {
49         try {
50             dos.flush();
51         } catch (Exception e) {
52             throw new RuntimeException(e);
53         }
54     }
55
56     public Client(Fpga fpga, String bitfile, Instruction[] instructions) throws Exception {
57         this.fpga = fpga;
58
59         s = new Socket(InetAddress.getByName("goliath.megacz.com"), 3133);
60         //s = new Socket(InetAddress.getByName("localhost"), 3133);
61         OutputStream os = new BufferedOutputStream(s.getOutputStream());
62         PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
63         pw.print(Server.pass_string+" "+bitfile+"\n");
64         pw.flush();
65
66         this.dos = new DataOutputStream(os);
67         for(Instruction inst : instructions)
68             dispatchInstruction(inst);
69         flush();
70
71         final InputStream is = new BufferedInputStream(s.getInputStream());
72         Thread t = new Thread() {
73             public void run() {
74                 try {
75                     while(true) {
76                         long result = 0;
77                         int val = 0;
78                         for(int i=0; i<6; i++) {
79                             val = is.read();
80                             if (val==-1) break;
81                             long val2 = (val & 0xffL);
82                             val2 = val2 << (i * 8);
83                             result |= val2;
84                         }
85                         if (val==-1) break;
86                         BitVector bs = new BitVector(37);
87                         for(int i=0; i<37; i++)
88                             bs.set(i, ((result >> i) & 1L)!=0);
89                         queue.put(bs);
90                     }
91                 } catch (SocketException e) {
92                 } catch (Exception e) { throw new RuntimeException(e);
93                 } finally { terminate(); }
94             }
95             };
96         t.setDaemon(true);
97         t.start();
98     }
99
100     public void dispatchToken(Destination d) { dispatchWord(d, new BitVector(fpga.getWordWidth()), true); }
101     public void dispatchWord(Destination d, BitVector word) { dispatchWord(d, word, false); }
102     private void dispatchWord(Destination d, BitVector word, boolean token) {
103         try {
104             Dock dispatchFrom = fpga.debugShip.getDock("in");
105             long out = 0;
106             out = PACKET_DATA.setval(out, word);
107             out = PACKET_TOKEN.setval(out, token ? 1 : 0);
108             out = PACKET_SIGNAL.setval(out, 0);
109             out = PACKET_DEST.setval(out, ((FpgaPath)dispatchFrom.getPath(d, null)).toLong());
110             synchronized(this) {
111                 for(int i=7; i>=0; i--)
112                     dos.write(BitManipulations.getIntField(i*8+7, i*8, out));
113             }
114         } catch (Exception e) {
115             throw new RuntimeException(e);
116         }
117     }
118     public void dispatchInstruction(Instruction inst) {
119         Dock dispatchFrom = fpga.debugShip.getDock("in");
120         dispatchWord(inst.dock.getInstructionDestination(),
121                      new BitVector(fpga.getWordWidth()).set(fpga.writeInstruction(inst, dispatchFrom)));
122     }
123
124     private static Move discard(Dock dock)           { return new Move(dock, false, IgnoreOLC, false, null, false, true,  false, false, false, false); }
125     private static Move deliver(Dock dock)           { return new Move(dock, false, IgnoreOLC, false, null, false, false, false, false, true,  false); }
126     private static Move wait(Dock dock)              { return new Move(dock, false, IgnoreOLC, false, null, true,  false, false, false, false, false); }
127     private static Move sendto(Dock dock, Path path) { return new Move(dock, false, IgnoreOLC, false, path, false, false, false, false, true,  false); }
128 }