-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from cabal-club/simpler-swarm
PROTOCOL BREAKING: use discovery-swarm for sending peer key
- Loading branch information
Showing
5 changed files
with
176 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,29 @@ | ||
var pump = require('pump') | ||
var discovery = require('discovery-swarm') | ||
var swarmDefaults = require('dat-swarm-defaults') | ||
|
||
module.exports = function (cabal) { | ||
var swarm = discovery(swarmDefaults()) | ||
swarm.join(cabal.key.toString('hex')) | ||
swarm.on('connection', function (conn, info) { | ||
var remoteKey | ||
var ended = false | ||
module.exports = function (cabal, cb) { | ||
cb = cb || function () {} | ||
|
||
cabal.getLocalKey(function (err, key) { | ||
if (key) { | ||
// send local key to remote | ||
conn.write(new Buffer(key, 'hex')) | ||
cabal.getLocalKey(function (err, key) { | ||
if (err) return cb(err) | ||
|
||
// read remote key from remote | ||
conn.once('readable', onReadable) | ||
var swarm = discovery(Object.assign({}, swarmDefaults(), { id: Buffer.from(key, 'hex') })) | ||
swarm.join(cabal.key.toString('hex')) | ||
swarm.on('connection', function (conn, info) { | ||
var remoteKey = info.id.toString('hex') | ||
conn.once('error', function () { if (remoteKey) cabal._removeConnection(remoteKey) }) | ||
conn.once('end', function () { if (remoteKey) cabal._removeConnection(remoteKey) }) | ||
|
||
conn.once('end', function () { | ||
ended = true | ||
}) | ||
|
||
function onReadable () { | ||
if (ended) return | ||
var rkey = conn.read(32) | ||
if (!rkey) { | ||
conn.once('readable', onReadable) | ||
return | ||
} | ||
var r = cabal.replicate() | ||
pump(conn, r, conn, function (err) { | ||
// TODO: report somehow | ||
}) | ||
|
||
remoteKey = rkey.toString('hex') | ||
cabal._addConnection(remoteKey) | ||
replicate() | ||
} | ||
} else { | ||
throw new Error('UNEXPECTED STATE: no local key!') | ||
} | ||
cabal._addConnection(remoteKey) | ||
}) | ||
|
||
function replicate () { | ||
var r = cabal.replicate() | ||
conn.pipe(r).pipe(conn) | ||
r.on('error', noop) | ||
} | ||
|
||
conn.once('error', function () { if (remoteKey) cabal._removeConnection(remoteKey) }) | ||
conn.once('end', function () { if (remoteKey) cabal._removeConnection(remoteKey) }) | ||
cb(null, swarm) | ||
}) | ||
return swarm | ||
} | ||
|
||
function noop () {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters