add more debugging output to fpga.Server.java
[fleet.git] / src / edu / berkeley / fleet / fpga / Server.java
1 package edu.berkeley.fleet.fpga;
2
3 import gnu.io.*;
4 import java.io.*;
5 import java.net.*;
6 import java.util.*;
7
8 // FIXME: accept connections, but stall, during programming
9 public class Server {
10     static boolean sign = false;
11
12     static long jarFileTime = 0;
13     static long bitFileTime = 0;
14     static {
15         try {
16             jarFileTime = new File("fleet.jar").lastModified();
17             bitFileTime = new File("build/fpga/main.bit").lastModified();
18         } catch (Exception e) { throw new RuntimeException(e); }
19     }
20
21     public static ServerSocket ss;
22     public static void main(String[] args) throws Exception {
23         System.err.println("programming...");
24         Process proc = Runtime.getRuntime().exec(new String[] {
25                 "misc/program.sh",
26                 "build/fpga/main.bit"
27             });
28         BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
29         String str = null;
30         while((str = br.readLine()) != null) {
31             System.err.println("  " + str);
32         }
33         int ret = proc.waitFor();
34         if (ret != 0) {
35             System.err.println("programming error: " + ret);
36             return;
37         }
38         System.err.println("done programming.");
39         new Thread() {
40             public void run() {
41                 try {
42                     while(true) {
43                         Thread.sleep(500);
44                         if (jarFileTime != new File("fleet.jar").lastModified()) {
45                             System.err.println("jarfile modified; exiting...");
46                             System.exit(0);
47                         }
48                         if (bitFileTime != new File("build/fpga/main.bit").lastModified()) {
49                             System.err.println("bitfile modified; exiting...");
50                             System.exit(0);
51                         }
52                     }
53                 } catch (Exception e) { throw new RuntimeException(e); }
54             }
55         }.start();
56         ss = new ServerSocket(3133);
57         while(true) {
58             try {
59                 Socket s = ss.accept();
60                 System.out.println("accept!");
61                 new Handler(s).start();
62             } catch (Exception e) {
63                 e.printStackTrace();
64             }
65         }
66     }
67
68     public static String pass_string = "password=security_is_for_wimps ";
69     static class Handler extends Thread {
70         private Socket socket;
71         boolean closed = false;
72         private SerialPort sp;
73         public Handler(Socket s) { this.socket = s; this.sp = sp; }
74         public void run() {
75             System.err.println("waiting for Server.class lock...");
76             synchronized(Server.class) {
77                 System.err.println("   (got it)");
78                 try {
79                     this.sp = new RXTXPort("/dev/ttyS0");
80                     sp.setInputBufferSize(0);
81                     sp.setOutputBufferSize(0);
82                     sp.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
83                     sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_IN | sp.FLOWCONTROL_RTSCTS_OUT);
84                     try {
85                         _run();
86                     } finally {
87                         sp.close();
88                     }
89                 } catch (Exception e) { throw new RuntimeException(e); }
90             }
91         }
92         public void _run() {
93             try {
94                 final InputStream is = new BufferedInputStream(socket.getInputStream());
95                 byte[] buf = new byte[1024];
96                 StringBuffer sb = new StringBuffer();
97                 while(true) {
98                     int i = is.read();
99                     if (i==-1) return;
100                     if (((char)i)=='\n') break;
101                     sb.append((char)i);
102                 }
103                 System.err.println("login string: " + sb.toString());
104                 if (!sb.toString().startsWith(pass_string)) return;
105                 final OutputStream os = new BufferedOutputStream(socket.getOutputStream());
106                 System.err.println("sending breaks...");
107                 sp.sendBreak(100);
108
109                 final OutputStream fos = sp.getOutputStream();
110                 final InputStream fis = new BufferedInputStream(sp.getInputStream());
111                 sign = false;
112
113                 System.err.println("sending instructions...");
114                 new Thread() {
115                     public void run() {
116                         try {
117                             while(true) {
118                                 int r = is.read();
119                                 if (r == -1) break;
120                                 fos.write(r);
121                                 if (!sign) {
122                                     fos.flush();
123                                     Thread.sleep(100);
124                                     sign = true;
125                                 }
126                             }
127                             fos.flush();
128                         } catch (Exception e) { throw new RuntimeException(e);
129                         } finally {
130                             System.err.println("closing...");
131                             closed = true;
132                             try { fos.close(); } catch (Throwable t) { t.printStackTrace(); }
133                         }
134                     }
135                 }.start();
136
137
138                 System.err.println("reading back...");
139                 while(true) {
140                     long result = 0;
141                     int val = 0;
142                     boolean hit=false;
143                     for(int i=0; i<6; i++) {
144                         int k = 0;
145                         while(!closed && fis.available()==0) {
146                             if (closed) return;
147                             k++;
148                             if (k >= 100) {
149                                 System.err.println("sleep");
150                                 k = 0;
151                             }
152                             if (k==10) os.flush();
153                             Thread.sleep(10);
154                         }
155                         if (closed) { os.flush(); return; }
156                         val = fis.read();
157                         if (!sign) {
158                             System.err.println("leader byte: 0x" + Integer.toString(val, 16) + " '"+((char)val)+"'");
159                             continue;
160                         }
161                         if (val==-1) break;
162                         System.err.println("byte: 0x"+Integer.toString(val & 0xff, 16));
163                         os.write((byte)val);
164                         result |= ((long)val) << (i * 8);
165                     }
166                     if (val==-1) break;
167                     System.err.print(result);
168                     System.err.print(" 0x");
169                     System.err.print(Long.toString(result, 16));
170                     System.err.println();
171                 }
172                 os.flush();
173                 System.err.println("done.");
174
175             } catch (Exception e) { throw new RuntimeException(e);
176             } finally {
177                 try {
178                     System.err.println("closing...");
179                     /*
180                     fis.close();
181                     if (fis != null) fis.close();
182                     */
183                 } catch (Throwable t) { t.printStackTrace(); }
184             }
185         }
186     }
187
188 }