-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
'peers' RPC command #78
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package commands | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
|
||
"github.com/jbenet/go-ipfs/core" | ||
b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58" | ||
) | ||
|
||
// peerInfo is a representation of a peer.Peer that is better suited for serialization | ||
type peerInfo struct { | ||
ID string | ||
Addresses []string | ||
PubKey string | ||
Latency int64 | ||
} | ||
|
||
func Peers(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error { | ||
enc := json.NewEncoder(out) | ||
|
||
i := 0 | ||
peers := make([]*peerInfo, len(*n.PeerMap)) | ||
|
||
for _, p := range *n.PeerMap { | ||
addrs := make([]string, len(p.Addresses)) | ||
for i, addr := range p.Addresses { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this 'i' is shadowing 'i' on L22. On 25, you can use the presently underscored variable as an outer i There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'd make them distinct too -- |
||
addrStr, err := addr.String() | ||
if err != nil { | ||
return err | ||
} | ||
addrs[i] = addrStr | ||
} | ||
|
||
pubkeyBytes, err := p.PubKey.Bytes() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
peer := &peerInfo{ | ||
ID: p.ID.Pretty(), | ||
Addresses: addrs, | ||
PubKey: b58.Encode(pubkeyBytes), | ||
Latency: p.GetLatency().Nanoseconds(), | ||
} | ||
peers[i] = peer | ||
i++ | ||
} | ||
|
||
enc.Encode(peers) | ||
|
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,6 +76,7 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) { | |
|
||
var ( | ||
net *swarm.Swarm | ||
peers *peer.Map | ||
// TODO: refactor so we can use IpfsRouting interface instead of being DHT-specific | ||
route* dht.IpfsDHT | ||
swap *bitswap.BitSwap | ||
|
@@ -88,13 +89,15 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) { | |
return nil, err | ||
} | ||
|
||
peers = &peer.Map{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you dont need to take a pointer to map types, theyre already psuedo reference types anyways |
||
|
||
route = dht.NewDHT(local, net, d) | ||
route.Start() | ||
|
||
swap = bitswap.NewBitSwap(local, net, d, route) | ||
swap.SetStrategy(bitswap.YesManStrategy) | ||
|
||
go initConnections(cfg, route) | ||
go initConnections(cfg, peers, route) | ||
} | ||
|
||
bs, err := bserv.NewBlockService(d, swap) | ||
|
@@ -106,7 +109,7 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) { | |
|
||
return &IpfsNode{ | ||
Config: cfg, | ||
PeerMap: &peer.Map{}, | ||
PeerMap: peers, | ||
Datastore: d, | ||
Blocks: bs, | ||
DAG: dag, | ||
|
@@ -155,18 +158,20 @@ func initIdentity(cfg *config.Config) (*peer.Peer, error) { | |
}, nil | ||
} | ||
|
||
func initConnections(cfg *config.Config, route *dht.IpfsDHT) { | ||
for _, p := range cfg.Peers { | ||
maddr, err := ma.NewMultiaddr(p.Address) | ||
func initConnections(cfg *config.Config, peers *peer.Map, route *dht.IpfsDHT) { | ||
for _, sp := range cfg.Peers { | ||
maddr, err := ma.NewMultiaddr(sp.Address) | ||
if err != nil { | ||
u.PErr("error: %v\n", err) | ||
continue | ||
} | ||
|
||
_, err = route.Connect(maddr) | ||
p, err := route.Connect(maddr) | ||
if err != nil { | ||
u.PErr("Bootstrapping error: %v\n", err) | ||
} | ||
|
||
(*peers)[p.Key()] = p | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the slice of peerInfo a slice of pointers in order to satisfy the encoding?
Go provides nice behavior around passing structs by value, so pointer indirection may not be necessary here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
quick note: don't use my code for good use of pointers. I overuse them still.