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