2016-06-12 11:01:32 -07:00
|
|
|
'use strict'
|
|
|
|
|
2016-08-09 12:16:16 +02:00
|
|
|
const defer = require('pull-defer/duplex')
|
2016-06-12 11:01:32 -07:00
|
|
|
|
2016-08-09 12:16:16 +02:00
|
|
|
module.exports = class Connection {
|
|
|
|
constructor (conn, info) {
|
|
|
|
this.peerInfo = null
|
|
|
|
this.conn = defer()
|
2016-06-12 11:01:32 -07:00
|
|
|
|
2016-08-09 12:16:16 +02:00
|
|
|
if (conn) {
|
|
|
|
this.setInnerConn(conn, info)
|
|
|
|
} else if (info) {
|
|
|
|
this.info = info
|
|
|
|
}
|
2016-06-12 11:01:32 -07:00
|
|
|
}
|
|
|
|
|
2016-08-09 12:16:16 +02:00
|
|
|
get source () {
|
|
|
|
return this.conn.source
|
|
|
|
}
|
2016-06-12 11:01:32 -07:00
|
|
|
|
2016-08-09 12:16:16 +02:00
|
|
|
get sink () {
|
|
|
|
return this.conn.sink
|
|
|
|
}
|
2016-06-12 11:01:32 -07:00
|
|
|
|
2016-08-09 12:16:16 +02:00
|
|
|
getPeerInfo (callback) {
|
|
|
|
if (this.info && this.info.getPeerInfo) {
|
|
|
|
return this.info.getPeerInfo(callback)
|
2016-06-12 11:01:32 -07:00
|
|
|
}
|
|
|
|
|
2016-08-09 12:16:16 +02:00
|
|
|
if (!this.peerInfo) {
|
2016-06-12 11:01:32 -07:00
|
|
|
return callback(new Error('Peer Info not set yet'))
|
|
|
|
}
|
|
|
|
|
2016-08-09 12:16:16 +02:00
|
|
|
callback(null, this.peerInfo)
|
2016-06-12 11:01:32 -07:00
|
|
|
}
|
|
|
|
|
2016-08-09 12:16:16 +02:00
|
|
|
setPeerInfo (peerInfo) {
|
|
|
|
if (this.info && this.info.setPeerInfo) {
|
|
|
|
return this.info.setPeerInfo(peerInfo)
|
2016-06-12 11:01:32 -07:00
|
|
|
}
|
2016-08-09 12:16:16 +02:00
|
|
|
|
|
|
|
this.peerInfo = peerInfo
|
2016-06-12 11:01:32 -07:00
|
|
|
}
|
|
|
|
|
2016-08-09 12:16:16 +02:00
|
|
|
getObservedAddrs (callback) {
|
|
|
|
if (this.info && this.info.getObservedAddrs) {
|
|
|
|
return this.info.getObservedAddrs(callback)
|
2016-06-12 11:01:32 -07:00
|
|
|
}
|
|
|
|
callback(null, [])
|
|
|
|
}
|
|
|
|
|
2016-08-09 12:16:16 +02:00
|
|
|
setInnerConn (conn, info) {
|
|
|
|
this.conn.resolve(conn)
|
|
|
|
if (info) {
|
|
|
|
this.info = info
|
|
|
|
} else {
|
|
|
|
this.info = conn
|
|
|
|
}
|
2016-06-12 11:01:32 -07:00
|
|
|
}
|
|
|
|
}
|