Skip to content
This repository has been archived by the owner on Jul 16, 2019. It is now read-only.

Commit

Permalink
+handle wait in source to have only one simultaneous download and wai…
Browse files Browse the repository at this point in the history
…t for a few ms after it
  • Loading branch information
jbdemonte committed Jan 19, 2017
1 parent de82e36 commit 41f3c52
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 41 deletions.
10 changes: 6 additions & 4 deletions server/classes/lib/Game.js
Expand Up @@ -4,15 +4,16 @@ var tools = {
string: require(__base + 'server/tools/lib/string')
};

var CACHE_KEYS = 'id sc sid ref img url name size'.split(' ');
var CACHE_KEYS = 'id sc sid ref ori img url name size'.split(' ');

/**
* Game instance
* @param {object} config
* @param {HTMLHandler} item
* @param {string} referer
* @constructor
*/
function Game(config, item, url) {
function Game(config, item, referer) {
var self = this;

self.id = '';
Expand Down Expand Up @@ -78,13 +79,14 @@ function Game(config, item, url) {

}

function extractData(config, item, url) {
function extractData(config, item, referer) {
var self = this;

self.id = tools.string.guid();
self.sc = config.sourceId;
self.sid = config.systemId;
self.ref = url;
self.ref = referer;
self.ori = config.url;

// classic image tag
if (config.pg_games.img) {
Expand Down
98 changes: 61 additions & 37 deletions server/classes/lib/Source.js
Expand Up @@ -63,6 +63,8 @@ function Source(sourcePath) {
this.games = new classes.GameList();

this.engine = new classes.Engine(this.config.origin, this.config.headers);

this.stack = Promise.resolve();
}

util.inherits(Source, EventEmitter);
Expand Down Expand Up @@ -183,6 +185,9 @@ Source.prototype.crawl = function (systemId) {
self._saveCache(systemId);
})
.catch(function (err) {
self.crawling[systemId] = false;
self.emit('crawling', self.crawling);
self.emit('server-error', {error: err.toString()});
console.log(err);
});

Expand All @@ -197,20 +202,19 @@ Source.prototype.download = function (jsonGame) {
var self = this;
var game = self.games.retrieve(jsonGame);

if (!game.download.start()) {
return ;
}

var config = this
.getSystemConfigs(game.sid)
.filter(function (systemConfig) {
return game.ref.indexOf(engine.completeURL(systemConfig.url)) === 0;
return game.ori === systemConfig.url;
})
.pop();

if (!config) {
// should never happen
game.download.end(false);
return ;
}

if (!game.download.start()) {
return ;
}

Expand All @@ -225,41 +229,58 @@ Source.prototype.download = function (jsonGame) {

engine.on(progressEventName, progress);

var tasks = [];
var files = [];
var filename, tmpfile;
function start() {
var filename, tmpfile;
var tasks = [];
var files = [];

if (config.pg_game) {
tasks = Array.isArray(config.pg_game) ? config.pg_game.slice() : [config.pg_game];
}

self.emit('started', {game: game});

if (config.pg_game) {
tasks = Array.isArray(config.pg_game) ? config.pg_game.slice() : [config.pg_game];
return self._download(game, progressEventName, tasks)
.then(function (response) {
engine.removeListener(progressEventName, progress);
if (response) {
filename = response.filename();
return tools.fs.saveToTmpFile(response.body, filename);
}
})
.then(function (_tmpfile) {
tmpfile = _tmpfile;
return tools.systems.get(game.sid).handleFile(tmpfile, filename, files);
})
.then(function (renamed) {
game.download.end(true);
self.emit('complete', {game: game, files: files});
if (!renamed) {
return tools.fs.unlink(tmpfile);
}
})
.catch(function (err) {
game.download.end(false);
self.emit('server-error', {error: err.toString()});
console.log(err);
console.log(err.stack);
});
}

this._download(game, progressEventName, tasks)
.then(function (response) {
engine.removeListener(progressEventName, progress);
if (response) {
filename = response.filename();
return tools.fs.saveToTmpFile(response.body, filename);
}
})
.then(function (_tmpfile) {
tmpfile = _tmpfile;
return tools.systems.get(game.sid).handleFile(tmpfile, filename, files);
})
.then(function (renamed) {
game.download.end(true);

self.emit('complete', {
game: game,
files: files
// If current web ressource required not to be a pig by downloading only one file in same time, we stack the promise
if (self.config.wait) {
self.stack = self.stack
.then(start)
.then(function () {
return new Promise(function (resolve) {
var duration = typeof self.config.wait === 'function' ? self.config.wait(game) : self.config.wait;
self.emit('pause', {duration: duration});
setTimeout(resolve, duration);
});
});
if (!renamed) {
return tools.fs.unlink(tmpfile);
}
})
.catch(function (err) {
game.download.end(false);
console.log(err);
});
} else {
start();
}

};

Expand Down Expand Up @@ -314,6 +335,9 @@ Source.prototype._download = function (game, progressEventName, tasks) {
if (!query.method) {
return Promise.reject('Unknown method');
}
if (!query.url) {
return Promise.reject('Download URL is missing');
}
if (tasks.length) {
return handle(engine.send(query, {referer: game.ref}));

Expand Down

0 comments on commit 41f3c52

Please sign in to comment.