Skip to content

Commit

Permalink
Add discovery drain event - Fix #93
Browse files Browse the repository at this point in the history
  • Loading branch information
mkg20001 committed Sep 10, 2017
1 parent dab99e0 commit 0d97bb0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
10 changes: 2 additions & 8 deletions zeronet-zite/lib/file/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ module.exports = function FileStream(data) {

let dlpath = data.path + "@" + data.site
let othersize = 0
//let fullchunk = []

log("init", dlpath, info)

Expand All @@ -35,21 +34,18 @@ module.exports = function FileStream(data) {
let chunks = []

function finishLoop(err) {
if (chunks.length && err) sendErr = err
if (chunks.length) return cb(null, chunks)
return cb(err)
return cb(err, chunks)
}

function loop() {
if (cur >= info.size) return finishLoop(!log("finished", dlpath, cur))
if (cur >= info.size) return finishLoop(true, log("finished", dlpath, cur))
let args = {
site: data.site,
location: cur,
inner_path: data.path,
}
if (info.size) args.file_size = info.size
peer.cmd("getFile", args, function (err, res) {
//console.log("peere", peer.multiaddr, err)
if (err) {
peer.score -= 20
return finishLoop() //goto: next
Expand All @@ -58,15 +54,13 @@ module.exports = function FileStream(data) {
if (!info.size) return finishLoop()
othersize++
if (othersize == 2) {
//require("fs").writeFileSync("/tmp/site-failed-download",Buffer.concat(fullchunk))
return finishLoop(new Error("Other size"))
}
return finishLoop()
}
if (!info.size) info.size = res.size
cur += res.body.length
log("downloaded", dlpath, cur, info.size)
//fullchunk.push(res.body)
chunks.push(res.body)
return loop()
})
Expand Down
2 changes: 1 addition & 1 deletion zeronet-zite/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ module.exports = function Zite(config, node) { //describes a single zite
i.version = i.authority.version
node.logger("node")("Downloaded %s", i.path)
}),
tree.storage.writeStream(tree.zite.address, 0, i.path)
tree.storage.writeStream(tree.zite.address, 0, i.path.replace(/^\//, ""))
)
})
}, err => err ? console.error(err) : null)
Expand Down
14 changes: 12 additions & 2 deletions zeronet-zite/lib/pool/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,17 @@ module.exports = function PeerStream(zite) {
function PeerList() { //gets peers
const getter = new Getters.MetaGetter([new Getters.OnlineGetter(zite.pool), new Getters.OfflineGetter(zite.pool)])
const dgetter = new Getters.MetaGetter([new Getters.DiscoveryCandidateGetter(zite.pool.main, zite.address)])
let last0 = false
let failedDiscovery = 0
return function (end, cb) {
if (end) return cb(end)
log("peer:list:out read")

if (!last0) failedDiscovery = 0
last0 = false

function getLoop() {
if (failedDiscovery >= 3) return cb(new Error("PeerList drained"))
if (getter.peers) {
if (getter.peers <= 5) zite.discovery.discover() //low peers
const peer_ = getter.getSync()
Expand All @@ -46,12 +52,16 @@ module.exports = function PeerStream(zite) {
log("peer:list:out discovered a peer")
return cb(null, peer)
} else {
log("peer:list:out discovery method fail")
last0 = true
failedDiscovery += 0.1
log("peer:list:out discovery method fail (f = %s)", failedDiscovery)
return getLoop()
}
})
} else {
log("peer:list:out drained. discover")
log("peer:list:out drained. discover (f = %s)", failedDiscovery)
last0 = true
failedDiscovery += 1
zite.discovery.discoverCB(() => {
process.nextTick(getLoop)
})
Expand Down

0 comments on commit 0d97bb0

Please sign in to comment.