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

Jeromy's avatar
Jeromy committed
41
	latency   time.Duration
42
	latenLock sync.RWMutex
43 44
}

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

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
50
// AddAddress adds the given Multiaddr address to Peer's addresses.
51
func (p *Peer) AddAddress(a *ma.Multiaddr) {
52 53 54 55 56
	for _, addr := range p.Addresses {
		if addr.Equal(a) {
			return
		}
	}
57 58 59
	p.Addresses = append(p.Addresses, a)
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
60
// NetAddress returns the first Multiaddr found for a given network.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
func (p *Peer) NetAddress(n string) *ma.Multiaddr {
	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
76

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
77
// GetLatency retrieves the current latency measurement.
78 79 80 81 82
func (p *Peer) GetLatency() (out time.Duration) {
	p.latenLock.RLock()
	out = p.latency
	p.latenLock.RUnlock()
	return
Jeromy's avatar
Jeromy committed
83 84
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
85
// SetLatency sets the latency measurement.
86 87
// TODO: Instead of just keeping a single number,
//		 keep a running average over the last hour or so
88 89 90 91
func (p *Peer) SetLatency(laten time.Duration) {
	p.latenLock.Lock()
	p.latency = laten
	p.latenLock.Unlock()
Jeromy's avatar
Jeromy committed
92
}