more
-add rpc_port argument -run node on localhost for testing
This commit is contained in:
parent
f068daf0b8
commit
0151982bea
2 changed files with 38 additions and 33 deletions
|
@ -17,8 +17,8 @@ import (
|
||||||
type Contact struct {
|
type Contact struct {
|
||||||
ID bits.Bitmap
|
ID bits.Bitmap
|
||||||
IP net.IP
|
IP net.IP
|
||||||
Port int
|
Port int // the udp port used for the dht
|
||||||
PeerPort int
|
PeerPort int // the tcp port a peer can be contacted on for blob requests
|
||||||
}
|
}
|
||||||
|
|
||||||
// Equals returns true if two contacts are the same.
|
// Equals returns true if two contacts are the same.
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
package dht
|
package dht
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"errors"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/gorilla/rpc"
|
"github.com/gorilla/rpc"
|
||||||
"github.com/gorilla/rpc/json"
|
"github.com/gorilla/rpc/json"
|
||||||
|
@ -29,7 +30,6 @@ type PingArgs struct {
|
||||||
|
|
||||||
type PingResult string
|
type PingResult string
|
||||||
|
|
||||||
|
|
||||||
func (n *NodeRPC) Ping(r *http.Request, args *PingArgs, result *PingResult) error {
|
func (n *NodeRPC) Ping(r *http.Request, args *PingArgs, result *PingResult) error {
|
||||||
if rpcServer == nil {
|
if rpcServer == nil {
|
||||||
return errors.New("no node set up")
|
return errors.New("no node set up")
|
||||||
|
@ -75,7 +75,7 @@ func (n *NodeRPC) FindNode(r *http.Request, args *FindArgs, result *FindNodeResu
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
c := Contact{ID: toQuery, IP: net.ParseIP(args.IP), Port: args.Port}
|
c := Contact{ID: toQuery, IP: net.ParseIP(args.IP), Port: args.Port}
|
||||||
req := Request{ Arg: &key, Method: "findNode"}
|
req := Request{Arg: &key, Method: "findNode"}
|
||||||
nodeResponse := rpcServer.Node.Send(c, req)
|
nodeResponse := rpcServer.Node.Send(c, req)
|
||||||
contacts := []ContactResponse{}
|
contacts := []ContactResponse{}
|
||||||
if nodeResponse != nil && nodeResponse.Contacts != nil {
|
if nodeResponse != nil && nodeResponse.Contacts != nil {
|
||||||
|
@ -105,7 +105,7 @@ func (n *NodeRPC) FindValue(r *http.Request, args *FindArgs, result *FindValueRe
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
c := Contact{ID: toQuery, IP: net.ParseIP(args.IP), Port: args.Port}
|
c := Contact{ID: toQuery, IP: net.ParseIP(args.IP), Port: args.Port}
|
||||||
req := Request{ Arg: &key, Method: "findValue"}
|
req := Request{Arg: &key, Method: "findValue"}
|
||||||
nodeResponse := rpcServer.Node.Send(c, req)
|
nodeResponse := rpcServer.Node.Send(c, req)
|
||||||
contacts := []ContactResponse{}
|
contacts := []ContactResponse{}
|
||||||
if nodeResponse != nil && nodeResponse.FindValueKey != "" {
|
if nodeResponse != nil && nodeResponse.FindValueKey != "" {
|
||||||
|
@ -161,7 +161,7 @@ type RoutingTableResponse struct {
|
||||||
Buckets []BucketResponse
|
Buckets []BucketResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
type GetRoutingTableArgs struct {}
|
type GetRoutingTableArgs struct{}
|
||||||
|
|
||||||
func (n *NodeRPC) GetRoutingTable(r *http.Request, args *GetRoutingTableArgs, result *RoutingTableResponse) error {
|
func (n *NodeRPC) GetRoutingTable(r *http.Request, args *GetRoutingTableArgs, result *RoutingTableResponse) error {
|
||||||
if rpcServer == nil {
|
if rpcServer == nil {
|
||||||
|
@ -182,6 +182,20 @@ func (n *NodeRPC) GetRoutingTable(r *http.Request, args *GetRoutingTableArgs, re
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AddKnownNodeResponse struct{}
|
||||||
|
|
||||||
|
func (n *NodeRPC) AddKnownNode(r *http.Request, args *ContactResponse, result *AddKnownNodeResponse) error {
|
||||||
|
if rpcServer == nil {
|
||||||
|
return errors.New("no node set up")
|
||||||
|
}
|
||||||
|
rpcServer.Node.AddKnownNode(
|
||||||
|
Contact{
|
||||||
|
bits.FromHexP(args.NodeID),
|
||||||
|
net.ParseIP(args.IP), args.Port, 0,
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func RunRPCServer(address, rpcPath string, node *BootstrapNode) NodeRPCServer {
|
func RunRPCServer(address, rpcPath string, node *BootstrapNode) NodeRPCServer {
|
||||||
mut.Lock()
|
mut.Lock()
|
||||||
defer mut.Unlock()
|
defer mut.Unlock()
|
||||||
|
@ -189,7 +203,6 @@ func RunRPCServer(address, rpcPath string, node *BootstrapNode) NodeRPCServer {
|
||||||
Wg: sync.WaitGroup{},
|
Wg: sync.WaitGroup{},
|
||||||
Node: node,
|
Node: node,
|
||||||
}
|
}
|
||||||
c := make(chan *http.Server)
|
|
||||||
rpcServer.Wg.Add(1)
|
rpcServer.Wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
s := rpc.NewServer()
|
s := rpc.NewServer()
|
||||||
|
@ -202,15 +215,7 @@ func RunRPCServer(address, rpcPath string, node *BootstrapNode) NodeRPCServer {
|
||||||
server := &http.Server{Addr: address, Handler: r}
|
server := &http.Server{Addr: address, Handler: r}
|
||||||
log.Println("rpc listening on " + address)
|
log.Println("rpc listening on " + address)
|
||||||
server.ListenAndServe()
|
server.ListenAndServe()
|
||||||
c <- server
|
|
||||||
}()
|
|
||||||
go func() {
|
|
||||||
rpcServer.Wg.Wait()
|
|
||||||
close(c)
|
|
||||||
log.Println("stopped rpc listening on " + address)
|
|
||||||
for server := range c {
|
|
||||||
server.Close()
|
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return *rpcServer
|
return *rpcServer
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue