Skip to content

Commit 891da8d

Browse files
committed
feat: connections close graciously, promissing
1 parent bd324de commit 891da8d

File tree

4 files changed

+51
-71
lines changed

4 files changed

+51
-71
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ js-libp2p-utp
55
[![](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
66
[![](https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23libp2p)
77
[![Discourse posts](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg)](https://discuss.libp2p.io)
8+
89
[![](https://img.shields.io/codecov/c/github/libp2p/js-libp2p-utp.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-utp)
910
[![](https://img.shields.io/travis/libp2p/js-libp2p-utp.svg?style=flat-square)](https://travis-ci.com/libp2p/js-libp2p-utp)
1011
[![Dependency Status](https://david-dm.org/libp2p/js-libp2p-utp.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-utp)

src/create-listener.js

+12-33
Original file line numberDiff line numberDiff line change
@@ -79,52 +79,31 @@ module.exports = (handler) => {
7979

8080
const lOpts = listeningAddr.toOptions()
8181
log('Listening on %s %s', lOpts.port, lOpts.host)
82-
console.log(Number(lOpts.port), lOpts.port)
8382
server.listen(Number(lOpts.port), lOpts.host, callback)
8483
}
8584

8685
listener.getAddrs = (callback) => {
8786
const multiaddrs = []
88-
const address = server.address()
87+
const addr = server.address()
8988

90-
if (!address) {
89+
if (!addr) {
9190
return callback(new Error('Listener is not ready yet'))
9291
}
9392

94-
// Because TCP will only return the IPv6 version
95-
// we need to capture from the passed multiaddr
96-
/*
97-
if (listeningAddr.toString().indexOf('ip4') !== -1) {
98-
let m = listeningAddr.decapsulate('utp')
99-
m = m.encapsulate('/tcp/' + address.port)
100-
if (ipfsId) {
101-
m = m.encapsulate('/ipfs/' + ipfsId)
102-
}
103-
104-
if (m.toString().indexOf('0.0.0.0') !== -1) {
105-
const netInterfaces = os.networkInterfaces()
106-
Object.keys(netInterfaces).forEach((niKey) => {
107-
netInterfaces[niKey].forEach((ni) => {
108-
if (ni.family === 'IPv4') {
109-
multiaddrs.push(multiaddr(m.toString().replace('0.0.0.0', ni.address)))
110-
}
111-
})
112-
})
113-
} else {
114-
multiaddrs.push(m)
115-
}
93+
let ma
94+
if (addr.family === 'IPv6') {
95+
ma = multiaddr(`/ip6/${addr.address}/udp/${addr.port}/utp`)
96+
} else if (addr.family === 'IPv4') {
97+
console.log(`/ip4/${addr.address}/udp/${addr.port}/utp`)
98+
ma = multiaddr(`/ip4/${addr.address}/udp/${addr.port}/utp`)
11699
}
117-
*/
118100

119-
if (address.family === 'IPv6') {
120-
let ma = multiaddr('/ip6/' + address.address + '/tcp/' + address.port)
121-
if (ipfsId) {
122-
ma = ma.encapsulate('/ipfs/' + ipfsId)
123-
}
124-
125-
multiaddrs.push(ma)
101+
if (ipfsId) {
102+
ma = ma.encapsulate('/ipfs/' + ipfsId)
126103
}
127104

105+
multiaddrs.push(ma)
106+
128107
callback(null, multiaddrs)
129108
}
130109

test/connection.spec.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,16 @@ const expect = chai.expect
77
chai.use(dirtyChai)
88
const UTP = require('../src')
99

10-
describe('Connection', () => {
11-
it('create an instance', () => {
12-
const utp = new UTP()
10+
describe('Valid libp2p Connection', () => {
11+
let utp
12+
13+
beforeEach(() => {
14+
utp = new UTP()
15+
})
16+
17+
it.skip('.getObservedAddrs', (done) => {
1318
expect(utp).to.exist()
1419
})
20+
it.skip('.getPeerInfo', (done) => {})
21+
it.skip('.setPeerInfo', (done) => {})
1522
})

test/listen-dial.spec.js

+28-35
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const multiaddr = require('multiaddr')
1212
const UTP = require('../src')
1313
const isCI = process.env.CI
1414

15-
describe('listen', () => {
15+
describe('Listener (.createListener => listener)', () => {
1616
let utp
1717

1818
function ma (port) {
@@ -24,17 +24,10 @@ describe('listen', () => {
2424
utp = new UTP()
2525
})
2626

27-
it('close listener with connections, through timeout', function (done) {
27+
it('.close with connections, through timeout', function (done) {
2828
this.timeout(20 * 1000)
2929

30-
const listener = utp.createListener((conn) => {
31-
pull(conn, conn)
32-
})
33-
34-
listener.on('connection', () => {
35-
// Testing
36-
console.log('incomming connection')
37-
})
30+
const listener = utp.createListener((conn) => pull(conn, conn))
3831

3932
const addr = ma(6000)
4033
const connectOptions = addr.toOptions()
@@ -46,30 +39,22 @@ describe('listen', () => {
4639
socket1.write('Some data that is never handled')
4740
socket1.end()
4841

49-
// TODO Unfortunately utp has no notion of gracious socket closing
50-
// This feature needs to be shimmed on top to make it a proper libp2p
51-
// transport
52-
socket1.on('error', (err) => {
53-
expect(err).to.not.exist()
54-
})
55-
socket2.on('error', (err) => {
56-
expect(err).to.not.exist()
57-
})
58-
socket1.on('connect', () => {
59-
listener.close(done)
60-
})
42+
socket1.on('error', (err) => expect(err).to.not.exist())
43+
socket2.on('error', (err) => expect(err).to.not.exist())
44+
socket1.on('connect', () => listener.close(done))
6145
})
6246
})
6347

64-
it.skip('listen on port 0', (done) => {
48+
it('.listen on port 0', (done) => {
6549
const listener = utp.createListener((conn) => {})
6650

6751
listener.listen(ma(0), () => {
6852
listener.close(done)
6953
})
7054
})
7155

72-
it.skip('listen on IPv6 addr', function (done) {
56+
// TODO: Get utp to work with IPv6 Addresses
57+
it.skip('.listen on IPv6 addr', function (done) {
7358
if (isCI) { return this.skip() }
7459

7560
const ma = multiaddr('/ip6/::/udp/12000/utp')
@@ -80,7 +65,7 @@ describe('listen', () => {
8065
})
8166
})
8267

83-
it.skip('listen on any Interface', (done) => {
68+
it('.listen on any Interface', (done) => {
8469
const ma = multiaddr('/ip4/0.0.0.0/udp/12000/utp')
8570

8671
const listener = utp.createListener((conn) => {})
@@ -90,7 +75,7 @@ describe('listen', () => {
9075
})
9176
})
9277

93-
it.skip('getAddrs', (done) => {
78+
it('.getAddrs', (done) => {
9479
const listener = utp.createListener((conn) => {})
9580
const addr = ma(12000)
9681

@@ -104,7 +89,7 @@ describe('listen', () => {
10489
})
10590
})
10691

107-
it.skip('getAddrs on port 0 listen', (done) => {
92+
it('.getAddrs on port 0 listen', (done) => {
10893
const addr = ma(0)
10994

11095
const listener = utp.createListener((conn) => {})
@@ -117,7 +102,8 @@ describe('listen', () => {
117102
})
118103
})
119104

120-
it.skip('getAddrs from listening on 0.0.0.0', (done) => {
105+
// TODO: Get utp to understand the meaning of 0.0.0.0
106+
it.skip('.getAddrs from listening on 0.0.0.0', (done) => {
121107
const addr = multiaddr('/ip4/0.0.0.0/udp/12000/utp')
122108

123109
const listener = utp.createListener((conn) => {})
@@ -132,7 +118,8 @@ describe('listen', () => {
132118
})
133119
})
134120

135-
it.skip('getAddrs from listening on 0.0.0.0 and port 0', (done) => {
121+
// TODO: Get utp to understand the meaning of 0.0.0.0
122+
it.skip('.getAddrs from listening on 0.0.0.0 and port 0', (done) => {
136123
const addr = multiaddr('/ip4/0.0.0.0/udp/0/utp')
137124
const listener = utp.createListener((conn) => {})
138125

@@ -146,7 +133,7 @@ describe('listen', () => {
146133
})
147134
})
148135

149-
it.skip('getAddrs preserves IPFS Id', (done) => {
136+
it('.getAddrs preserves IPFS Id', (done) => {
150137
const ipfsId = '/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw'
151138
const addr = ma(9090).encapsulate(ipfsId)
152139

@@ -156,16 +143,22 @@ describe('listen', () => {
156143
listener.getAddrs((err, multiaddrs) => {
157144
expect(err).to.not.exist()
158145
expect(multiaddrs.length).to.equal(1)
159-
expect(multiaddrs[0]).to.eql(ma)
146+
expect(multiaddrs[0]).to.eql(addr)
160147
listener.close(done)
161148
})
162149
})
163150
})
164151
})
165152

166-
describe('dial', () => {
167-
it.skip('create an instance', () => {
168-
const utp = new UTP()
169-
expect(utp).to.exist()
153+
/*
154+
describe('Dialer (.dial)', () => {
155+
let utp
156+
157+
beforeEach(() => {
158+
utp = new UTP()
159+
})
160+
161+
it.skip('things', () => {
170162
})
171163
})
164+
*/

0 commit comments

Comments
 (0)