Skip to content

Commit

Permalink
feat(online): Set Online flag for peers when using lib.Info. Cleanups.
Browse files Browse the repository at this point in the history
When getting Info for a peer, set the Online flag appropriately, to match
the behavior of lib.List (peers.go). Do this by checking the other connected
peers that are known to be online, and also checking if you yourself are online.

Fixes part 2 of #577.
  • Loading branch information
dustmop committed Nov 29, 2018
1 parent 107c0dc commit 4542056
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 25 deletions.
37 changes: 16 additions & 21 deletions actions/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/qri-io/qri/config"
"github.com/qri-io/qri/p2p"
"github.com/qri-io/qri/repo/profile"
)

// ListPeers lists Peers on the qri network
Expand All @@ -15,14 +16,17 @@ func ListPeers(node *p2p.QriNode, limit, offset int, onlineOnly bool) ([]*config
return nil, err
}

peers := make([]*config.ProfilePod, limit)
online := []*config.ProfilePod{}
if online, err = ConnectedQriProfiles(node, limit); err != nil {
peers := make([]*config.ProfilePod, 0, limit)
connected, err := ConnectedQriProfiles(node)
if err != nil {
return nil, err
}

if onlineOnly {
return online, nil
for _, p := range connected {
peers = append(peers, p)
}
return peers, nil
}

ps, err := r.Profiles().List()
Expand All @@ -34,38 +38,29 @@ func ListPeers(node *p2p.QriNode, limit, offset int, onlineOnly bool) ([]*config
return []*config.ProfilePod{}, nil
}

i := 0
for _, pro := range ps {
if i >= limit {
if len(peers) >= limit {
break
}
if pro == nil || pro.ID == user.ID {
continue
}

// TODO - this is dumb use a map
for _, olp := range online {
if pro.ID.String() == olp.ID {
pro.Online = true
}
if _, ok := connected[pro.ID]; ok {
pro.Online = true
}

peers[i], err = pro.Encode()
p, err := pro.Encode()
if err != nil {
return nil, err
}

i++
peers = append(peers, p)
}

return peers, nil
}

// ConnectedQriProfiles gives any currently connected qri profiles to this node
func ConnectedQriProfiles(node *p2p.QriNode, limit int) ([]*config.ProfilePod, error) {
parsed := []*config.ProfilePod{}
for _, p := range node.ConnectedQriProfiles() {
parsed = append(parsed, p)
}
return parsed, nil
// ConnectedQriProfiles returns a map from ProfileIDs to profiles for each connected node
func ConnectedQriProfiles(node *p2p.QriNode) (map[profile.ID]*config.ProfilePod, error) {
return node.ConnectedQriProfiles(), nil
}
8 changes: 7 additions & 1 deletion actions/peers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,14 @@ func TestListPeers(t *testing.T) {
func TestConnectedQriProfiles(t *testing.T) {
node := newTestNode(t)

_, err := ConnectedQriProfiles(node, 100)
peers, err := ConnectedQriProfiles(node)
if err != nil {
t.Error(err.Error())
}

if len(peers) != 0 {
t.Errorf("expected 0 connected peers, got %d", len(peers))
}

// TODO: Test for node with at least 1 connected peer
}
40 changes: 37 additions & 3 deletions lib/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,20 @@ func (d *PeerRequests) ConnectedQriProfiles(limit *int, peers *[]*config.Profile
return d.cli.Call("PeerRequests.ConnectedQriProfiles", limit, peers)
}

*peers, err = actions.ConnectedQriProfiles(d.qriNode, *limit)
return err
connected, err := actions.ConnectedQriProfiles(d.qriNode)
if err != nil {
return err
}

build := make([]*config.ProfilePod, intMin(len(connected), *limit))
for _, p := range connected {
build = append(build, p)
if len(build) >= *limit {
break
}
}
*peers = build
return nil
}

// PeerConnectionParamsPod defines parameters for defining a connection
Expand Down Expand Up @@ -197,6 +209,7 @@ func (d *PeerRequests) Info(p *PeerInfoParams, res *config.ProfilePod) error {
return d.cli.Call("PeerRequests.Info", p, res)
}

// TODO: Move most / all of this to actions package, perhaps.
r := d.qriNode.Repo

profiles, err := r.Profiles().List()
Expand All @@ -215,7 +228,21 @@ func (d *PeerRequests) Info(p *PeerInfoParams, res *config.ProfilePod) error {

prof, err := pro.Encode()
*res = *prof
return err

connected, err := actions.ConnectedQriProfiles(d.qriNode)
if err != nil {
return err
}
// If the requested profileID is in the list of connected peers, set Online flag.
if _, ok := connected[pro.ID]; ok {
res.Online = true
}
// If the requested profileID is myself and I'm Online, set Online flag.
if peer.ID(pro.ID) == d.qriNode.ID && d.qriNode.Online {
res.Online = true
}
return nil

}
}

Expand Down Expand Up @@ -253,3 +280,10 @@ func (d *PeerRequests) GetReferences(p *PeerRefsParams, res *[]repo.DatasetRef)
*res = refs
return err
}

func intMin(a, b int) int {
if a < b {
return a
}
return b
}

0 comments on commit 4542056

Please sign in to comment.