mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-22 17:31:34 +00:00
first draft of network crawler
This commit is contained in:
@ -42,8 +42,11 @@ func NetInfo() (*ctypes.ResponseNetInfo, error) {
|
||||
peers := []ctypes.Peer{}
|
||||
for _, peer := range p2pSwitch.Peers().List() {
|
||||
peers = append(peers, ctypes.Peer{
|
||||
Address: peer.Connection().RemoteAddress.String(),
|
||||
//Address: peer.Connection().RemoteAddress.String(),
|
||||
Host: peer.Nodeinfo.Host,
|
||||
IsOutbound: peer.IsOutbound(),
|
||||
P2PPort: peer.Nodeinfo.P2PPort,
|
||||
RPCPort: peer.Nodeinfo.RPCPort,
|
||||
})
|
||||
}
|
||||
return &ctypes.ResponseNetInfo{
|
||||
|
@ -78,7 +78,9 @@ type ResponseNetInfo struct {
|
||||
}
|
||||
|
||||
type Peer struct {
|
||||
Address string
|
||||
Host string // ip
|
||||
P2PPort uint16
|
||||
RPCPort uint16
|
||||
IsOutbound bool
|
||||
}
|
||||
|
||||
|
47
rpc/core_client/ws_client.go
Normal file
47
rpc/core_client/ws_client.go
Normal file
@ -0,0 +1,47 @@
|
||||
package core_client
|
||||
|
||||
import (
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/tendermint/tendermint/rpc"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// A websocket client subscribes and unsubscribes to events
|
||||
type WSClient struct {
|
||||
host string
|
||||
conn *websocket.Conn
|
||||
}
|
||||
|
||||
// create a new connection
|
||||
func NewWSClient(addr string) *WSClient {
|
||||
return &WSClient{
|
||||
host: addr,
|
||||
}
|
||||
}
|
||||
|
||||
func (wsc *WSClient) Dial() error {
|
||||
dialer := websocket.DefaultDialer
|
||||
rHeader := http.Header{}
|
||||
conn, _, err := dialer.Dial(wsc.host, rHeader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
wsc.conn = conn
|
||||
return nil
|
||||
}
|
||||
|
||||
// subscribe to an event
|
||||
func (wsc *WSClient) Subscribe(eventid string) error {
|
||||
return wsc.conn.WriteJSON(rpc.WSRequest{
|
||||
Type: "subscribe",
|
||||
Event: eventid,
|
||||
})
|
||||
}
|
||||
|
||||
// unsubscribe from an event
|
||||
func (wsc *WSClient) Unsubscribe(eventid string) error {
|
||||
return wsc.conn.WriteJSON(rpc.WSRequest{
|
||||
Type: "unsubscribe",
|
||||
Event: eventid,
|
||||
})
|
||||
}
|
@ -258,6 +258,7 @@ func (con *WSConnection) Start(evsw *events.EventSwitch) {
|
||||
// close the connection
|
||||
func (con *WSConnection) Stop() {
|
||||
if atomic.CompareAndSwapUint32(&con.stopped, 0, 1) {
|
||||
con.evsw.RemoveListener(con.id)
|
||||
close(con.quitChan)
|
||||
// the write loop closes the websocket connection
|
||||
// when it exits its loop, and the read loop
|
||||
@ -285,6 +286,9 @@ func (con *WSConnection) read() {
|
||||
reaper := time.Tick(time.Second * WSConnectionReaperSeconds)
|
||||
for {
|
||||
select {
|
||||
// TODO: this actually doesn't work
|
||||
// since ReadMessage blocks. Really it needs its own
|
||||
// go routine
|
||||
case <-reaper:
|
||||
if con.failedSends > MaxFailedSends {
|
||||
// sending has failed too many times.
|
||||
|
Reference in New Issue
Block a user