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