emscripten Humble Cloud interface
Edward Rudd
2014-08-20 6b0132005065e517f73d349aca8bc372a6f7e281

start building some initial framework for cloud syncing

3 files modified
87 ■■■■■ changed files
humble_cloud.js 53 ●●●●● patch | view | raw | blame | history
main.cpp 17 ●●●● patch | view | raw | blame | history
shell.html 17 ●●●● patch | view | raw | blame | history
humble_cloud.js
@@ -3,13 +3,64 @@
    $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() {
main.cpp
@@ -10,23 +10,32 @@
    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";
shell.html
@@ -1210,7 +1210,8 @@
    <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>
@@ -1234,7 +1235,7 @@
            //text = text.replace(/</g, "&lt;");
            //text = text.replace(/>/g, "&gt;");
            //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
@@ -1293,6 +1294,10 @@
          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)
      {
@@ -1306,7 +1311,13 @@
      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>