summaryrefslogtreecommitdiffstats
path: root/cid/cid.go
blob: 22642b97c61d6d735f279003f1414f6e0e16f4ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (C) 2014 Jakob Borg and other contributors. All rights reserved.
// Use of this source code is governed by an MIT-style license that can be
// found in the LICENSE file.

// Package cid provides a manager for mappings between node ID:s and connection ID:s.
package cid

import (
	"sync"

	"github.com/calmh/syncthing/protocol"
)

type Map struct {
	sync.Mutex
	toCid  map[protocol.NodeID]uint
	toName []protocol.NodeID
}

var (
	LocalNodeID      = protocol.NodeID{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
	LocalID     uint = 0
	emptyNodeID protocol.NodeID
)

func NewMap() *Map {
	return &Map{
		toCid:  map[protocol.NodeID]uint{LocalNodeID: LocalID},
		toName: []protocol.NodeID{LocalNodeID},
	}
}

func (m *Map) Get(name protocol.NodeID) uint {
	m.Lock()
	defer m.Unlock()

	cid, ok := m.toCid[name]
	if ok {
		return cid
	}

	// Find a free slot to get a new ID
	for i, n := range m.toName {
		if n == emptyNodeID {
			m.toName[i] = name
			m.toCid[name] = uint(i)
			return uint(i)
		}
	}

	// Add it to the end since we didn't find a free slot
	m.toName = append(m.toName, name)
	cid = uint(len(m.toName) - 1)
	m.toCid[name] = cid
	return cid
}

func (m *Map) Name(cid uint) protocol.NodeID {
	m.Lock()
	defer m.Unlock()

	return m.toName[cid]
}

func (m *Map) Names() []protocol.NodeID {
	m.Lock()

	var names []protocol.NodeID
	for _, name := range m.toName {
		if name != emptyNodeID {
			names = append(names, name)
		}
	}

	m.Unlock()
	return names
}

func (m *Map) Clear(name protocol.NodeID) {
	m.Lock()
	cid, ok := m.toCid[name]
	if ok {
		m.toName[cid] = emptyNodeID
		delete(m.toCid, name)
	}
	m.Unlock()
}