jaros1 / Money-Network

Demo with complementary and alternative money. Implemented in ZeroNet and AngularJS. Focus on privacy, encryption, max data in client and min data on ZeroNet. Work in progress.
GNU General Public License v2.0
4 stars 4 forks source link

MoneyNetworkAPI: z_file_get wrapper #252

Closed jaros1 closed 6 years ago

jaros1 commented 7 years ago

Add fileGet wrapper to MoneyNetworkAPI. Now three different implementations in MoneyNetworkAPI, MN and W2. From MN:

// ZeroFrame fileGet wrapper. first fileGet request must wait for mergerSiteAdd operation to finish
var inner_path_re1 = /^data\/users\// ; // invalid inner_path. old before merger-site syntax
var inner_path_re2 = /^merged-MoneyNetwork\/(.*?)\/data\/users\/(.*?)$/ ; // extract hub and auth_address
function z_file_get (pgm, options, cb) {
    var inner_path, match2, hub, pos, filename, optional_file, get_optional_file_info ;
    inner_path = options.inner_path ;

    // check inner_path.
    if (inner_path.match(inner_path_re1)) throw pgm + 'Invalid fileGet path. Not a merger-site path. inner_path = ' + inner_path ;
    match2 = inner_path.match(inner_path_re2) ;
    if (match2) {
        hub = match2[1] ;
        if (new_user_data_hub_cbs[hub]) {
            console.log(pgm + 'new data hub ' + hub + '. waiting with fileGet request for ' + inner_path) ;
            new_user_data_hub_cbs[hub].push(function() { z_file_get (pgm, options, cb) }) ;
            return ;
        }
    }
    else throw pgm + 'Invalid fileGet path. Not a merger-site path. inner_path = ' + inner_path ;

    // optional fileGet operation? Some issues with optional fileGet calls
    // problem with fileGet operation for delete optional files. timeout after > 60 seconds. should be solved now. null file_info is returned for deleted optional files
    // also a problem with fileGet operation for optional files without any peer (peer information is not always 100% correct)
    pos = inner_path.lastIndexOf('/') ;
    filename = inner_path.substr(pos+1, inner_path.length-pos) ;
    optional_file = (['content.json', 'data.json', 'status.json', 'like.json', 'avatar.jpg', 'avatar.png', 'wallet.json'].indexOf(filename) == -1);
    debug('z_file_get', pgm + 'filename = ' + JSON.stringify(filename) + ', optional_file = ' + optional_file) ;

    // optional step. get info about optional file before fileGet operation
    // "!file_info.is_downloaded && !file_info.peer" should be not downloaded optional files without any peers
    // but the information is not already correct. peer can be 0 and other client is ready to serve optional file.
    // try a fileGet with required and a "short" timeout
    get_optional_file_info = function (cb) {
        if (!optional_file) return cb(null) ;
        ZeroFrame.cmd("optionalFileInfo", [inner_path], function (file_info) {
            debug('z_file_get', pgm + 'file_info = ' + JSON.stringify(file_info)) ;
            cb(file_info) ;
        }) ; // optionalFileInfo
    } ; // get_optional_file_info
    get_optional_file_info(function(file_info) {
        var cb2_done, cb2, timeout, process_id, debug_seq, warnings, old_options ;
        if (optional_file && !file_info) {
            debug('z_file_get', pgm + 'optional fileGet and no optional file info. must be a deleted optional file. abort fileGet operation') ;
            return cb(null) ;
        }
        if (optional_file) {
            // some additional checks and warnings.
            if (!file_info) {
                debug('z_file_get', pgm + 'optional fileGet and no optional file info. must be a deleted optional file. abort fileGet operation') ;
                return cb(null) ;
            }
            if (!file_info.is_downloaded && !file_info.peer) {
                // not downloaded optional files and (maybe) no peers! peer information is not always correct
                debug('z_file_get', pgm + 'warning. starting fileGet operation for optional file without any peers. file_info = ' + JSON.stringify(file_info)) ;
                warnings = [] ;
                old_options = JSON.stringify(options) ;
                if (!options.required) {
                    options.required = true ;
                    warnings.push('added required=true to fileGet operation') ;
                }
                if (!options.timeout) {
                    options.timeout = 60 ;
                    warnings.push('added timeout=60 to fileGet operation') ;
                }
                if (warnings.length) debug('z_file_get', pgm + 'Warning: ' + warnings.join('. ') + '. old options = ' + old_options + ', new_options = ' + JSON.stringify(options)) ;
            }
        }

        // extend cb. add ZeroNet API debug messages + timeout processing.
        // cb2 is run as fileGet callback or is run by $timeout (sometimes problem with optional fileGet operation running forever)
        cb2_done = false ;
        cb2 = function (data) {
            if (process_id) {
                try {$timeout.cancel(process_id)}
                catch (e) {}
                process_id = null ;
            }
            if (cb2_done) return ; // cb2 has already run
            cb2_done = true ;
            // MoneyNetworkHelper.debug_z_api_operation_end(debug_seq);
            debug_z_api_operation_end(debug_seq, data ? 'OK' : 'Not found');
            cb(data) ;
        } ; // fileGet callback

        timeout = options.timeout || 60 ; // timeout in seconds
        process_id = $timeout(cb2, timeout*1000) ;
        // debug_seq = MoneyNetworkHelper.debug_z_api_operation_start('z_file_get', pgm + inner_path + ' fileGet') ;
        debug_seq = debug_z_api_operation_start(pgm, inner_path, 'fileGet', show_debug('z_file_get')) ;
        ZeroFrame.cmd("fileGet", options, cb2) ;

    }) ; // get_optional_file_info callback

} // z_file_get
jaros1 commented 7 years ago

Also see timeout_count handling in MoneyNetworkAPI.

callback: distinct between timeout response (for example optional fileGet) and real file not found. Now an empty response for both. W2 needs to do something special in case of timeout for money transaction files.