1 package edu.berkeley.fleet.fpga;
9 public static ServerSocket ss;
10 public static void main(String[] args) throws Exception {
11 ss = new ServerSocket(3133);
13 new Listener(3).start();
14 new Listener(4).start();
15 new Listener(1).start();
16 new Listener(2).start();
18 new Listener(0).start();
21 public static String pass_string = "password=security_is_for_wimps ";
23 static class Listener extends Thread {
25 public Listener(int devnum) { this.devnum = devnum; }
30 synchronized(Server.class) {
31 System.err.println("thread " + devnum + " listening");
33 System.err.println("connection");
35 new Handler(s, devnum).run();
36 } catch (Exception e) { throw new RuntimeException(e); }
37 try { Thread.sleep(1000); } catch (Exception e) { }
42 static class Handler extends Thread {
43 private Socket socket;
44 boolean closed = false;
46 public Handler(Socket s, int devnum) { this.socket = s; this.devnum = devnum; }
48 RandomAccessFile raf = null;
49 FileInputStream fis = null;
51 final InputStream is = socket.getInputStream();
52 byte[] buf = new byte[1024];
53 StringBuffer sb = new StringBuffer();
57 if (((char)i)=='\n') break;
60 System.err.println("login string: " + sb.toString());
61 if (!sb.toString().startsWith(pass_string)) return;
62 String file = sb.toString().substring(pass_string.length()).trim();
64 System.err.println("programming...");
65 Process proc = Runtime.getRuntime().exec(new String[] { "/afs/megacz.com/work/ml410/program.sh", "/afs/megacz.com/work/ml410/main.bit" });
66 BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
68 while((s = br.readLine()) != null) {
69 System.err.println(" " + s);
71 int ret = proc.waitFor();
73 System.err.println("programming error: " + ret);
77 System.err.println("programming...");
78 ret = Runtime.getRuntime().exec(new String[] {
79 "/usr/bin/user_program", ""+devnum, "/var/slipway/"+file }).waitFor();
81 System.err.println("programming error: " + ret);
85 System.err.println("done programming.");
86 try { Thread.sleep(2000); } catch(Exception e) { }
87 final OutputStream os = socket.getOutputStream();
88 System.err.println("sending instructions...");
89 raf = new RandomAccessFile("/dev/ttyS"+devnum, "rw");
92 final RandomAccessFile raf2 = raf;
95 FileOutputStream fos = null;
97 fos = new FileOutputStream(raf2.getFD());
98 byte[] buf = new byte[1024];
100 int numread = is.read(buf, 0, buf.length);
101 if (numread == -1) break;
102 fos.write(buf, 0, numread);
105 } catch (Exception e) { throw new RuntimeException(e);
108 System.err.println("closing...");
110 try { raf2.close(); } catch (Throwable t) { t.printStackTrace(); }
111 try { fos.close(); } catch (Throwable t) { t.printStackTrace(); }
117 System.err.println("reading back...");
118 fis = new FileInputStream(raf.getFD());
122 for(int i=0; i<6; i++) {
124 while(!closed && fis.available()==0) {
128 System.err.println("sleep");
138 result |= ((long)val) << (i * 8);
141 System.err.print(result);
142 System.err.print(" 0x");
143 System.err.print(Long.toString(result, 16));
144 System.err.println();
146 System.err.println("done.");
148 } catch (Exception e) { throw new RuntimeException(e);
152 System.err.println("closing...");
155 if (fis != null) fis.close();
156 } catch (Throwable t) { t.printStackTrace(); }