Changeset 20597


Ignore:
Timestamp:
05/09/16 21:26:22 (9 years ago)
Author:
dlcheng
Message:

CHG (javascript): Adding progress streaming.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/m/classes/clusters/generic.js

    r20588 r20597  
    4747        this.UploadAndRun = function (md,callbackfunction,fid,toolkitsstring,solutionstring,name,runtimename) { //{{{
    4848
    49                 var oReq = new XMLHttpRequest();
    50                 oReq.open("POST", this.url, true);
    51                 oReq.responseType = 'application/octet-stream';
    52                 oReq.position = 0
    53        
    54                 //TODO: dynamic progress update with package lengths and parsing
    55                 oReq.onprogress = function (oEvent) {
    56                         var newResponse = oReq.responseText;
    57                         if (newResponse.length == 13) { //"Starting" length as encoded in base64 length
    58                                 $(".run-button").html("COMPUTING...").prop("disabled", true);
    59                                 console.log("base64 repsonse: " + newResponse + " string response: " + window.atob(oReq.responseText));
     49                var request = new XMLHttpRequest();
     50                $(".run-button").html("CONNECTING...").prop("disabled", true);
     51                request.position = 0; //Keep track of current parsing position in repsonseText
     52                //TODO: Test timeout conditions (timeout will trigger even if ocnnection is successful, if computation takes too long)
     53                request.timeout = 3000;
     54                request.ontimeout = function (event) {
     55                        $(".run-button").html("RUN").prop("disabled", false);
     56                }
     57               
     58                request.upload.onprogress = function(event) {
     59                        var progress = (event.loaded / event.total * 100).toFixed(0);
     60                        $(".run-button").html("UPLOADING: " + progress + "%");
     61        }
     62
     63                request.onprogress = function (event) {
     64                        //Receive updates by parsing message length as a 32-bit hex string of form 0x*09ABCDEF))
     65                        var startIndex = request.position;
     66                        var endIndex = request.position + 10;
     67                        if (request.responseText.length >= endIndex) { //Ensure entire hex string is loaded
     68                                var chunkSize = parseInt(request.responseText.slice(startIndex, endIndex));
     69                                startIndex = endIndex;
     70                                endIndex = startIndex + chunkSize;
     71                                if (chunkSize >= 65535) { //Arbitrary maximium size of message (Must be below minimium size of model results)
     72                                        $(".run-button").html("DOWNLOADING: " + ((request.responseText.length - request.position) / chunkSize * 100).toFixed(0) + "%").prop("disabled", true);
     73                                }
     74                                else if (request.responseText.length >= endIndex) { //Ensure entire chunk is loaded
     75                                        var responseChunk = request.responseText.slice(startIndex, endIndex);
     76                                        $(".run-button").html(responseChunk);
     77                                        request.position = endIndex;
     78                                }
    6079                        }
    6180                };
    6281               
    63                 oReq.onload = function (oEvent) {
     82                request.onload = function (event) {
    6483                        //get context to this.str2ab to avoid duplciation
    6584                        function str2ab(str) {
     
    7089                                return buf;
    7190                        } //}}}
    72                         var buffer2 = str2ab(window.atob(oReq.responseText.slice(13)));                 
     91                        //if (request.responseText.slice(request.position, request.position + 1) == "0x") request.position = request.position + 10;
     92                        var buffer2 = str2ab(window.atob(request.responseText.slice(request.position + 10)));                   
    7393                        var returnBuffer = new Uint8Array(buffer2);
    7494                        var returnBuffer_size=returnBuffer.byteLength;
     
    7999                                console.log(e);
    80100                        }
     101                        $(".run-button").html("RUN").prop("disabled", false);
    81102                        callbackfunction();
    82103                };
     
    102123                solutionlength[0] = solutionbuffer.byteLength;
    103124               
    104                 var binbuffer = new Uint8Array(fid.rawbuffer());
    105                 //var binbuffer = new Uint16Array(fid.rawbuffer()); seems that 16 array bytes length could be incompatible.
     125                var binbuffer = new Uint8Array(fid.rawbuffer()); //seems that 16 array bytes length could be incompatible.
    106126
    107127
     
    111131                var data = new Blob([nplength,npbuffer,runtimenamelength,runtimenamebuffer,namelength,namebuffer,toolkitslength,toolkitsbuffer,solutionlength,solutionbuffer,binlength,binbuffer]);
    112132                //if (typeof(download) != "undefined") download(fid.rawbuffer());
    113                 oReq.send(data);
     133       
     134                request.open("POST", this.url, true);
     135                request.responseType = 'application/octet-stream';
     136                request.send(data);
    114137               
    115138                return;
Note: See TracChangeset for help on using the changeset viewer.