syncthing/cid/cid.go

79 lines
1.2 KiB
Go
Raw Normal View History

2014-03-11 22:32:26 -07:00
// Package cid provides a manager for mappings between node ID:s and connection ID:s.
2014-03-01 03:11:37 -07:00
package cid
import "sync"
2014-03-01 03:11:37 -07:00
type Map struct {
sync.Mutex
toCid map[string]uint
2014-03-01 03:11:37 -07:00
toName []string
}
var (
LocalName = "<local>"
LocalID uint = 0
)
2014-03-01 03:11:37 -07:00
func NewMap() *Map {
return &Map{
toCid: map[string]uint{"<local>": 0},
toName: []string{"<local>"},
2014-03-01 03:11:37 -07:00
}
}
func (m *Map) Get(name string) uint {
m.Lock()
defer m.Unlock()
2014-03-01 03:11:37 -07:00
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 == "" {
m.toName[i] = name
m.toCid[name] = uint(i)
return uint(i)
2014-03-01 03:11:37 -07:00
}
}
// 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)
2014-03-01 03:11:37 -07:00
m.toCid[name] = cid
return cid
}
func (m *Map) Name(cid uint) string {
m.Lock()
defer m.Unlock()
return m.toName[cid]
}
func (m *Map) Names() []string {
m.Lock()
var names []string
for _, name := range m.toName {
if name != "" {
names = append(names, name)
}
}
m.Unlock()
return names
}
2014-03-01 03:11:37 -07:00
func (m *Map) Clear(name string) {
m.Lock()
2014-03-01 03:11:37 -07:00
cid, ok := m.toCid[name]
if ok {
m.toName[cid] = ""
delete(m.toCid, name)
}
m.Unlock()
2014-03-01 03:11:37 -07:00
}