peer.go 2.39 KB
Newer Older
1 2 3
package peer

import (
Jeromy's avatar
Jeromy committed
4
	"sync"
Jeromy's avatar
Jeromy committed
5
	"time"
6

7 8 9
	b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
	mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
10
	ic "github.com/jbenet/go-ipfs/crypto"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11
	u "github.com/jbenet/go-ipfs/util"
12 13

	"bytes"
14 15
)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
16 17
// ID is a byte slice representing the identity of a peer.
type ID mh.Multihash
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
18

19 20 21 22 23
// String is utililty function for printing out peer ID strings.
func (id ID) String() string {
	return id.Pretty()
}

Siraj Ravel's avatar
Siraj Ravel committed
24
// Equal is utililty function for comparing two peer ID's
25 26
func (id ID) Equal(other ID) bool {
	return bytes.Equal(id, other)
27 28
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
29
// Pretty returns a b58-encoded string of the ID
30
func (id ID) Pretty() string {
31
	return b58.Encode(id)
32 33
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
34 35 36 37 38
// DecodePrettyID returns a b58-encoded string of the ID
func DecodePrettyID(s string) ID {
	return b58.Decode(s)
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
39 40
// Map maps Key (string) : *Peer (slices are not comparable).
type Map map[u.Key]*Peer
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
42
// Peer represents the identity information of an IPFS Node, including
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
43
// ID, and relevant Addresses.
44
type Peer struct {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
45
	ID        ID
46
	Addresses []ma.Multiaddr
Jeromy's avatar
Jeromy committed
47

48 49
	PrivKey ic.PrivKey
	PubKey  ic.PubKey
50

51 52 53
	latency time.Duration

	sync.RWMutex
54 55
}

56 57
// String prints out the peer.
func (p *Peer) String() string {
58
	return "[Peer " + p.ID.String() + "]"
59 60
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
61
// Key returns the ID as a Key (string) for maps.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
62
func (p *Peer) Key() u.Key {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
63
	return u.Key(p.ID)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
64 65
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
66
// AddAddress adds the given Multiaddr address to Peer's addresses.
67
func (p *Peer) AddAddress(a ma.Multiaddr) {
68 69 70
	p.Lock()
	defer p.Unlock()

71 72 73 74 75
	for _, addr := range p.Addresses {
		if addr.Equal(a) {
			return
		}
	}
76 77 78
	p.Addresses = append(p.Addresses, a)
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
79
// NetAddress returns the first Multiaddr found for a given network.
80
func (p *Peer) NetAddress(n string) ma.Multiaddr {
81 82 83
	p.RLock()
	defer p.RUnlock()

84
	for _, a := range p.Addresses {
85
		for _, p := range a.Protocols() {
86 87 88 89 90 91 92
			if p.Name == n {
				return a
			}
		}
	}
	return nil
}
Jeromy's avatar
Jeromy committed
93

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
94
// GetLatency retrieves the current latency measurement.
95
func (p *Peer) GetLatency() (out time.Duration) {
96
	p.RLock()
97
	out = p.latency
98
	p.RUnlock()
99
	return
Jeromy's avatar
Jeromy committed
100 101
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
102
// SetLatency sets the latency measurement.
103 104
// TODO: Instead of just keeping a single number,
//		 keep a running average over the last hour or so
105
// Yep, should be EWMA or something. (-jbenet)
106
func (p *Peer) SetLatency(laten time.Duration) {
107
	p.Lock()
108
	p.latency = laten
109
	p.Unlock()
Jeromy's avatar
Jeromy committed
110
}