Skip to content

Commit

Permalink
Rename exported functions to fit webrtc-chord's naming, no reason why…
Browse files Browse the repository at this point in the history
… they should be different. Introduce a hacky way of keeping CPU usage low, keeps reloading the background page every hour (issue #7). Have a way of letting other modules know that dht.js has joined the network (so things like restoring documents from cache can happen).
  • Loading branch information
jure committed Jul 16, 2014
1 parent 374c09f commit 1f164be
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions app/scripts/dht.js
Expand Up @@ -4,10 +4,12 @@ var _ = require('underscore');
var $ = require('jquery');
var eliminatedPeers = [];
var chord;
var onSuccess;
var reloadInterval;

var peerJsConfig = {
host: 'scholar.ninja',
//host: 'localhost',
// host: 'localhost',
port: 9003,
debug: 1,
config: {
Expand All @@ -26,7 +28,7 @@ var config = {
numberOfEntriesInSuccessorList: 4,
connectionPoolSize: 10,
connectionOpenTimeout: 10000,
requestTimeout: 60000,
requestTimeout: 120000,
debug: false,
stabilizeTaskInterval: 30000,
fixFingerTaskInterval: 30000,
Expand All @@ -47,8 +49,7 @@ var updatePeerId = function(peerId) {
console.log(error);
// Ignore other errors, are handled elswhere.
if(error.type === 'network') {
chord.leave();
createOrJoin();
chord._localNode._nodeFactory._connectionFactory._peerAgent._peer.reconnect();
}
});

Expand All @@ -60,6 +61,20 @@ var updatePeerId = function(peerId) {
chord.setEntries(obj.entries);
}
});

// Let others know we've joined the network
if(onSuccess) {
onSuccess(peerId);
}

// Temporary fix for Chrome bug: https://code.google.com/p/chromium/issues/detail?id=392651
if(reloadInterval) {
clearInterval(reloadInterval);
}
reloadInterval = setInterval(function() {
chord._localNode._nodeFactory._connectionFactory._peerAgent._peer.disconnect();
window.location.reload();
}, 60*60*1000);
};

var errorHandler = function(error) {
Expand All @@ -68,7 +83,8 @@ var errorHandler = function(error) {
}
};

var createOrJoin = function() {
var createOrJoin = function(onSuccessCallback) {
onSuccess = onSuccessCallback;
var peers = [];
$.get(
'http://' + peerJsConfig.host + ':9001/',
Expand Down Expand Up @@ -121,7 +137,7 @@ var join = function(myPeerId, error) {
// Example: "ID `w34ru68zauz93sor` is taken"
// Usually the taken ID will be a stale node (us from the past)
delete config.peer.id;
} else {
} else if (error.type !== 'network') {
// Examples:
// "Failed to open connection to 8brhes5lytmd9529."
// "FIND_SUCCESSOR request to aoeupaxej1rr7ldi timed out."
Expand All @@ -135,17 +151,13 @@ var join = function(myPeerId, error) {
}
};

window.onunload = window.onbeforeunload = function() {
chord.leave();
};

chord.onentriesinserted = _.debounce(function() {
console.log('Storing entries locally.');
chrome.storage.local.set({entries: chord.getEntries()});
}, 10000);

module.exports = chord;
module.exports.get = chord.retrieve;
module.exports.put = chord.insert;
module.exports.retrieve = chord.retrieve;
module.exports.insert = chord.insert;
module.exports.remove = chord.remove;
module.exports.createOrJoin = createOrJoin;

0 comments on commit 1f164be

Please sign in to comment.