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);
12 new Listener(3).start();
13 new Listener(4).start();
14 new Listener(1).start();
15 new Listener(2).start();
18 public static String pass_string = "password=security_is_for_wimps ";
20 static class Listener extends Thread {
22 public Listener(int devnum) { this.devnum = devnum; }
27 synchronized(Server.class) {
28 System.err.println("thread " + devnum + " listening");
30 System.err.println("connection");
32 new Handler(s, devnum).run();
33 } catch (Exception e) { throw new RuntimeException(e); }
34 try { Thread.sleep(1000); } catch (Exception e) { }
39 static class Handler extends Thread {
40 private Socket socket;
41 boolean closed = false;
43 public Handler(Socket s, int devnum) { this.socket = s; this.devnum = devnum; }
45 RandomAccessFile raf = null;
46 FileInputStream fis = null;
48 final InputStream is = socket.getInputStream();
49 byte[] buf = new byte[1024];
50 StringBuffer sb = new StringBuffer();
54 if (((char)i)=='\n') break;
57 System.err.println("login string: " + sb.toString());
58 if (!sb.toString().startsWith(pass_string)) return;
59 String file = sb.toString().substring(pass_string.length()).trim();
61 System.err.println("unprogramming...");
62 int ret = Runtime.getRuntime().exec(new String[] { "/usr/bin/user_unprogram", ""+devnum }).waitFor();
64 System.err.println("programming error: " + ret);
67 System.err.println("programming...");
68 ret = Runtime.getRuntime().exec(new String[] {
69 "/usr/bin/user_program", ""+devnum, "/var/slipway/"+file }).waitFor();
71 System.err.println("programming error: " + ret);
74 System.err.println("done programming.");
75 try { Thread.sleep(2000); } catch(Exception e) { }
76 final OutputStream os = socket.getOutputStream();
77 System.err.println("sending instructions...");
78 raf = new RandomAccessFile("/dev/selectmap"+devnum, "rw");
81 final RandomAccessFile raf2 = raf;
84 FileOutputStream fos = null;
86 fos = new FileOutputStream(raf2.getFD());
87 byte[] buf = new byte[1024];
89 int numread = is.read(buf, 0, buf.length);
90 if (numread == -1) break;
91 fos.write(buf, 0, numread);
94 } catch (Exception e) { throw new RuntimeException(e);
97 System.err.println("closing...");
98 try { raf2.close(); } catch (Throwable t) { t.printStackTrace(); }
99 try { fos.close(); } catch (Throwable t) { t.printStackTrace(); }
106 System.err.println("reading back...");
107 fis = new FileInputStream(raf.getFD());
111 for(int i=0; i<6; i++) {
113 while(fis.available()==0) {
117 System.err.println("sleep");
126 result |= ((long)val) << (i * 8);
129 System.err.print(result);
130 System.err.print(" 0x");
131 System.err.print(Long.toString(result, 16));
132 System.err.println();
134 System.err.println("done.");
136 } catch (Exception e) { throw new RuntimeException(e);
140 System.err.println("closing...");
143 if (fis != null) fis.close();
144 } catch (Throwable t) { t.printStackTrace(); }