peer.go 2.13 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

Siraj Ravel's avatar
Siraj Ravel committed
19
// Equal is utililty function for comparing two peer ID's
20 21
func (id ID) Equal(other ID) bool {
	return bytes.Equal(id, other)
22 23
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
24
// Pretty returns a b58-encoded string of the ID
25
func (id ID) Pretty() string {
26
	return b58.Encode(id)
27 28
}

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

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
32
// Peer represents the identity information of an IPFS Node, including
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
33
// ID, and relevant Addresses.
34
type Peer struct {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
35
	ID        ID
36
	Addresses []*ma.Multiaddr
Jeromy's avatar
Jeromy committed
37

38 39
	PrivKey ic.PrivKey
	PubKey  ic.PubKey
40

41 42 43
	latency time.Duration

	sync.RWMutex
44 45
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
46
// Key returns the ID as a Key (string) for maps.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
47
func (p *Peer) Key() u.Key {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
48
	return u.Key(p.ID)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
49 50
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
51
// AddAddress adds the given Multiaddr address to Peer's addresses.
52
func (p *Peer) AddAddress(a *ma.Multiaddr) {
53 54 55
	p.Lock()
	defer p.Unlock()

56 57 58 59 60
	for _, addr := range p.Addresses {
		if addr.Equal(a) {
			return
		}
	}
61 62 63
	p.Addresses = append(p.Addresses, a)
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
64
// NetAddress returns the first Multiaddr found for a given network.
65
func (p *Peer) NetAddress(n string) *ma.Multiaddr {
66 67 68
	p.RLock()
	defer p.RUnlock()

69 70 71 72 73 74 75 76 77 78 79 80 81 82
	for _, a := range p.Addresses {
		ps, err := a.Protocols()
		if err != nil {
			continue // invalid addr
		}

		for _, p := range ps {
			if p.Name == n {
				return a
			}
		}
	}
	return nil
}
Jeromy's avatar
Jeromy committed
83

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
84
// GetLatency retrieves the current latency measurement.
85
func (p *Peer) GetLatency() (out time.Duration) {
86
	p.RLock()
87
	out = p.latency
88
	p.RUnlock()
89
	return
Jeromy's avatar
Jeromy committed
90 91
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
92
// SetLatency sets the latency measurement.
93 94
// TODO: Instead of just keeping a single number,
//		 keep a running average over the last hour or so
95
// Yep, should be EWMA or something. (-jbenet)
96
func (p *Peer) SetLatency(laten time.Duration) {
97
	p.Lock()
98
	p.latency = laten
99
	p.Unlock()
Jeromy's avatar
Jeromy committed
100
}