2014-06-01 22:50:14 +02:00
|
|
|
// 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.
|
|
|
|
|
2014-03-12 06:32:26 +01:00
|
|
|
// Package cid provides a manager for mappings between node ID:s and connection ID:s.
|
2014-03-01 11:11:37 +01:00
|
|
|
package cid
|
|
|
|
|
2014-06-30 01:42:03 +02:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/calmh/syncthing/protocol"
|
|
|
|
)
|
2014-03-28 14:36:57 +01:00
|
|
|
|
2014-03-01 11:11:37 +01:00
|
|
|
type Map struct {
|
2014-03-28 14:36:57 +01:00
|
|
|
sync.Mutex
|
2014-06-30 01:42:03 +02:00
|
|
|
toCid map[protocol.NodeID]uint
|
|
|
|
toName []protocol.NodeID
|
2014-03-01 11:11:37 +01:00
|
|
|
}
|
|
|
|
|
2014-03-28 14:36:57 +01:00
|
|
|
var (
|
2014-06-30 01:42:03 +02:00
|
|
|
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
|
2014-03-28 14:36:57 +01:00
|
|
|
)
|
|
|
|
|
2014-03-01 11:11:37 +01:00
|
|
|
func NewMap() *Map {
|
|
|
|
return &Map{
|
2014-06-30 01:42:03 +02:00
|
|
|
toCid: map[protocol.NodeID]uint{LocalNodeID: LocalID},
|
|
|
|
toName: []protocol.NodeID{LocalNodeID},
|
2014-03-01 11:11:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-30 01:42:03 +02:00
|
|
|
func (m *Map) Get(name protocol.NodeID) uint {
|
2014-03-28 14:36:57 +01:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2014-03-01 11:11:37 +01: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 {
|
2014-06-30 01:42:03 +02:00
|
|
|
if n == emptyNodeID {
|
2014-03-01 11:11:37 +01:00
|
|
|
m.toName[i] = name
|
2014-03-28 14:36:57 +01:00
|
|
|
m.toCid[name] = uint(i)
|
|
|
|
return uint(i)
|
2014-03-01 11:11:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add it to the end since we didn't find a free slot
|
|
|
|
m.toName = append(m.toName, name)
|
2014-03-28 14:36:57 +01:00
|
|
|
cid = uint(len(m.toName) - 1)
|
2014-03-01 11:11:37 +01:00
|
|
|
m.toCid[name] = cid
|
|
|
|
return cid
|
|
|
|
}
|
|
|
|
|
2014-06-30 01:42:03 +02:00
|
|
|
func (m *Map) Name(cid uint) protocol.NodeID {
|
2014-03-28 14:36:57 +01:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
return m.toName[cid]
|
|
|
|
}
|
|
|
|
|
2014-06-30 01:42:03 +02:00
|
|
|
func (m *Map) Names() []protocol.NodeID {
|
2014-03-28 14:36:57 +01:00
|
|
|
m.Lock()
|
|
|
|
|
2014-06-30 01:42:03 +02:00
|
|
|
var names []protocol.NodeID
|
2014-03-28 14:36:57 +01:00
|
|
|
for _, name := range m.toName {
|
2014-06-30 01:42:03 +02:00
|
|
|
if name != emptyNodeID {
|
2014-03-28 14:36:57 +01:00
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Unlock()
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
2014-06-30 01:42:03 +02:00
|
|
|
func (m *Map) Clear(name protocol.NodeID) {
|
2014-03-28 14:36:57 +01:00
|
|
|
m.Lock()
|
2014-03-01 11:11:37 +01:00
|
|
|
cid, ok := m.toCid[name]
|
|
|
|
if ok {
|
2014-06-30 01:42:03 +02:00
|
|
|
m.toName[cid] = emptyNodeID
|
2014-03-01 11:11:37 +01:00
|
|
|
delete(m.toCid, name)
|
|
|
|
}
|
2014-03-28 14:36:57 +01:00
|
|
|
m.Unlock()
|
2014-03-01 11:11:37 +01:00
|
|
|
}
|