Skip to content

Commit

Permalink
dht: changed msgs, include multiple addrs + conn type
Browse files Browse the repository at this point in the history
  • Loading branch information
jbenet committed Dec 9, 2014
1 parent a123773 commit c5c0e7e
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 28 deletions.
7 changes: 5 additions & 2 deletions routing/dht/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,11 +517,14 @@ func (dht *IpfsDHT) peerFromInfo(pbp *pb.Message_Peer) (peer.Peer, error) {
return nil, err
}

maddr, err := pbp.Address()
// add addresses we've just discovered
maddrs, err := pbp.Addresses()
if err != nil {
return nil, err
}
p.AddAddress(maddr)
for _, maddr := range maddrs {
p.AddAddress(maddr)
}
return p, nil
}

Expand Down
10 changes: 6 additions & 4 deletions routing/dht/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,16 @@ func (dht *IpfsDHT) handleAddProvider(ctx context.Context, p peer.Peer, pmes *pb
pid := peer.ID(pb.GetId())
if pid.Equal(p.ID()) {

addr, err := pb.Address()
maddrs, err := pb.Addresses()
if err != nil {
log.Errorf("provider %s error with address %s", p, *pb.Addr)
log.Errorf("provider %s error with addresses %s", p, pb.Addrs)
continue
}

log.Infof("received provider %s %s for %s", p, addr, key)
p.AddAddress(addr)
log.Infof("received provider %s %s for %s", p, maddrs, key)
for _, maddr := range maddrs {
p.AddAddress(maddr)
}
dht.providers.AddProvider(key, p)

} else {
Expand Down
74 changes: 66 additions & 8 deletions routing/dht/pb/dht.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 22 additions & 1 deletion routing/dht/pb/dht.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,30 @@ message Message {
PING = 5;
}

enum ConnectionType {
// sender does not have a connection to peer, and no extra information (default)
NOT_CONNECTED = 0;

// sender has a live connection to peer
CONNECTED = 1;

// sender recently connected to peer
CAN_CONNECT = 2;

// sender recently tried to connect to peer repeatedly but failed to connect
// ("try" here is loose, but this should signal "made strong effort, failed")
CANNOT_CONNECT = 3;
}

message Peer {
// ID of a given peer.
optional string id = 1;
optional string addr = 2;

// multiaddrs for a given peer
repeated string addrs = 2;

// used to signal the sender's connection capabilities to the peer
optional ConnectionType connection = 3;
}

// defines what type of message it is.
Expand Down
27 changes: 17 additions & 10 deletions routing/dht/pb/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dht_pb
import (
"errors"

"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
peer "github.com/jbenet/go-ipfs/peer"
)
Expand All @@ -19,12 +18,11 @@ func NewMessage(typ Message_MessageType, key string, level int) *Message {

func peerToPBPeer(p peer.Peer) *Message_Peer {
pbp := new(Message_Peer)
addrs := p.Addresses()
if len(addrs) == 0 || addrs[0] == nil {
pbp.Addr = proto.String("")
} else {
addr := addrs[0].String()
pbp.Addr = &addr

maddrs := p.Addresses()
pbp.Addrs = make([]string, len(maddrs))
for i, maddr := range maddrs {
pbp.Addrs[i] = maddr.String()
}
pid := string(p.ID())
pbp.Id = &pid
Expand All @@ -41,12 +39,21 @@ func PeersToPBPeers(peers []peer.Peer) []*Message_Peer {
return pbpeers
}

// Address returns a multiaddr associated with the Message_Peer entry
func (m *Message_Peer) Address() (ma.Multiaddr, error) {
// Addresses returns a multiaddr associated with the Message_Peer entry
func (m *Message_Peer) Addresses() ([]ma.Multiaddr, error) {
if m == nil {
return nil, errors.New("MessagePeer is nil")
}
return ma.NewMultiaddr(*m.Addr)

var err error
maddrs := make([]ma.Multiaddr, len(m.Addrs))
for i, addr := range m.Addrs {
maddrs[i], err = ma.NewMultiaddr(addr)
if err != nil {
return nil, err
}
}
return maddrs, nil
}

// GetClusterLevel gets and adjusts the cluster level on the message.
Expand Down
11 changes: 8 additions & 3 deletions routing/dht/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,17 @@ func (dht *IpfsDHT) FindPeer(ctx context.Context, id peer.ID) (peer.Peer, error)
log.Warningf("Received invalid peer from query: %v", err)
continue
}
ma, err := pbp.Address()

// add addresses
maddrs, err := pbp.Addresses()
if err != nil {
log.Warning("Received peer with bad or missing address.")
log.Warning("Received peer with bad or missing addresses: %s", pbp.Addrs)
continue
}
np.AddAddress(ma)
for _, maddr := range maddrs {
np.AddAddress(maddr)
}

if pbp.GetId() == string(id) {
return &dhtQueryResult{
peer: np,
Expand Down

0 comments on commit c5c0e7e

Please sign in to comment.