mirror of
https://github.com/arnaucube/go-dht.git
synced 2026-02-06 19:06:44 +01:00
node rpc PING & PONG methods and calls
This commit is contained in:
37
node/node.go
37
node/node.go
@@ -1,11 +1,13 @@
|
||||
package node
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go-dht/config"
|
||||
"go-dht/kademlia"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/rpc"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
@@ -22,7 +24,7 @@ func NewNode() (Node, error) {
|
||||
}
|
||||
|
||||
var n Node
|
||||
n.kademlia = kademlia.NewKademliaTable(id)
|
||||
n.kademlia = kademlia.NewKademliaTable(id, config.C.Addr, config.C.Port)
|
||||
return n, nil
|
||||
}
|
||||
|
||||
@@ -32,15 +34,16 @@ func LoadNode(idStr string) (Node, error) {
|
||||
return Node{}, err
|
||||
}
|
||||
var n Node
|
||||
n.kademlia = kademlia.NewKademliaTable(id)
|
||||
n.kademlia = kademlia.NewKademliaTable(id, config.C.Addr, config.C.Port)
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (n Node) ID() kademlia.ID {
|
||||
return n.kademlia.ID
|
||||
return n.kademlia.N.ID
|
||||
}
|
||||
|
||||
func (n *Node) Start() error {
|
||||
// rpc server
|
||||
err := rpc.Register(n)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -50,11 +53,31 @@ func (n *Node) Start() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
go func() {
|
||||
// TMP in order to print the KBuckets of the node
|
||||
for {
|
||||
fmt.Println(n.kademlia)
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
}()
|
||||
|
||||
go n.pingKnownNodes(config.C.KnownNodes)
|
||||
|
||||
err = http.Serve(listener, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// TODO ping config.C.KnownNodes
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *Node) pingKnownNodes(lns []kademlia.ListedNode) error {
|
||||
for _, ln := range lns {
|
||||
err := n.kademlia.CallPing(ln)
|
||||
if err != nil {
|
||||
log.Warning("[pingKnownNodes]", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -68,7 +91,11 @@ func (n *Node) Ping(ln kademlia.ListedNode, thisLn *kademlia.ListedNode) error {
|
||||
Addr: config.C.Addr,
|
||||
Port: config.C.Port,
|
||||
}
|
||||
// TODO perform PONG call to the requester (maybe ping&pong can be unified)
|
||||
// perform PONG call to the requester (maybe ping&pong can be unified)
|
||||
err := n.kademlia.CallPong(ln)
|
||||
if err != nil {
|
||||
log.Warning("[PONG]", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user