Message.go 1.35 KB
Newer Older
1 2
package dht

Jeromy's avatar
Jeromy committed
3
import (
4
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
Jeromy's avatar
Jeromy committed
5
	peer "github.com/jbenet/go-ipfs/peer"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
6
	u "github.com/jbenet/go-ipfs/util"
Jeromy's avatar
Jeromy committed
7 8
)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9
func peerToPBPeer(p *peer.Peer) *Message_Peer {
10
	pbp := new(Message_Peer)
Jeromy's avatar
Jeromy committed
11 12 13 14 15 16 17 18 19
	if len(p.Addresses) == 0 || p.Addresses[0] == nil {
		pbp.Addr = proto.String("")
	} else {
		addr, err := p.Addresses[0].String()
		if err != nil {
			//Temp: what situations could cause this?
			panic(err)
		}
		pbp.Addr = &addr
Jeromy's avatar
Jeromy committed
20 21 22 23
	}
	pid := string(p.ID)
	pbp.Id = &pid
	return pbp
24 25
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
26 27 28 29 30 31 32 33
func peersToPBPeers(peers []*peer.Peer) []*Message_Peer {
	pbpeers = make([]*Message_Peer, len(peers))
	for i, p := range peers {
		pbpeers[i] = peerToPBPeer(p)
	}
	return pbpeers
}

34 35 36 37
// GetClusterLevel gets and adjusts the cluster level on the message.
// a +/- 1 adjustment is needed to distinguish a valid first level (1) and
// default "no value" protobuf behavior (0)
func (m *Message) GetClusterLevel() int32 {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38 39 40 41 42 43
	level := m.GetClusterLevelRaw() - 1
	if level < 0 {
		u.PErr("handleGetValue: no routing level specified, assuming 0\n")
		level = 0
	}
	return level
44
}
45

46 47 48 49 50
// SetClusterLevel adjusts and sets the cluster level on the message.
// a +/- 1 adjustment is needed to distinguish a valid first level (1) and
// default "no value" protobuf behavior (0)
func (m *Message) SetClusterLevel(level int32) {
	m.ClusterLevelRaw = &level
51
}