Skip to content
This repository has been archived by the owner on Apr 24, 2023. It is now read-only.

Commit

Permalink
refactor: tidy up the place
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Jan 17, 2017
1 parent dd42a44 commit 50fa9a6
Show file tree
Hide file tree
Showing 17 changed files with 226 additions and 251 deletions.
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const gulp = require('gulp')
const sigServer = require('./src/signalling')
const sigServer = require('./src/sig-server')

let sigS

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "libp2p WebRTC transport that includes a discovery mechanism provided by the signalling-star",
"main": "src/index.js",
"bin": {
"star-sig": "src/signalling/bin.js"
"star-sig": "src/sig-server/bin.js"
},
"browser": {
"wrtc": false
Expand Down Expand Up @@ -78,4 +78,4 @@
"interfect <interfect@gmail.com>",
"michaelfakhri <fakhrimichael@live.com>"
]
}
}
211 changes: 210 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,212 @@
'use strict'

module.exports = require('./webrtc-star')
const debug = require('debug')
const log = debug('libp2p:webrtc-star')
const multiaddr = require('multiaddr')
const mafmt = require('mafmt')
const io = require('socket.io-client')
const EE = require('events').EventEmitter
const wrtc = require('wrtc')
const isNode = require('detect-node')
const SimplePeer = require('simple-peer')
const peerId = require('peer-id')
const PeerInfo = require('peer-info')
const Connection = require('interface-connection').Connection
const toPull = require('stream-to-pull-stream')

exports = module.exports = WebRTCStar

const sioOptions = {
transports: ['websocket'],
'force new connection': true
}

function WebRTCStar () {
if (!(this instanceof WebRTCStar)) {
return new WebRTCStar()
}

let maSelf
const listeners = {}
this.discovery = new EE()

this.dial = function (ma, options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}

if (!callback) {
callback = function noop () {}
}

const intentId = (~~(Math.random() * 1e9)).toString(36) + Date.now()
const sioClient = listeners[Object.keys(listeners)[0]].io

const spOptions = {
initiator: true,
trickle: false
}
if (isNode) {
spOptions.wrtc = wrtc
}
const channel = new SimplePeer(spOptions)

const conn = new Connection(toPull.duplex(channel))
let connected = false

channel.on('signal', function (signal) {
sioClient.emit('ss-handshake', {
intentId: intentId,
srcMultiaddr: maSelf.toString(),
dstMultiaddr: ma.toString(),
signal: signal
})
})

channel.on('timeout', () => {
callback(new Error('timeout'))
})

channel.on('error', (err) => {
if (!connected) {
callback(err)
}
})

sioClient.on('ws-handshake', (offer) => {
if (offer.intentId === intentId && offer.err) {
return callback(new Error(offer.err))
}

if (offer.intentId !== intentId || !offer.answer) {
return
}

channel.on('connect', () => {
connected = true
conn.destroy = channel.destroy.bind(channel)

channel.on('close', () => {
conn.destroy()
})

conn.getObservedAddrs = (callback) => {
return callback(null, [ma])
}

callback(null, conn)
})

channel.signal(offer.signal)
})

return conn
}

this.createListener = (options, handler) => {
if (typeof options === 'function') {
handler = options
options = {}
}

const listener = new EE()

listener.listen = (ma, callback) => {
if (!callback) {
callback = function noop () {}
}
maSelf = ma

const sioUrl = 'http://' + ma.toString().split('/')[3] + ':' + ma.toString().split('/')[5]

listener.io = io.connect(sioUrl, sioOptions)

listener.io.on('connect_error', callback)
listener.io.on('error', (err) => {
console.log('got error')

listener.emit('error', err)
listener.emit('close')
})

listener.io.on('connect', () => {
listener.io.emit('ss-join', ma.toString())
listener.io.on('ws-handshake', incommingDial)
listener.io.on('ws-peer', peerDiscovered.bind(this))
listener.emit('listening')
callback()
})

function incommingDial (offer) {
if (offer.answer || offer.err) {
return
}

const spOptions = {
trickle: false
}
if (isNode) {
spOptions.wrtc = wrtc
}
const channel = new SimplePeer(spOptions)

const conn = new Connection(toPull.duplex(channel))

channel.on('connect', () => {
conn.getObservedAddrs = (callback) => {
return callback(null, [offer.srcMultiaddr])
}

listener.emit('connection', conn)
handler(conn)
})

channel.on('signal', (signal) => {
offer.signal = signal
offer.answer = true
listener.io.emit('ss-handshake', offer)
})

channel.signal(offer.signal)
}
}

listener.close = (callback) => {
if (!callback) {
callback = function noop () {}
}
listener.io.emit('ss-leave')
setTimeout(() => {
listener.emit('close')
callback()
}, 100)
}

listener.getAddrs = (callback) => {
process.nextTick(() => {
callback(null, [maSelf])
})
}

listeners[multiaddr.toString()] = listener
return listener
}

this.filter = (multiaddrs) => {
if (!Array.isArray(multiaddrs)) {
multiaddrs = [multiaddrs]
}
return multiaddrs.filter((ma) => {
return mafmt.WebRTCStar.matches(ma)
})
}

function peerDiscovered (maStr) {
log('Peer Discovered:', maStr)
const id = peerId.createFromB58String(maStr.split('/')[8])
const peer = new PeerInfo(id)
peer.multiaddr.add(multiaddr(maStr))
this.discovery.emit('peer', peer)
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 50fa9a6

Please sign in to comment.