chiark / gitweb /
debug output option
[jarrg-ian.git] / src / com / tedpearson / ypp / market / MarketUploader.java
index f91de7aedd25e93cbadbb3df782e46bfee03f0f6..a7a90c8c1489d0dfcfaf3f381226c7d866ad2725 100644 (file)
@@ -40,6 +40,7 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
        private JButton findMarket = null;
        private JLabel resultSummary = null;
        private JLabel arbitrageResult = null;
+        private long startTime = 0;
 
        private final static String PCTB_LIVE_HOST_URL = "http://pctb.crabdance.com/";
        private final static String PCTB_TEST_HOST_URL = "http://pctb.ilk.org/";
@@ -64,6 +65,7 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
 
        private AccessibleContext sidePanel;
        private HashMap<String,Integer> commodMap;
+        public PrintStream dtxt = null;
 
        private PropertyChangeListener changeListener = new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent e) {
@@ -89,6 +91,11 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
                }
        }
 
+        private void progresslog(String s) {
+           long now = new Date().getTime();
+           System.out.println("progress "+(now - startTime)+"ms "+s);
+       }
+
         private void progressNote(ProgressMonitor pm, String s) {
            String arb = null;
            if (arbitrageResult != null)
@@ -230,6 +237,14 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
                // check if we've been turned off in the control panel
                Preferences prefs = Preferences.userNodeForPackage(getClass());
 
+               if (prefs.getBoolean("writeDebugFiles", false)) {
+                   try {
+                       dtxt = new PrintStream(new File("jarrg-debug-log.txt"));
+                   } catch (java.io.FileNotFoundException e) {
+                       System.err.println("JARRG: Error opening debug log: "+e);
+                   }
+               }
+
                if (prefs.getBoolean("useLiveServers", false)) {
                        YARRG_URL = YARRG_LIVE_URL;
                        PCTB_HOST_URL = PCTB_LIVE_HOST_URL;
@@ -272,10 +287,11 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
                                findMarket.setEnabled(false);
                                new Thread() {
                                        public void run() {
+                                               startTime = new Date().getTime();
                                                resultSummary.setText("");
                                                arbitrageResult.setText("");
                                                try {
-                                                       runPCTB();
+                                                       runUpload();
                                                } catch(Exception e) {
                                                        error(e.toString());
                                                        e.printStackTrace();
@@ -392,7 +408,9 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
        *
        *       @exception Exception if an error we didn't expect occured
        */
-       private void runPCTB() throws Exception {
+       private void runUpload() throws Exception {
+               progresslog("starting");
+
                String yarrgts = "";
                ProgressMonitor pm = new ProgressMonitor(frame,"Processing Market Data","Getting table data",0,100);
                pm.setMillisToDecideToPopup(0);
@@ -400,7 +418,9 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
                boolean doneyarrg = false, donepctb = false;
 
                if (uploadToYarrg) {
+                       progresslog("yarrg timestamp...");
                        yarrgts = getYarrgTimestamp();
+                       progresslog("yarrg timestamp done.");
                }
 
                AccessibleTable accesstable = findMarketTable();
@@ -417,13 +437,23 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
                        return;
                }
 
+               progresslog("getisland...");
                getIsland();
+               progresslog("getocean...");
                getOcean();
+               progresslog("getocean done");
 
                if (latch != null) {
                    latch.await(2, java.util.concurrent.TimeUnit.SECONDS);
                }
 
+               if (islandName == null) {
+                   error("Could not find island name in YPP user interface.");
+                   return;
+               }
+
+               progresslog("table check...");
+
                String headings_expected[] = new String[]
                    { "Commodity", "Trading outlet", "Buy price", "Will buy", "Sell price", "Will sell" };
                ArrayList<ArrayList<String>> headers = getData(accesstable.getAccessibleColumnHeader());
@@ -446,13 +476,18 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
                    }
                }
 
+               progresslog("table read...");
+
                ArrayList<ArrayList<String>> data = getData(accesstable);
 
                if (showArbitrage) {
+                       progresslog("arbitrage...");
                        calculateArbitrage(data);
+                       progresslog("arbitrage done.");
                }
 
                if (uploadToYarrg && yarrgts != null) {
+                       progresslog("yarrg prepare...");
                        progressNote(pm, "Yarrg: Preparing data");
                        pm.setProgress(10);
 
@@ -474,15 +509,14 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
                        yarrgdata = yarrgsb.toString();
 
                        progressNote(pm, "Yarrg: Uploading");
+                       progresslog("yarrg upload...");
 
-                       if (islandName != null) {
-                               doneyarrg = runYarrg(yarrgts, oceanName, islandName, yarrgdata);
-                       } else {
-                               System.out.println("Couldn't upload to Yarrg - no island name found");
-                       }
+                       doneyarrg = runYarrg(yarrgts, oceanName, islandName, yarrgdata);
+                       progresslog("yarrg done.");
                }
 
                if (uploadToPCTB) {
+                   progresslog("pctb prepare...");
                    progressNote(pm, "PCTB: Getting stall names");
                    pm.setProgress(20);
                    if(pm.isCanceled()) {
@@ -521,14 +555,18 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
                    dos.writeBytes(getAbbrevStallList(stallMap));
                    writeBuySellOffers(buys,sells,offerCount,out);
                    out.finish();
+                   progresslog("pctb send...");
                    InputStream in = sendInitialData(new ByteArrayInputStream(outStream.toByteArray()));
+                   progresslog("pctb sent.");
                    if (in == null) return;
                    pm.setProgress(80);
                    if(pm.isCanceled()) {
                        return;
                    }
                    progressNote(pm, "PCTB: Waiting ...");
+                   progresslog("pctb finish...");
                    donepctb = finishUpload(in);
+                   progresslog("pctb done.");
                }
                pm.setProgress(100);
 
@@ -540,6 +578,7 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
                } else {
                        resultSummary.setText("uploaded nowhere!");
                }
+               progresslog("done.");
        }
        
        /**
@@ -675,6 +714,13 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
                        int first = sb.indexOf("<pre>") + 5;
                        int last = sb.indexOf("</body>");
                        xml = sb.substring(first,last);
+                       //if (dtxt!=null) {
+                       //        dtxt.println(">>");
+                       //        dtxt.println(sb);;
+                       //        dtxt.println("||");
+                       //        dtxt.println(xml);
+                       //        dtxt.println("<<");
+                       //}
                        //System.out.println(xml);
                        Reader reader = new CharArrayReader(xml.toCharArray());
                        Document d = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(reader));