Revision: 2343
          http://vexi.svn.sourceforge.net/vexi/?rev=2343&view=rev
Author:   mkpg2
Date:     2007-09-27 08:32:42 -0700 (Thu, 27 Sep 2007)

Log Message:
-----------
Fix. Use HTTP.HEAD to get info about remote files - so that they aren't 
downloaded twice by the ProgressWatcher.
Moved logic from RemoteArchive to ProgressWatcher.

Modified Paths:
--------------
    trunk/core/org.ibex.js/src/org/ibex/js/Constants.java
    trunk/core/org.ibex.js/src/org/ibex/js/Fountain.java

Modified: trunk/core/org.ibex.js/src/org/ibex/js/Constants.java
===================================================================
--- trunk/core/org.ibex.js/src/org/ibex/js/Constants.java       2007-09-27 
15:28:50 UTC (rev 2342)
+++ trunk/core/org.ibex.js/src/org/ibex/js/Constants.java       2007-09-27 
15:32:42 UTC (rev 2343)
@@ -7,10 +7,10 @@
        static final JS SC_boolean = JSU.S("boolean",true);
        static final JS SC_callee = JSU.S("callee",true);
        static final JS SC_date = JSU.S("date",true);
-       static final JS SC_Downloading = JSU.S("Downloading",true);
        static final JS SC_exception = JSU.S("exception",true);
        static final JS SC_exec = JSU.S("exec",true);
        static final JS SC_faultString = JSU.S("faultString",true);
+       static final JS SC_finish = JSU.S("finish",true);
        static final JS SC_function = JSU.S("function",true);
        static final JS SC_hasNext = JSU.S("hasNext",true);
        static final JS SC_index = JSU.S("index",true);
@@ -25,8 +25,9 @@
        static final JS SC_number = JSU.S("number",true);
        static final JS SC_object = JSU.S("object",true);
        static final JS SC_remote = JSU.S("remote",true);
-       static final JS SC_Progress = JSU.S("Progress",true);
+       static final JS SC_progress = JSU.S("progress",true);
        static final JS SC_some = JSU.S("some",true);
+       static final JS SC_start = JSU.S("start",true);
        static final JS SC_string = JSU.S("string",true);
        static final JS SC_trapee = JSU.S("trapee",true);
        static final JS SC_trapname = JSU.S("trapname",true);

Modified: trunk/core/org.ibex.js/src/org/ibex/js/Fountain.java
===================================================================
--- trunk/core/org.ibex.js/src/org/ibex/js/Fountain.java        2007-09-27 
15:28:50 UTC (rev 2342)
+++ trunk/core/org.ibex.js/src/org/ibex/js/Fountain.java        2007-09-27 
15:32:42 UTC (rev 2343)
@@ -15,17 +15,13 @@
 import java.lang.ref.WeakReference;
 import java.util.HashSet;
 import java.util.Iterator;
-import java.util.NoSuchElementException;
 import java.util.Set;
 import java.util.Vector;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipInputStream;
 
-import org.ibex.js.JS.Enumeration;
-import org.ibex.js.JS.Keys;
 import org.ibex.net.HTTP.HTTPInputStream;
 import org.ibex.util.Cache;
-import org.ibex.util.Callable;
 import org.ibex.util.Log;
 import org.ibex.util.Pausable;
 import org.ibex.util.Pausable.NotPausableException;
@@ -48,13 +44,16 @@
 
     // streams are "sealed" by default to prevent accidental object leakage
     private Cache getCache = new Cache(100, true);
-    protected JS _get(JS key) throws JSExn { return null; }
+    
     public final JS get(JS key) throws JSExn {
         JS ret = (JS)getCache.get(key);
         if (ret == null) getCache.put(key, ret = _get(key));
         return ret;
     }
-
+    /** For delegating to fountain type to get child */
+    protected JS _get(JS key) throws JSExn { return null; }
+    
+    
     // Private Interface 
//////////////////////////////////////////////////////////////////////////////
 
        
@@ -75,7 +74,7 @@
     public void cache(JS principal) throws IOException{};
 
     // Info about the stream (lastModified, contentLength, url ...)
-    public JS getInfo() throws IOException, JSExn{     return new JS.Obj(); }
+    public JS getInfo() throws IOException {   return new JS.Obj(); }
     
     public JS call(JS method, JS[] args) throws JSExn {
        if("info".equals(JSU.toString(method))){
@@ -109,20 +108,26 @@
         public String coerceToString() { return "HTTP:" + url; }
         public HTTP(String url) { while (url.endsWith("/")) url = 
url.substring(0, url.length() - 1); this.url = url; }
         public JS _get(JS key) throws JSExn { return new HTTP(url + "/" + 
JSU.toString(key)); }
-        public JS getInfo() throws IOException, JSExn {
+        public JS getInfo() throws IOException {
+               Log.warn(Fountain.HTTP.class, "getInfo()");
                JS.Obj r = new JS.Obj();
-               HTTPInputStream is = (HTTPInputStream) new 
org.ibex.net.HTTP(url).GET(null, null);
-               try {
-                               r.put(SC_lastModified, 
JSU.S(is.getLastModified()));
-                       r.put(SC_length, JSU.N(is.getLength()));
+               // FIXME FIXME - use HTTP HEAD (not HTTP head)
+               HTTPInputStream his = (HTTPInputStream) new 
org.ibex.net.HTTP(url).HEAD(null, null);
+               try {
+                       r.put(SC_lastModified, JSU.S(his.getLastModified()));
+                       r.put(SC_length, JSU.N(his.getLength()));
                        r.put(SC_name, JSU.S(url));
-                       }finally{
-                               is.close();
+               }catch(JSExn e){
+                       throw new IOException(e);
+               } finally{
+                               his.close();
                        }
                return r;
         }
         //public String getCacheKey(Vec path) throws NotCacheableException { 
return url; }
         public InputStream getInputStream() throws IOException { return new 
org.ibex.net.HTTP(url).GET(null, null); }
+        public InputStream getHeadInputStream() throws IOException { return 
new org.ibex.net.HTTP(url).HEAD(null, null); }
+        
     }
 
     /** byte arrays */
@@ -328,42 +333,73 @@
 
     /** shadow resource which replaces the graft */
     public static class ProgressWatcher extends Fountain {
-        //private final JS[] callargs = new JS[2];
-        final Fountain watchee;
-        Callable callback;
-        public ProgressWatcher(Fountain watchee, Callable callback) { 
this.watchee = watchee; this.callback = callback; }
+        final Fountain watchee;         // underlying stream we are watching
+        final JS principal;             // overlying stream that we are part 
of (could be ourself)
+        private Trap startTrap;         // notified of start
+        private Trap progressTrap;      // notified of progress
+        public int callbackRate = 0;
+        
+        public ProgressWatcher(JS principal, Fountain watchee) throws 
IOException, JSExn { 
+               this.principal = principal; this.watchee = watchee;
+               
+               JS info = getInfo();
+               //      detirmine the callback rate 
+               callbackRate = JSU.toInt(info.get(SC_length))/100;
+               
+               // get traps
+               startTrap = wtrap(SC_start);
+               progressTrap = wtrap(SC_progress);
+
+        }
+        
+        private Trap wtrap(JS key) throws JSExn{
+               Trap r = principal.getTrap(key);
+               return r==null?null:r.write();
+        }
+        
         public InputStream getInputStream() throws IOException {
-            final InputStream is = watchee.getInputStream();
-            return new FilterInputStream(is) {
-                    int bytesDownloaded = 0;
-                    public int read() throws IOException {
-                        int ret = super.read();
-                        if (ret != -1) bytesDownloaded++;
-                        return ret;
-                    }
-                    public int read(byte[] b, int off, int len) throws 
IOException {
-                        int ret = super.read(b, off, len);
-                        if (ret != 1) bytesDownloaded += ret;
-                        JS arg = JSU.N(bytesDownloaded);
-                        //callargs[1] = JSU.N(is instanceof 
KnownLength.KnownLengthInputStream ?
-                        //                    
((KnownLength.KnownLengthInputStream)is).getLength() : 0);
-                        try {
-                            callback.run(arg);
-                        } catch (Exception e) {
-                            Log.warn(ProgressWatcher.class, e);
-                        }
-                        return ret;
-                    }
-                };
+               try{
+                       if(startTrap!=null) 
Thread.runInNew(startTrap.function(), new JS[]{info});
+
+                       return new FilterInputStream(watchee.getInputStream()) {
+                               private int bytesDownloaded = 0;
+                               private int bytesSinceCallback = 0;
+                               public int read() throws IOException {
+                                       int ret = super.read();
+                                       if (ret != -1) bytesDownloaded++;
+                                       return ret;
+                               }
+                               public int read(byte[] b, int off, int len) 
throws IOException {
+                                       int ret = super.read(b, off, len);
+                                       if (ret != 1){ bytesDownloaded += ret; 
bytesSinceCallback += ret;}
+                                       if(bytesSinceCallback>callbackRate){
+                                               try {
+                                                       
bytesSinceCallback-=callbackRate;
+                                                       // FEATURE - if not run 
yet, update previous 
+                                                       // scheduled JS arg. 
+                                                       if(progressTrap!=null) 
+                                                               
Thread.runInNew(progressTrap.function(), new JS[]{JSU.N(bytesDownloaded)});
+                                               } catch (Exception e) {
+                                                       
Log.warn(ProgressWatcher.class, e);
+                                               }
+                                       }
+                                       return ret;
+                               }
+                       };
+               }catch(JSExn e){
+                       throw new IOException(e);
+               }
         }
         
         public JS call(JS method, JS[] args) throws JSExn {
                return watchee.call(method, args);
         }
-        // Not very clean, perhaps should unwrap in the code 
+        // Perhaps not very clean, perhaps should unwrap in the code 
         // and call functions on watchee directly like that.
-        public JS getInfo() throws IOException, JSExn {
-               return watchee.getInfo();
+        private JS info = null;
+        public JS getInfo() throws IOException {
+               if(info==null)info = watchee.getInfo();
+               return info;
         }
     }
 


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Vexi-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/vexi-svn

Reply via email to