diff --git a/lib/server/parse-websocket.js b/lib/server/parse-websocket.js index ec4e606d..8958ece3 100644 --- a/lib/server/parse-websocket.js +++ b/lib/server/parse-websocket.js @@ -5,6 +5,10 @@ var common = require('../common') function parseWebSocketRequest (socket, opts, params) { if (!opts) opts = {} params = JSON.parse(params) // may throw + if (typeof params.info_hash === 'object') { + var infoHash = params.info_hash + params.info_hash = String.fromCharCode.apply(null, infoHash) + } params.type = 'ws' params.socket = socket @@ -21,7 +25,7 @@ function parseWebSocketRequest (socket, opts, params) { } params.peer_id = common.binaryToHex(params.peer_id) - if (params.answer) { + if (params.answer || params.candidate) { if (typeof params.to_peer_id !== 'string' || params.to_peer_id.length !== 20) { throw new Error('invalid `to_peer_id` (required with `answer`)') } diff --git a/lib/server/swarm.js b/lib/server/swarm.js index 2c6f49a8..243c95b9 100644 --- a/lib/server/swarm.js +++ b/lib/server/swarm.js @@ -14,8 +14,8 @@ function Swarm (infoHash, server) { self.incomplete = 0 self.peers = new LRU({ - max: server.peersCacheLength || 1000, - maxAge: server.peersCacheTtl || 20 * 60 * 1000 // 20 minutes + max: 1000, + maxAge: 20 * 60 * 1000 // 20 minutes }) // When a peer is evicted from the LRU store, send a synthetic 'stopped' event diff --git a/server.js b/server.js index 10eb8393..d84e6277 100644 --- a/server.js +++ b/server.js @@ -504,37 +504,11 @@ class Server extends EventEmitter { response.interval = Math.ceil(this.intervalMs / 1000 / 5) } - // Skip sending update back for 'answer' announce messages – not needed - if (!params.answer) { - socket.send(JSON.stringify(response), socket.onSend) - debug('sent response %s to %s', JSON.stringify(response), params.peer_id) - } - - if (Array.isArray(params.offers)) { - debug('got %s offers from %s', params.offers.length, params.peer_id) + // add trickler support + if (params.candidate && params.peer_id) { + debug('got candidate from %s to %s', params.peer_id, params.to_peer_id) + debug('peers length %s', peers.length) debug('got %s peers from swarm %s', peers.length, params.info_hash) - peers.forEach((peer, i) => { - peer.socket.send(JSON.stringify({ - action: 'announce', - offer: params.offers[i].offer, - offer_id: params.offers[i].offer_id, - peer_id: common.hexToBinary(params.peer_id), - info_hash: common.hexToBinary(params.info_hash) - }), peer.socket.onSend) - debug('sent offer to %s from %s', peer.peerId, params.peer_id) - }) - } - - const done = () => { - // emit event once the announce is fully "processed" - if (params.action === common.ACTIONS.ANNOUNCE) { - this.emit(common.EVENT_NAMES[params.event], params.peer_id, params) - } - } - - if (params.answer) { - debug('got answer %s from %s', JSON.stringify(params.answer), params.peer_id) - this.getSwarm(params.info_hash, (err, swarm) => { if (this.destroyed) return if (err) return this.emit('warning', err) @@ -542,24 +516,85 @@ class Server extends EventEmitter { return this.emit('warning', new Error('no swarm with that `info_hash`')) } // Mark the destination peer as recently used in cache - const toPeer = swarm.peers.get(params.to_peer_id) + var toPeer = swarm.peers.get(params.to_peer_id) if (!toPeer) { return this.emit('warning', new Error('no peer with that `to_peer_id`')) } toPeer.socket.send(JSON.stringify({ action: 'announce', - answer: params.answer, + candidate: params.candidate, offer_id: params.offer_id, peer_id: common.hexToBinary(params.peer_id), info_hash: common.hexToBinary(params.info_hash) }), toPeer.socket.onSend) - debug('sent answer to %s from %s', toPeer.peerId, params.peer_id) - + debug('sent candidate to %s from %s', toPeer.peerId, params.peer_id) + const done = () => { + // emit event once the announce is fully "processed" + if (params.action === common.ACTIONS.ANNOUNCE) { + this.emit(common.EVENT_NAMES[params.event], params.peer_id, params) + } + } done() }) } else { - done() + // Skip sending update back for 'answer' announce messages – not needed + if (!params.answer) { + socket.send(JSON.stringify(response), socket.onSend) + debug('sent response %s to %s', JSON.stringify(response), params.peer_id) + } + + if (Array.isArray(params.offers)) { + debug('got %s offers from %s', params.offers.length, params.peer_id) + debug('got %s peers from swarm %s', peers.length, params.info_hash) + peers.forEach((peer, i) => { + peer.socket.send(JSON.stringify({ + action: 'announce', + offer: params.offers[i].offer, + offer_id: params.offers[i].offer_id, + peer_id: common.hexToBinary(params.peer_id), + info_hash: common.hexToBinary(params.info_hash) + }), peer.socket.onSend) + debug('sent offer to %s from %s', peer.peerId, params.peer_id) + }) + } + + const done = () => { + // emit event once the announce is fully "processed" + if (params.action === common.ACTIONS.ANNOUNCE) { + this.emit(common.EVENT_NAMES[params.event], params.peer_id, params) + } + } + + if (params.answer) { + debug('got answer %s from %s', JSON.stringify(params.answer), params.peer_id) + + this.getSwarm(params.info_hash, (err, swarm) => { + if (this.destroyed) return + if (err) return this.emit('warning', err) + if (!swarm) { + return this.emit('warning', new Error('no swarm with that `info_hash`')) + } + // Mark the destination peer as recently used in cache + const toPeer = swarm.peers.get(params.to_peer_id) + if (!toPeer) { + return this.emit('warning', new Error('no peer with that `to_peer_id`')) + } + + toPeer.socket.send(JSON.stringify({ + action: 'announce', + answer: params.answer, + offer_id: params.offer_id, + peer_id: common.hexToBinary(params.peer_id), + info_hash: common.hexToBinary(params.info_hash) + }), toPeer.socket.onSend) + debug('sent answer to %s from %s', toPeer.peerId, params.peer_id) + + done() + }) + } else { + done() + } } }) } diff --git a/start_tracker.sh b/start_tracker.sh new file mode 100644 index 00000000..9e845b96 --- /dev/null +++ b/start_tracker.sh @@ -0,0 +1 @@ +DEBUG=* ./bin/cmd.js -p 8081 --ws --interval 15000 diff --git a/test/ui.html b/test/ui.html new file mode 100644 index 00000000..00004255 --- /dev/null +++ b/test/ui.html @@ -0,0 +1,10 @@ + + + + + + + + RUNNING + + \ No newline at end of file diff --git a/test/ui.js b/test/ui.js new file mode 100644 index 00000000..3925ff71 --- /dev/null +++ b/test/ui.js @@ -0,0 +1,33 @@ +function start1(argument) { + // body... + ws1 = new WebSocket("ws://localhost:8000") + + var t1 = {"info_hash":"dddddddddddddddddddd", + "action": "announce", + "left" : 1000, + "uploaded": 0, + "downloaded": 500, + "peer_id": "eeeeeeeeeeeeeeeeeeee" + } + + var t2 = {"info_hash":"ddddddddddddddddddde", + "action": "announce", + "left" : 1000, + "uploaded": 0, + "downloaded": 500, + "peer_id": "eeeeeeeeeeeeeeeeeeee" + } + + var t3 = {"info_hash":"dddddddddddddddddddf", + "action": "announce", + "left" : 1000, + "uploaded": 0, + "downloaded": 500, + "peer_id": "eeeeeeeeeeeeeeeeeeee" + } + + setInterval( ()=>{ws1.send(JSON.stringify(t1))},5000) + setInterval( ()=>{ws1.send(JSON.stringify(t2))},5000) + setInterval( ()=>{ws1.send(JSON.stringify(t3))},5000) + console.log("started peer1") +}