Skip to content
This repository was archived by the owner on Jan 7, 2022. It is now read-only.

Commit be33628

Browse files
author
Joe Hand
authored
add pause/resume and improve network join/leave (#121)
* add pause/resume and improve network join/leave * add docs * remove db close * use leave for network close * cleanup snapshot test * add console to figure out why test fail * check closed on pause test * use download finished for pause test
1 parent c8cc0c4 commit be33628

File tree

3 files changed

+81
-25
lines changed

3 files changed

+81
-25
lines changed

dat.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@ Dat.prototype.joinNetwork = function (opts, cb) {
5959
cb = opts
6060
opts = {}
6161
}
62-
opts = opts || {}
63-
cb = cb || noop
64-
if (this.network) return this.network.join(this.archive.discoveryKey, { announce: opts.upload }, cb)
6562

6663
var self = this
64+
if (!opts && self.options.network) opts = self.options.network // use previous options
65+
else opts = opts || {}
66+
cb = cb || noop
67+
6768
var netOpts = xtend({
6869
stream: function (peer) {
6970
var stream = self.archive.replicate({
@@ -78,7 +79,7 @@ Dat.prototype.joinNetwork = function (opts, cb) {
7879
}, opts)
7980

8081
var network = self.network = createNetwork(self.archive, netOpts, cb)
81-
self.options.network = network.options
82+
self.options.network = netOpts
8283
network.swarm = network // 1.0 backwards compat. TODO: Remove in v2
8384

8485
if (self.owner) return network
@@ -91,9 +92,23 @@ Dat.prototype.joinNetwork = function (opts, cb) {
9192
}
9293

9394
Dat.prototype.leave =
94-
Dat.prototype.leaveNetwork = function () {
95+
Dat.prototype.leaveNetwork = function (cb) {
9596
if (!this.network) return
97+
debug('leaveNetwork()')
98+
this.archive.unreplicate()
9699
this.network.leave(this.archive.discoveryKey)
100+
this.network.destroy(cb)
101+
delete this.network
102+
}
103+
104+
Dat.prototype.pause = function () {
105+
debug('pause()')
106+
this.leave()
107+
}
108+
109+
Dat.prototype.resume = function () {
110+
debug('resume()')
111+
this.join()
97112
}
98113

99114
Dat.prototype.trackStats = function (opts) {
@@ -132,10 +147,7 @@ Dat.prototype.close = function (cb) {
132147
closeFileWatch(done())
133148
closeArchiveDb(done())
134149

135-
done(function (err) {
136-
if (err) return cb(err)
137-
cb()
138-
})
150+
done(cb)
139151

140152
function closeArchiveDb (cb) {
141153
self.archive.close(function (err) {
@@ -152,7 +164,7 @@ Dat.prototype.close = function (cb) {
152164

153165
function closeNet (cb) {
154166
if (!self.network) return cb()
155-
self.network.close(cb)
167+
self.leave(cb)
156168
}
157169

158170
function closeFileWatch (cb) {

readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,14 @@ Additionally, you can use a `.datignore` file to ignore any the user specifies.
255255

256256
Get upload and download speeds: `stats.network.uploadSpeed` or `stats.network.downloadSpeed`. Transfer speeds are tracked using [hyperdrive-network-speed](https://github.com/joehand/hyperdrive-network-speed/).
257257

258+
#### `dat.pause()`
259+
260+
Pause all upload & downloads. Currently, this is the same as `dat.leaveNetwork()`, which leaves the network and destroys the swarm. Discovery will happen again on `resume()`.
261+
262+
#### `dat.resume()`
263+
264+
Resume network activity. Current, this is the same as `dat.joinNetwork()`.
265+
258266
#### `dat.close(cb)`
259267

260268
Stops replication and closes all the things opened for dat-node, including:

test/download.js

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var os = require('os')
44
var test = require('tape')
55
var rimraf = require('rimraf')
66
var mkdirp = require('mkdirp')
7+
var memdb = require('memdb')
78

89
var Dat = require('..')
910

@@ -26,7 +27,7 @@ test('prep', function (t) {
2627
t.error(err, 'share error okay')
2728
shareKey = dat.key
2829
shareDat = dat
29-
dat.joinNetwork()
30+
dat.joinNetwork({ dht: false })
3031
dat.importFiles({ live: true }, function () { // need live for live download tests!
3132
testFolder(function () {
3233
t.end()
@@ -185,7 +186,46 @@ test('Download with sparse', function (t) {
185186
})
186187
})
187188

188-
test('close first sharer', function (t) {
189+
test('Download pause', function (t) {
190+
testFolder(function () {
191+
Dat(downloadDir, {key: shareKey, db: memdb()}, function (err, dat) {
192+
t.error(err, 'no download init error')
193+
194+
var paused = false
195+
dat.joinNetwork({ dht: false }).once('connection', function () {
196+
dat.pause()
197+
paused = true
198+
199+
dat.archive.on('download', failDownload)
200+
201+
setTimeout(function () {
202+
dat.archive.removeListener('download', failDownload)
203+
dat.resume()
204+
paused = false
205+
}, 500)
206+
207+
function failDownload () {
208+
if (paused) t.fail('download when paused')
209+
}
210+
})
211+
212+
dat.archive.open(function () {
213+
dat.archive.content.on('download-finished', done)
214+
})
215+
216+
function done () {
217+
t.pass('finished download after resume')
218+
if (dat._closed) return
219+
dat.close(function (err) {
220+
t.error(err, 'no error')
221+
t.end()
222+
})
223+
}
224+
})
225+
})
226+
})
227+
228+
test('close first test', function (t) {
189229
shareDat.close(function (err) {
190230
t.error(err, 'no close error')
191231
t.pass('close')
@@ -196,12 +236,13 @@ test('close first sharer', function (t) {
196236

197237
test('download joinNetwork callback without connections', function (t) {
198238
testFolder(function () {
199-
Dat(downloadDir, function (err, dat) {
239+
Dat(downloadDir, {db: memdb()}, function (err, dat) {
200240
t.error(err, 'no error')
201241
dat.joinNetwork(function () {
202242
t.pass('joinNetwork callback')
203243
t.same(dat.network.connected, 0, 'no connections')
204-
dat.close(function () {
244+
dat.close(function (err) {
245+
t.error(err, 'no error')
205246
t.end()
206247
})
207248
})
@@ -211,9 +252,10 @@ test('download joinNetwork callback without connections', function (t) {
211252

212253
test('download from snapshot', function (t) {
213254
var shareKey
255+
var snapshotDat
214256
Dat(fixtures, {live: false}, function (err, dat) {
215257
t.error(err, 'live: false share, no error')
216-
shareDat = dat
258+
snapshotDat = dat
217259
dat.importFiles(function (err) {
218260
t.error(err, 'import no error')
219261
dat.archive.finalize(function (err) {
@@ -255,7 +297,10 @@ test('download from snapshot', function (t) {
255297

256298
dat.close(function () {
257299
t.pass('close callback ok')
258-
t.end()
300+
snapshotDat.close(function () {
301+
rimraf.sync(path.join(fixtures, '.dat'))
302+
t.end()
303+
})
259304
})
260305
})
261306
}
@@ -264,15 +309,6 @@ test('download from snapshot', function (t) {
264309
}
265310
})
266311

267-
test('finished', function (t) {
268-
shareDat.close(function () {
269-
shareDat.db.close(function () {
270-
rimraf.sync(path.join(fixtures, '.dat'))
271-
t.end()
272-
})
273-
})
274-
})
275-
276312
test.onFinish(function () {
277313
rimraf.sync(downloadDir)
278314
})

0 commit comments

Comments
 (0)