add new urjtag-based code, fjmem
[fleet.git] / src / edu / berkeley / fleet / fpga / Server.java
index dfc8b22..eb017ba 100644 (file)
 package edu.berkeley.fleet.fpga;
 
+import gnu.io.*;
 import java.io.*;
 import java.net.*;
 import java.util.*;
+import java.util.concurrent.Semaphore;
 
+// FIXME: accept connections, but stall, during programming
 public class Server {
+    static boolean sign = false;
 
-    public static ServerSocket ss;
-    public static void main(String[] args) throws Exception {
-        ss = new ServerSocket(3133);
-        /*
-        new Listener(3).start();
-        new Listener(4).start();
-        new Listener(1).start();
-        new Listener(2).start();
-        */
-        new Listener(0).start();
+    static long jarFileTime = 0;
+    static long bitFileTime = 0;
+    static {
+        try {
+            jarFileTime = new File("fleet.jar").lastModified();
+            bitFileTime = new File("./main.bit").lastModified();
+        } catch (Exception e) { throw new RuntimeException(e); }
     }
 
-    public static String pass_string = "password=security_is_for_wimps ";
-
-    static class Listener extends Thread {
-        private int devnum;
-        public Listener(int devnum) { this.devnum = devnum; }
-        public void run() {
-            while(true) {
+    public static ServerSocket ss;
+    public static void main(String[] args) throws Exception {
+        System.err.println("programming...");
+        Process proc = Runtime.getRuntime().exec(new String[] {
+                "./misc/program.sh",
+                "./main.bit"
+            });
+        BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
+        String str = null;
+        while((str = br.readLine()) != null) {
+            System.err.println("  " + str);
+        }
+        int ret = proc.waitFor();
+        if (ret != 0) {
+            System.err.println("programming error: " + ret);
+            return;
+        }
+        System.err.println("done programming.");
+        new Thread() {
+            public void run() {
                 try {
-                    Socket s = null;
-                    synchronized(Server.class) {
-                        System.err.println("thread " + devnum + " listening");
-                        s = ss.accept();
-                        System.err.println("connection");
+                    while(true) {
+                        Thread.sleep(500);
+                        if (jarFileTime != new File("fleet.jar").lastModified()) {
+                            System.err.println("jarfile modified; exiting...");
+                            System.exit(0);
+                        }
+                        if (bitFileTime != new File("./main.bit").lastModified()) {
+                            System.err.println("bitfile modified; exiting...");
+                            System.exit(0);
+                        }
                     }
-                    new Handler(s, devnum).run();
                 } catch (Exception e) { throw new RuntimeException(e); }
-                try { Thread.sleep(1000); } catch (Exception e) { }
+            }
+        }.start();
+        ss = new ServerSocket(3133);
+        while(true) {
+            try {
+                Socket s = ss.accept();
+                System.out.println("accept!");
+                new Handler(s).start();
+            } catch (Exception e) {
+                e.printStackTrace();
             }
         }
     }
 
+    public static String pass_string = "password=security_is_for_wimps ";
     static class Handler extends Thread {
         private Socket socket;
         boolean closed = false;
-        private int devnum;
-        public Handler(Socket s, int devnum) { this.socket = s; this.devnum = devnum; }
+        private SerialPort sp;
+        public Handler(Socket s) { this.socket = s; this.sp = sp; }
         public void run() {
-            RandomAccessFile raf = null;
-            FileInputStream fis = null;
+            System.err.println("waiting for Server.class lock...");
+            synchronized(Server.class) {
+                System.err.println("   (got it)");
+                try {
+                    if (new File("/dev/cu.usbserial-FTCBWI2P").exists())
+                        this.sp = new RXTXPort("/dev/cu.usbserial-FTCBWI2P");
+                    else
+                        this.sp = new RXTXPort("/dev/ttyS0");
+                    sp.setInputBufferSize(0);
+                    sp.setOutputBufferSize(0);
+                    //sp.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
+                    sp.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
+                    //sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_IN | sp.FLOWCONTROL_RTSCTS_OUT);
+                    sp.setFlowControlMode(0);
+                    try {
+                        _run();
+                    } finally {
+                        sp.close();
+                    }
+                } catch (Exception e) { throw new RuntimeException(e); }
+            }
+        }
+        public void _run() {
             try {
-                final InputStream is = socket.getInputStream();
+                final InputStream is = new BufferedInputStream(socket.getInputStream());
+                final OutputStream os = socket.getOutputStream();
+                final OutputStream fos = sp.getOutputStream();
+                final InputStream fis = new BufferedInputStream(sp.getInputStream());
+
+                sp.sendBreak(100);
+
+                // read login string
                 byte[] buf = new byte[1024];
                 StringBuffer sb = new StringBuffer();
                 while(true) {
@@ -59,103 +115,63 @@ public class Server {
                 }
                 System.err.println("login string: " + sb.toString());
                 if (!sb.toString().startsWith(pass_string)) return;
-                String file = sb.toString().substring(pass_string.length()).trim();
 
-                System.err.println("programming...");
-                Process proc = Runtime.getRuntime().exec(new String[] { "/afs/megacz.com/work/ml410/program.sh", "/afs/megacz.com/work/ml410/main.bit" });
-                BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
-                String s = null;
-                while((s = br.readLine()) != null) {
-                    System.err.println("  " + s);
-                }
-                int ret = proc.waitFor();
-                if (ret != 0) {
-                    System.err.println("programming error: " + ret);
-                    return;
-                }
-                /*
-                System.err.println("programming...");
-                ret = Runtime.getRuntime().exec(new String[] {
-                        "/usr/bin/user_program", ""+devnum, "/var/slipway/"+file }).waitFor();
-                if (ret != 0) {
-                    System.err.println("programming error: " + ret);
-                   return;
-                }
-                */
-                System.err.println("done programming.");
-                try { Thread.sleep(2000); } catch(Exception e) { }
-                final OutputStream os = socket.getOutputStream();
                 System.err.println("sending instructions...");
-                raf = new RandomAccessFile("/dev/ttyS"+devnum, "rw");
-
-
-                final RandomAccessFile raf2 = raf;
                 new Thread() {
                     public void run() {
-                        FileOutputStream fos = null;
                         try {
-                            fos = new FileOutputStream(raf2.getFD());
-                            byte[] buf = new byte[1024];
                             while(true) {
-                                int numread = is.read(buf, 0, buf.length);
-                                if (numread == -1) break;
-                                fos.write(buf, 0, numread);
+                                int r = is.read();
+                                if (r == -1) break;
+                                System.err.println("write: 0x"+Integer.toString(r & 0xff, 16));
+                                synchronized(fos) {
+                                    fos.write(r);
+                                    if (is.available()==0) fos.flush();
+                                }
+                            }
+                            synchronized(fos) {
                                 fos.flush();
                             }
                         } catch (Exception e) { throw new RuntimeException(e);
                         } finally {
-                            if (raf2 != null) {
-                                System.err.println("closing...");
-                                closed = true;
-                                try { raf2.close(); } catch (Throwable t) { t.printStackTrace(); }
-                                try { fos.close(); } catch (Throwable t) { t.printStackTrace(); }
-                            }
+                            System.err.println("closing...");
+                            closed = true;
+                            try { fos.close(); } catch (Throwable t) { t.printStackTrace(); }
                         }
                     }
                 }.start();
 
                 System.err.println("reading back...");
-                fis = new FileInputStream(raf.getFD());
                 while(true) {
-                    long result = 0;
                     int val = 0;
                     for(int i=0; i<6; i++) {
                         int k = 0;
-                        while(!closed && fis.available()==0) {
+                        while (fis.available()==0) {
                             if (closed) return;
-                            k++;
-                            if (k >= 100) {
-                                System.err.println("sleep");
-                                k = 0;
-                            }
                             Thread.sleep(10);
                         }
-                        if (closed) return;
                         val = fis.read();
                         if (val==-1) break;
+                        if ((val & (3<<6)) == 0) {
+                            synchronized(fos) {
+                                fos.write( (1<<6) | 1);
+                            }
+                        }
+                        System.err.println("read: 0x"+Integer.toString(val & 0xff, 16));
                         os.write((byte)val);
-                        os.flush();
-                        result |= ((long)val) << (i * 8);
                     }
                     if (val==-1) break;
-                    System.err.print(result);
-                    System.err.print(" 0x");
-                    System.err.print(Long.toString(result, 16));
-                    System.err.println();
                 }
+                os.flush();
                 System.err.println("done.");
 
             } catch (Exception e) { throw new RuntimeException(e);
             } finally {
                 try {
-                    if (raf != null) {
-                        System.err.println("closing...");
-                        raf.close();
-                    }
-                    if (fis != null) fis.close();
+                    System.err.println("closing...");
                 } catch (Throwable t) { t.printStackTrace(); }
             }
         }
     }
 
-}
\ No newline at end of file
+}