start building some initial framework for cloud syncing
| | |
| | | $HUMBLE_CLOUD__postset: 'HUMBLE_CLOUD.staticInit()', |
| | | $HUMBLE_CLOUD: { |
| | | applicationToken: '__NOTOKEN__', |
| | | remoteAPIEndpoint: 'http://asmjs.brigadoon.outoforder.cc/cloud/', |
| | | |
| | | staticInit: function() { |
| | | console.log('Humble Cloud Init'); |
| | | addRunDependency('HUMBLE_CLOUD'); |
| | | FS.createFolder('/', 'user_data', true, true); |
| | | FS.createFolder('/', 'user_cloud', true, true); |
| | | FS.mount(HUMBLE_CLOUD, {}, '/user_cloud'); |
| | | console.log('Doing stuff to synchronize files'); |
| | | removeRunDependency('HUMBLE_CLOUD'); |
| | | }, |
| | | |
| | | mount: function(mount) { |
| | | console.log("Mounting", mount); |
| | | return MEMFS.mount.apply(null, arguments); |
| | | }, |
| | | syncfs: function(mount, populate, callback) { |
| | | window.HB_mount = mount; |
| | | console.log("Syncing", mount, populate); |
| | | // re-use IDBFS.getLocalSet for now |
| | | IDBFS.getLocalSet(mount, function(err, local) { |
| | | console.log('local files', local); |
| | | |
| | | mount.type.getRemoteSet(mount, function(err, remote) { |
| | | console.log('remote files', remote); |
| | | }); |
| | | }); |
| | | }, |
| | | |
| | | xhrLoad: function(url, onload, onerror) { |
| | | var xhr = new XMLHttpRequest(); |
| | | xhr.open('GET', url, true); |
| | | xhr.onload = function () { |
| | | if (xhr.status == 200 || (xhr.status == 0 && xhr.response)) { |
| | | onload(xhr.response); |
| | | } else { |
| | | onerror(); |
| | | } |
| | | }; |
| | | xhr.onerror = onerror; |
| | | xhr.send(null); |
| | | }, |
| | | getRemoteSet: function(mount, callback) { |
| | | var url = mount.type.remoteAPIEndpoint + 'storage/files'; |
| | | HUMBLE_CLOUD.xhrLoad(url, function(data) { |
| | | var entries = {}, |
| | | json = JSON.parse(data); |
| | | for(var k in json.data) { |
| | | var f = json.data[k]; |
| | | var absolute = PATH.join2(mount.mountpoint, f.path); |
| | | entries[absolute] = { |
| | | timestamp: new Date(parseInt(f.timestamp)), |
| | | size: f.size |
| | | } |
| | | } |
| | | return callback(null, { type: 'remote', entries: entries } ); |
| | | }, function(e) { |
| | | callback(e || new Error('failed request')); |
| | | }); |
| | | } |
| | | }, |
| | | humble_cloud_init: function() { |
| | |
| | | void test_list_files(); |
| | | } |
| | | |
| | | std::string userPath = "/user_data"; |
| | | std::string userDataPath = "/user_data"; |
| | | std::string userCloudPath = "/user_cloud"; |
| | | |
| | | void test_list_files() |
| | | void list_folder(const std::string& folder) |
| | | { |
| | | std::cout << "Listing Files\n"; |
| | | DIR *d = opendir(userPath.c_str()); |
| | | std::cout << "Listing files in " << folder << "\n"; |
| | | |
| | | DIR *d = opendir(folder.c_str()); |
| | | if (d) |
| | | { |
| | | struct dirent *entry = NULL; |
| | | while((entry = readdir(d))) |
| | | { |
| | | if (entry->d_name[0] == '.') continue; |
| | | std::cout << entry->d_name << "\n"; |
| | | } |
| | | closedir(d); |
| | | } |
| | | } |
| | | |
| | | void test_list_files() |
| | | { |
| | | list_folder(userDataPath); |
| | | list_folder(userCloudPath); |
| | | } |
| | | |
| | | int main(int argc, char * argv[]) |
| | | { |
| | | std::cout << "Welcome Main\n"; |
| | |
| | | <div class="emscripten_border"> |
| | | <span class="controls"> |
| | | <span><input type="button" value="List Files" onclick="call_test_function('test_list_files')"></span> |
| | | <span><input type="button" value="Fetch From Cloud" onclick="fetch_from_cloud()"></span> |
| | | <span><input type="button" value="Fetch from Cloud" onclick="fetch_from_cloud()"></span> |
| | | <span><input type="button" value="Push to Cloud" onclick="push_to_cloud()"></span> |
| | | </span> |
| | | <canvas class="emscripten" id="canvas" oncontextmenu="event.preventDefault()"></canvas> |
| | | </div> |
| | |
| | | //text = text.replace(/</g, "<"); |
| | | //text = text.replace(/>/g, ">"); |
| | | //text = text.replace('\n', '<br>', 'g'); |
| | | console.log(text); |
| | | // console.log(text); |
| | | if (element) { |
| | | element.value += text + "\n"; |
| | | element.scrollTop = element.scrollHeight; // focus on bottom |
| | |
| | | if (text) Module.printErr('[post-exception status] ' + text); |
| | | }; |
| | | }; |
| | | Module['preRun'].push(function() { |
| | | FS.createFolder('/', 'user_data', true, true); |
| | | FS.mount(IDBFS, {}, '/user_data'); |
| | | }); |
| | | var test_functions = {}; |
| | | function call_test_function(func) |
| | | { |
| | |
| | | function fetch_from_cloud() |
| | | { |
| | | FS.syncfs(true, function(e) { |
| | | console.log('Sync complete. Error?: ', e); |
| | | Module.print('Sync from Remote', e); |
| | | }); |
| | | } |
| | | function push_to_cloud() |
| | | { |
| | | FS.syncfs(function(e) { |
| | | Module.print('Sync to Remote', e); |
| | | }); |
| | | } |
| | | </script> |