2019-07-24 18:42:02 +00:00
|
|
|
import React from 'react';
|
2019-09-19 14:27:01 +00:00
|
|
|
import Window from "./Window";
|
2019-07-24 18:42:02 +00:00
|
|
|
|
2019-07-25 00:55:19 +00:00
|
|
|
async function awaitReducer(prev, c) {
|
2019-07-25 14:40:01 +00:00
|
|
|
return {...await prev, ...await c}
|
2019-07-25 00:55:19 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 18:42:02 +00:00
|
|
|
class ConnMgr extends React.Component {
|
2019-07-24 20:34:40 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
|
|
|
|
|
|
|
this.connect = this.connect.bind(this)
|
2019-07-25 00:10:52 +00:00
|
|
|
this.connectAll = this.connectAll.bind(this)
|
|
|
|
this.connect1 = this.connect1.bind(this)
|
|
|
|
this.connectChain = this.connectChain.bind(this)
|
2019-07-25 00:55:19 +00:00
|
|
|
this.getActualState = this.getActualState.bind(this)
|
|
|
|
|
|
|
|
this.state = {conns: {}, lock: true}
|
2019-07-25 00:10:52 +00:00
|
|
|
|
2019-07-25 00:55:19 +00:00
|
|
|
this.getActualState()
|
2019-07-25 14:40:01 +00:00
|
|
|
setInterval(this.getActualState, 500)
|
2019-07-25 00:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async getActualState() {
|
|
|
|
const nodes = this.props.nodes
|
|
|
|
let keys = Object.keys(nodes)
|
|
|
|
|
2019-09-20 09:01:49 +00:00
|
|
|
const newConns = await keys.filter((_, i) => i > 0).filter(kfrom => this.props.nodes[kfrom].conn !== undefined).map(async (kfrom, i) => {
|
|
|
|
return keys.filter((_, j) => i >= j).filter(kto => this.props.nodes[kto].conn !== undefined).map(async kto => {
|
2019-07-25 00:55:19 +00:00
|
|
|
|
|
|
|
const fromNd = this.props.nodes[kfrom]
|
|
|
|
const toNd = this.props.nodes[kto]
|
|
|
|
|
|
|
|
const connectedness = await fromNd.conn.call('Filecoin.NetConnectedness', [toNd.peerid])
|
|
|
|
|
2019-07-25 14:40:01 +00:00
|
|
|
return {[`${kfrom},${kto}`]: connectedness === 1}
|
|
|
|
}).reduce(awaitReducer, Promise.resolve({}))
|
|
|
|
}).reduce(awaitReducer, Promise.resolve({}))
|
2019-07-25 00:55:19 +00:00
|
|
|
|
2019-07-25 14:40:01 +00:00
|
|
|
this.setState({conns: newConns, lock: false})
|
2019-07-24 20:34:40 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 14:40:01 +00:00
|
|
|
async connect(action, from, to, noupdate) {
|
2019-07-25 00:55:19 +00:00
|
|
|
const fromNd = this.props.nodes[from]
|
|
|
|
const toNd = this.props.nodes[to]
|
2019-07-24 21:28:51 +00:00
|
|
|
|
2019-07-25 00:55:19 +00:00
|
|
|
if (action) {
|
2019-07-24 21:28:51 +00:00
|
|
|
const toPeerInfo = await toNd.conn.call('Filecoin.NetAddrsListen', [])
|
|
|
|
|
|
|
|
await fromNd.conn.call('Filecoin.NetConnect', [toPeerInfo])
|
2019-07-25 00:55:19 +00:00
|
|
|
} else {
|
|
|
|
await fromNd.conn.call('Filecoin.NetDisconnect', [toNd.peerid])
|
2019-07-24 20:34:40 +00:00
|
|
|
}
|
2019-07-25 00:10:52 +00:00
|
|
|
|
2019-07-25 14:40:01 +00:00
|
|
|
if (!noupdate)
|
|
|
|
this.setState(prev => ({conns: {...prev.conns, [`${from},${to}`]: action}}))
|
2019-07-25 00:10:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 00:55:19 +00:00
|
|
|
connectAll(discon) {
|
|
|
|
return () => {
|
|
|
|
const nodes = this.props.nodes
|
|
|
|
let keys = Object.keys(nodes)
|
2019-07-25 00:10:52 +00:00
|
|
|
|
2019-07-25 00:55:19 +00:00
|
|
|
keys.filter((_, i) => i > 0).forEach((kfrom, i) => {
|
|
|
|
keys.filter((_, j) => i >= j).forEach((kto, i) => {
|
2019-07-25 14:40:01 +00:00
|
|
|
this.connect(!discon, kfrom, kto, true)
|
2019-07-25 00:55:19 +00:00
|
|
|
})
|
2019-07-25 00:10:52 +00:00
|
|
|
})
|
2019-07-25 00:55:19 +00:00
|
|
|
}
|
2019-07-25 00:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
connect1() {
|
|
|
|
const nodes = this.props.nodes
|
|
|
|
let keys = Object.keys(nodes)
|
|
|
|
|
|
|
|
keys.filter((_, i) => i > 0).forEach((k, i) => {
|
|
|
|
this.connect(true, k, keys[0])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
connectChain() {
|
|
|
|
const nodes = this.props.nodes
|
|
|
|
let keys = Object.keys(nodes)
|
|
|
|
|
|
|
|
keys.filter((_, i) => i > 0).forEach((k, i) => {
|
|
|
|
this.connect(true, k, keys[i])
|
|
|
|
})
|
2019-07-24 20:34:40 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 18:42:02 +00:00
|
|
|
render() {
|
2019-07-24 20:34:40 +00:00
|
|
|
const nodes = this.props.nodes
|
|
|
|
let keys = Object.keys(nodes)
|
|
|
|
|
|
|
|
const rows = keys.filter((_, i) => i > 0).map((k, i) => {
|
|
|
|
const cols = keys.filter((_, j) => i >= j).map((kt, i) => {
|
2019-07-25 00:10:52 +00:00
|
|
|
const checked = this.state.conns[`${k},${kt}`] === true
|
|
|
|
|
2019-07-25 00:55:19 +00:00
|
|
|
return (
|
|
|
|
<td key={k + "," + kt}>
|
|
|
|
<input checked={checked} disabled={this.state.lock} type="checkbox" onChange={e => this.connect(e.target.checked, k, kt)}/>
|
|
|
|
</td>
|
|
|
|
)
|
2019-07-24 20:34:40 +00:00
|
|
|
})
|
|
|
|
return (
|
2019-07-25 00:10:52 +00:00
|
|
|
<tr key={k}><td>{k}</td>{cols}</tr>
|
2019-07-24 20:34:40 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-07-24 18:42:02 +00:00
|
|
|
return(
|
2019-09-19 14:27:01 +00:00
|
|
|
<Window title={`Connection Manager${this.state.lock ? ' (syncing)' : ''}`}>
|
2019-07-24 20:34:40 +00:00
|
|
|
<table>
|
2019-07-25 00:10:52 +00:00
|
|
|
<thead><tr><td></td>{keys.slice(0, -1).map((i) => (<td key={i}>{i}</td>))}</tr></thead>
|
2019-07-24 20:34:40 +00:00
|
|
|
<tbody>{rows}</tbody>
|
|
|
|
</table>
|
2019-07-25 00:10:52 +00:00
|
|
|
<div>
|
2019-07-25 00:55:19 +00:00
|
|
|
<button onClick={this.connectAll(true)}>DisonnAll</button>
|
|
|
|
<button onClick={this.connectAll(false)}>ConnAll</button>
|
2019-07-25 00:10:52 +00:00
|
|
|
<button onClick={this.connect1}>Conn1</button>
|
|
|
|
<button onClick={this.connectChain}>ConnChain</button>
|
|
|
|
</div>
|
2019-09-19 14:27:01 +00:00
|
|
|
</Window>
|
2019-07-24 18:42:02 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ConnMgr
|