bucket.go 2.06 KB
Newer Older
1
package kbucket
2 3 4

import (
	"container/list"
5
	"sync"
6

7
	"github.com/libp2p/go-libp2p-core/peer"
8
)
Jeromy's avatar
Jeromy committed
9

10
// Bucket holds a list of peers.
11 12 13 14 15
type Bucket struct {
	lk   sync.RWMutex
	list *list.List
}

16
func newBucket() *Bucket {
17 18 19 20
	b := new(Bucket)
	b.list = list.New()
	return b
}
21

22 23 24 25 26 27 28 29 30 31 32
func (b *Bucket) Peers() []peer.ID {
	b.lk.RLock()
	defer b.lk.RUnlock()
	ps := make([]peer.ID, 0, b.list.Len())
	for e := b.list.Front(); e != nil; e = e.Next() {
		id := e.Value.(peer.ID)
		ps = append(ps, id)
	}
	return ps
}

33
func (b *Bucket) Has(id peer.ID) bool {
34 35 36
	b.lk.RLock()
	defer b.lk.RUnlock()
	for e := b.list.Front(); e != nil; e = e.Next() {
37
		if e.Value.(peer.ID) == id {
38
			return true
39 40
		}
	}
41
	return false
42 43
}

44
func (b *Bucket) Remove(id peer.ID) bool {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
45 46
	b.lk.Lock()
	defer b.lk.Unlock()
47 48 49
	for e := b.list.Front(); e != nil; e = e.Next() {
		if e.Value.(peer.ID) == id {
			b.list.Remove(e)
50
			return true
51 52
		}
	}
53
	return false
54 55
}

56
func (b *Bucket) MoveToFront(id peer.ID) {
57
	b.lk.Lock()
58 59 60 61 62 63
	defer b.lk.Unlock()
	for e := b.list.Front(); e != nil; e = e.Next() {
		if e.Value.(peer.ID) == id {
			b.list.MoveToFront(e)
		}
	}
64 65
}

66
func (b *Bucket) PushFront(p peer.ID) {
67 68 69
	b.lk.Lock()
	b.list.PushFront(p)
	b.lk.Unlock()
70 71
}

72
func (b *Bucket) PopBack() peer.ID {
73 74 75 76
	b.lk.Lock()
	defer b.lk.Unlock()
	last := b.list.Back()
	b.list.Remove(last)
77
	return last.Value.(peer.ID)
78 79
}

80
func (b *Bucket) Len() int {
81 82 83
	b.lk.RLock()
	defer b.lk.RUnlock()
	return b.list.Len()
84 85
}

86
// Split splits a buckets peers into two buckets, the methods receiver will have
87 88 89
// peers with CPL equal to cpl, the returned bucket will have peers with CPL
// greater than cpl (returned bucket has closer peers)
func (b *Bucket) Split(cpl int, target ID) *Bucket {
90 91 92
	b.lk.Lock()
	defer b.lk.Unlock()

93
	out := list.New()
94
	newbuck := newBucket()
95 96
	newbuck.list = out
	e := b.list.Front()
97
	for e != nil {
98
		peerID := ConvertPeerID(e.Value.(peer.ID))
Matt Joiner's avatar
Matt Joiner committed
99
		peerCPL := CommonPrefixLen(peerID, target)
100
		if peerCPL > cpl {
101 102 103
			cur := e
			out.PushBack(e.Value)
			e = e.Next()
104
			b.list.Remove(cur)
105 106 107 108
			continue
		}
		e = e.Next()
	}
109
	return newbuck
110
}
Steven Allen's avatar
Steven Allen committed
111 112

//go:generate go run ./generate