浏览代码

Add periodic saving

Baglio Tabifata 4 年之前
父节点
当前提交
07d3f745f4
共有 2 个文件被更改,包括 58 次插入5 次删除
  1. 1 1
      src/net/hkzlab/dupal/App.java
  2. 57 4
      src/net/hkzlab/dupal/boardio/DuPALAnalyzer.java

+ 1 - 1
src/net/hkzlab/dupal/App.java

@@ -17,7 +17,7 @@ public class App {
         // PALSpecs pspecs = new PAL16R6Specs();
         PALSpecs pspecs = new PAL16R4Specs();
         // DuPALAnalyzer dpan = new DuPALAnalyzer(dpm, pspecs);
-        DuPALAnalyzer dpan = new DuPALAnalyzer(dpm, pspecs, 0x80);
+        DuPALAnalyzer dpan = new DuPALAnalyzer(dpm, pspecs, 0x80, "/tmp/dupal.dmp");
 
         if(!dpm.enterRemoteMode()) {
             System.out.println("Unable to enter remote mode!");

+ 57 - 4
src/net/hkzlab/dupal/boardio/DuPALAnalyzer.java

@@ -1,5 +1,11 @@
 package net.hkzlab.dupal.boardio;
 
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.Set;
@@ -17,24 +23,62 @@ import net.hkzlab.palanalisys.SubState;
 public class DuPALAnalyzer {
     private final Logger logger = LoggerFactory.getLogger(DuPALAnalyzer.class);
 
-    private final MacroState[] mStates;
+    private static final long SAVE_INTERVAL = 5 * 60 * 1000; // 5 minutes
+
+    private MacroState[] mStates;
 
     private final DuPALManager dpm;
     private final PALSpecs pspecs;
+    private final String serObjPath;
     private int IOasOUT_Mask = -1;
     private int additionalOUTs = 0;
 
-    public DuPALAnalyzer(final DuPALManager dpm, final PALSpecs pspecs, final int IOasOUT_Mask) {
+    public DuPALAnalyzer(final DuPALManager dpm, final PALSpecs pspecs, final int IOasOUT_Mask, String serObjPath) {
         this.dpm = dpm;
         this.pspecs = pspecs;
         this.IOasOUT_Mask = IOasOUT_Mask;
+        this.serObjPath = serObjPath;
 
         this.mStates = new MacroState[1 << pspecs.getNumROUTPins()];
         logger.info("Provisioning for " +this.mStates.length+" possible MacroStates");
     } 
     
     public DuPALAnalyzer(final DuPALManager dpm, final PALSpecs pspecs) {
-        this(dpm, pspecs, -1);
+        this(dpm, pspecs, -1, null);
+    }
+
+    public void saveStatus(final String path) {
+        try {
+            logger.info("Saving state to " + path);
+
+            FileOutputStream fileOut = new FileOutputStream(path);
+            ObjectOutputStream out = new ObjectOutputStream(fileOut);
+        
+            out.writeObject(mStates);
+            out.close();
+            fileOut.close();
+        } catch (IOException e) {
+            logger.warn("Unable to save status to " + path);
+            e.printStackTrace();
+        }
+    }
+
+    public void restoreStatus(final String path) {
+        try {
+            FileInputStream fileIn = new FileInputStream(path);
+            ObjectInputStream in = new ObjectInputStream(fileIn);
+        
+            mStates = (MacroState[])in.readObject();
+            in.close();
+            fileIn.close();
+        } catch (ClassNotFoundException e) {
+            e.printStackTrace();
+        } catch (FileNotFoundException e) {
+            logger.warn("Could not find " + path);
+        } catch (IOException e) {
+            logger.warn("Unable to save status to " + path);
+            e.printStackTrace();
+        }
     }
 
     public void startAnalisys() {
@@ -46,7 +90,9 @@ public class DuPALAnalyzer {
 
         additionalOUTs = calculateAdditionalOutsFromMask(IOasOUT_Mask);
 
+        if(serObjPath != null) restoreStatus(serObjPath);
         internal_analisys();
+        if(serObjPath != null) saveStatus(serObjPath);
     }
 
     private int guessIOs() {
@@ -126,7 +172,14 @@ public class DuPALAnalyzer {
             logger.info("Recovered MacroState ["+ms+"] from index " + mstate_idx);
         }
 
+        long last_save = 0;
         while(true) {
+            long now = System.currentTimeMillis();
+            if(((now - last_save) >= SAVE_INTERVAL) && (serObjPath != null)) {
+                saveStatus(serObjPath);
+                last_save = now;
+            }
+
             if(ms == null) {
                 logger.info("There are no more unknown StateLinks we can reach.");
                 return;
@@ -155,7 +208,7 @@ public class DuPALAnalyzer {
                     logger.info("Found a path to another MacroState: ["+ms+"]");
                     // Traverse the path
                     for(StateLink sl : slPath) {
-                        logger.info("Traversing SL -> " + sl);
+                        logger.trace("Traversing SL -> " + sl);
                         pulseClock(sl.raw_addr);
                     }