refactor codebag-memory-block creation code
[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 java.util.*;
8 import java.util.concurrent.*;
9 import static edu.berkeley.fleet.api.Instruction.Set.*;
10 import static edu.berkeley.fleet.api.Predicate.*;
11 import static edu.berkeley.fleet.api.Instruction.*;
12
13
14 public class Client extends FleetProcess {
15
16     private Socket s;
17     private BlockingQueue<BitVector> queue = new LinkedBlockingQueue<BitVector>();
18
19     public void invokeInstruction(Instruction i) {
20         throw new RuntimeException("not implemented");
21     }
22
23     public static long signExtend(long val) {
24         if ((val & (1L << 36)) != 0)
25             val = val | (0xffffffffffffffffL << 36);
26         return val;
27     }
28                                     
29     public Dock getDebugInputDock() {
30         throw new RuntimeException();
31     }
32     public BitVector readWord() {
33         if (isTerminated())
34             throw new RuntimeException("this fleet has been terminated");
35         try {
36             return queue.take();
37         } catch (InterruptedException e) { throw new RuntimeException(e); }
38     }
39
40     protected void _terminate() {
41         try {
42             s.close();
43         } catch (Exception e) { e.printStackTrace(); }
44     }
45
46     private Fpga fpga;
47
48     public Client(Fpga fpga, String bitfile, Instruction[] instructions) throws Exception {
49         this.fpga = fpga;
50
51         s = new Socket(InetAddress.getByName("goliath.megacz.com"), 3133);
52         OutputStream os = new BufferedOutputStream(s.getOutputStream());
53         PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
54         pw.print(Server.pass_string+" "+bitfile+"\n");
55         pw.flush();
56
57         DataOutputStream dos = new DataOutputStream(os);
58         for(Instruction inst : instructions)
59             fpga.writeInstruction(dos, fpga.getUniversalSource(), inst);
60         dos.flush();
61
62         final InputStream is = new BufferedInputStream(s.getInputStream());
63         new Thread() {
64             public void run() {
65                 try {
66                     while(true) {
67                         long result = 0;
68                         int val = 0;
69                         for(int i=0; i<6; i++) {
70                             val = is.read();
71                             if (val==-1) break;
72                             long val2 = (val & 0xffL);
73                             val2 = val2 << (i * 8);
74                             result |= val2;
75                         }
76                         if (val==-1) break;
77                         BitVector bs = new BitVector(37);
78                         for(int i=0; i<37; i++)
79                             bs.set(i, ((result >> i) & 1L)!=0);
80                         queue.put(bs);
81                     }
82                 } catch (SocketException e) {
83                 } catch (Exception e) { throw new RuntimeException(e);
84                 } finally { terminate(); }
85             }
86         }.start();
87     }
88
89     public void dispatchInstruction(Instruction i) { throw new RuntimeException(); }
90     private static Move discard(Dock dock)           { return new Move(dock, false, IgnoreOLC, false, null, false, true,  false, false, false, false); }
91     private static Move deliver(Dock dock)           { return new Move(dock, false, IgnoreOLC, false, null, false, false, false, false, true,  false); }
92     private static Move wait(Dock dock)              { return new Move(dock, false, IgnoreOLC, false, null, true,  false, false, false, false, false); }
93     private static Move sendto(Dock dock, Path path) { return new Move(dock, false, IgnoreOLC, false, path, false, false, false, false, true,  false); }
94 }