mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-20 04:26:31 +00:00
feat: add delegated peer and content routing support (#242)
* feat: allow for configuring content and peer routing * feat: support multiple peer and content routing modules * docs: add delegated routing example
This commit is contained in:
153
examples/delegated-routing/src/App.js
Normal file
153
examples/delegated-routing/src/App.js
Normal file
@ -0,0 +1,153 @@
|
||||
// eslint-disable-next-line
|
||||
'use strict'
|
||||
|
||||
const React = require('react')
|
||||
const Component = React.Component
|
||||
const Ipfs = require('ipfs')
|
||||
const libp2pBundle = require('./libp2p-bundle')
|
||||
// require('./App.css')
|
||||
|
||||
const BootstrapNode = '/ip4/127.0.0.1/tcp/8081/ws/ipfs/QmdoG8DpzYUZMVP5dGmgmigZwR1RE8Cf6SxMPg1SBXJAQ8'
|
||||
|
||||
class App extends Component {
|
||||
constructor (props) {
|
||||
super(props)
|
||||
this.state = {
|
||||
peers: 0,
|
||||
// This hash is the IPFS readme
|
||||
hash: 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB',
|
||||
// This peer is one of the Bootstrap nodes for IPFS
|
||||
peer: 'QmV6kA2fB8kTr6jc3pL5zbNsjKbmPUHAPKKHRBYe1kDEyc',
|
||||
isLoading: 0
|
||||
}
|
||||
this.peerInterval = null
|
||||
|
||||
this.handleHashChange = this.handleHashChange.bind(this)
|
||||
this.handleHashSubmit = this.handleHashSubmit.bind(this)
|
||||
this.handlePeerChange = this.handlePeerChange.bind(this)
|
||||
this.handlePeerSubmit = this.handlePeerSubmit.bind(this)
|
||||
}
|
||||
|
||||
handleHashChange (event) {
|
||||
this.setState({
|
||||
hash: event.target.value
|
||||
})
|
||||
}
|
||||
handlePeerChange (event) {
|
||||
this.setState({
|
||||
peer: event.target.value
|
||||
})
|
||||
}
|
||||
|
||||
handleHashSubmit (event) {
|
||||
event.preventDefault()
|
||||
this.setState({
|
||||
isLoading: this.state.isLoading + 1
|
||||
})
|
||||
|
||||
this.ipfs.files.cat(this.state.hash, (err, data) => {
|
||||
if (err) console.log('Error', err)
|
||||
|
||||
this.setState({
|
||||
response: data.toString(),
|
||||
isLoading: this.state.isLoading - 1
|
||||
})
|
||||
})
|
||||
}
|
||||
handlePeerSubmit (event) {
|
||||
event.preventDefault()
|
||||
this.setState({
|
||||
isLoading: this.state.isLoading + 1
|
||||
})
|
||||
|
||||
this.ipfs.dht.findpeer(this.state.peer, (err, results) => {
|
||||
if (err) console.log('Error', err)
|
||||
|
||||
this.setState({
|
||||
response: JSON.stringify(results, null, 2),
|
||||
isLoading: this.state.isLoading - 1
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
componentDidMount () {
|
||||
window.ipfs = this.ipfs = new Ipfs({
|
||||
config: {
|
||||
Addresses: {
|
||||
Swarm: []
|
||||
},
|
||||
Discovery: {
|
||||
MDNS: {
|
||||
Enabled: false
|
||||
},
|
||||
webRTCStar: {
|
||||
Enabled: false
|
||||
}
|
||||
},
|
||||
Bootstrap: [
|
||||
BootstrapNode
|
||||
]
|
||||
},
|
||||
preload: {
|
||||
enabled: false
|
||||
},
|
||||
libp2p: libp2pBundle
|
||||
})
|
||||
this.ipfs.on('ready', () => {
|
||||
if (this.peerInterval) {
|
||||
clearInterval(this.peerInterval)
|
||||
}
|
||||
|
||||
this.ipfs.swarm.connect(BootstrapNode, (err) => {
|
||||
if (err) {
|
||||
console.log('Error connecting to the node', err)
|
||||
}
|
||||
console.log('Connected!')
|
||||
})
|
||||
|
||||
this.peerInterval = setInterval(() => {
|
||||
this.ipfs.swarm.peers((err, peers) => {
|
||||
if (err) console.log(err)
|
||||
if (peers) this.setState({peers: peers.length})
|
||||
})
|
||||
}, 2500)
|
||||
})
|
||||
}
|
||||
|
||||
render () {
|
||||
return (
|
||||
<div>
|
||||
<header className="center">
|
||||
<h1>Delegated Routing</h1>
|
||||
<h2>There are currently {this.state.peers} peers.</h2>
|
||||
</header>
|
||||
<section className="center">
|
||||
<form onSubmit={this.handleHashSubmit}>
|
||||
<label>
|
||||
Hash:
|
||||
<input type="text" value={this.state.hash} onChange={this.handleHashChange} />
|
||||
<input type="submit" value="Find" />
|
||||
</label>
|
||||
</form>
|
||||
<form onSubmit={this.handlePeerSubmit}>
|
||||
<label>
|
||||
Peer:
|
||||
<input type="text" value={this.state.peer} onChange={this.handlePeerChange} />
|
||||
<input type="submit" value="Find" />
|
||||
</label>
|
||||
</form>
|
||||
</section>
|
||||
<section className={[this.state.isLoading > 0 ? 'loading' : '', 'loader'].join(' ')}>
|
||||
<div className="lds-ripple"><div></div><div></div></div>
|
||||
</section>
|
||||
<section>
|
||||
<pre>
|
||||
{this.state.response}
|
||||
</pre>
|
||||
</section>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = App
|
9
examples/delegated-routing/src/index.js
Normal file
9
examples/delegated-routing/src/index.js
Normal file
@ -0,0 +1,9 @@
|
||||
// eslint-disable-next-line
|
||||
'use strict'
|
||||
|
||||
const React = require('react') // eslint-disable-line no-unused-vars
|
||||
const ReactDOM = require('react-dom')
|
||||
const App = require('./App') // eslint-disable-line no-unused-vars
|
||||
// require('index.css')
|
||||
|
||||
ReactDOM.render(<App />, document.getElementById('root'))
|
78
examples/delegated-routing/src/libp2p-bundle.js
Normal file
78
examples/delegated-routing/src/libp2p-bundle.js
Normal file
@ -0,0 +1,78 @@
|
||||
// eslint-disable-next-line
|
||||
'use strict'
|
||||
|
||||
const Libp2p = require('libp2p')
|
||||
const Websockets = require('libp2p-websockets')
|
||||
const WebSocketStar = require('libp2p-websocket-star')
|
||||
const WebRTCStar = require('libp2p-webrtc-star')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const KadDHT = require('libp2p-kad-dht')
|
||||
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
|
||||
const DelegatedContentRouter = require('libp2p-delegated-content-routing')
|
||||
|
||||
module.exports = ({peerInfo, peerBook}) => {
|
||||
const wrtcstar = new WebRTCStar({id: peerInfo.id})
|
||||
const wsstar = new WebSocketStar({id: peerInfo.id})
|
||||
const delegatedApiOptions = {
|
||||
host: '0.0.0.0',
|
||||
protocol: 'http',
|
||||
port: '8080'
|
||||
}
|
||||
|
||||
return new Libp2p({
|
||||
peerInfo,
|
||||
peerBook,
|
||||
// Lets limit the connection managers peers and have it check peer health less frequently
|
||||
connectionManager: {
|
||||
maxPeers: 10,
|
||||
pollInterval: 5000
|
||||
},
|
||||
modules: {
|
||||
contentRouting: [
|
||||
new DelegatedContentRouter(peerInfo.id, delegatedApiOptions)
|
||||
],
|
||||
peerRouting: [
|
||||
new DelegatedPeerRouter(delegatedApiOptions)
|
||||
],
|
||||
peerDiscovery: [
|
||||
wrtcstar.discovery,
|
||||
wsstar.discovery
|
||||
],
|
||||
transport: [
|
||||
wrtcstar,
|
||||
wsstar,
|
||||
Websockets
|
||||
],
|
||||
streamMuxer: [
|
||||
MPLEX
|
||||
],
|
||||
connEncryption: [
|
||||
SECIO
|
||||
],
|
||||
dht: KadDHT
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
webrtcStar: {
|
||||
enabled: false
|
||||
},
|
||||
websocketStar: {
|
||||
enabled: false
|
||||
}
|
||||
},
|
||||
dht: {
|
||||
kBucketSize: 20
|
||||
},
|
||||
relay: {
|
||||
enabled: true,
|
||||
hop: {
|
||||
enabled: false
|
||||
}
|
||||
},
|
||||
EXPERIMENTAL: {
|
||||
dht: false
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user