2019-07-24 17:10:44 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { Client } from 'rpc-websockets'
|
2019-07-24 18:42:02 +00:00
|
|
|
import Cristal from 'react-cristal'
|
2019-07-24 17:10:44 +00:00
|
|
|
|
|
|
|
const stateConnected = 'connected'
|
|
|
|
const stateConnecting = 'connecting'
|
|
|
|
const stateGettingToken = 'getting-token'
|
|
|
|
|
|
|
|
class FullNode extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
|
|
|
|
|
|
|
this.state = {
|
2019-07-24 18:42:02 +00:00
|
|
|
state: stateGettingToken,
|
|
|
|
id: "~"
|
2019-07-24 17:10:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.loadInfo = this.loadInfo.bind(this);
|
|
|
|
|
|
|
|
this.connect()
|
|
|
|
}
|
|
|
|
|
|
|
|
async connect() {
|
|
|
|
console.log("gettok")
|
|
|
|
|
|
|
|
const token = await this.props.pondClient.call('Pond.TokenFor', [this.props.node.ID])
|
|
|
|
|
|
|
|
this.setState(() => ({
|
|
|
|
state: stateConnecting,
|
|
|
|
token: token,
|
|
|
|
}))
|
|
|
|
|
2019-07-24 21:28:51 +00:00
|
|
|
const client = new Client(`ws://127.0.0.1:${this.props.node.ApiPort}/rpc/v0?token=${token}`)
|
2019-07-24 17:10:44 +00:00
|
|
|
client.on('open', () => {
|
|
|
|
this.setState(() => ({
|
|
|
|
state: stateConnected,
|
|
|
|
client: client,
|
|
|
|
|
|
|
|
version: {Version: "~version~"},
|
|
|
|
id: "~peerid~",
|
|
|
|
peers: -1
|
|
|
|
}))
|
|
|
|
|
2019-07-24 20:00:11 +00:00
|
|
|
this.props.onConnect(client)
|
|
|
|
|
2019-07-24 17:10:44 +00:00
|
|
|
this.loadInfo()
|
|
|
|
setInterval(this.loadInfo, 1000)
|
|
|
|
})
|
|
|
|
|
|
|
|
console.log(token) // todo: use
|
|
|
|
}
|
|
|
|
|
|
|
|
async loadInfo() {
|
|
|
|
const version = await this.state.client.call("Filecoin.Version", [])
|
|
|
|
this.setState(() => ({version: version}))
|
|
|
|
|
|
|
|
const id = await this.state.client.call("Filecoin.ID", [])
|
|
|
|
this.setState(() => ({id: id}))
|
|
|
|
|
|
|
|
const peers = await this.state.client.call("Filecoin.NetPeers", [])
|
|
|
|
this.setState(() => ({peers: peers.length}))
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
let runtime = <div></div>
|
|
|
|
if (this.state.state === stateConnected) {
|
|
|
|
runtime = (
|
|
|
|
<div>
|
|
|
|
<div>v{this.state.version.Version}, {this.state.id.substr(-8)}, {this.state.peers} peers</div>
|
2019-07-24 18:42:02 +00:00
|
|
|
<div>{this.props.node.Repo}</div>
|
2019-07-24 17:10:44 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2019-07-24 18:42:02 +00:00
|
|
|
<Cristal
|
|
|
|
title={"Node " + this.props.node.ID}
|
|
|
|
initialPosition={{x: this.props.node.ID*30, y: this.props.node.ID * 30}} >
|
|
|
|
|
|
|
|
<div className="FullNode">
|
|
|
|
<div>{this.props.node.ID} - {this.state.state}</div>
|
|
|
|
{runtime}
|
|
|
|
</div>
|
|
|
|
</Cristal>
|
2019-07-24 17:10:44 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FullNode
|