Refactor node ID handling, use check digits (fixes #269)

New node ID:s contain four Luhn check digits and are grouped
differently. Code uses NodeID type instead of string, so it's formatted
homogenously everywhere.
This commit is contained in:
Jakob Borg
2014-06-30 01:42:03 +02:00
parent fee8289c0a
commit 8f3effed32
35 changed files with 563 additions and 478 deletions

View File

@@ -5,27 +5,32 @@
// Package cid provides a manager for mappings between node ID:s and connection ID:s.
package cid
import "sync"
import (
"sync"
"github.com/calmh/syncthing/protocol"
)
type Map struct {
sync.Mutex
toCid map[string]uint
toName []string
toCid map[protocol.NodeID]uint
toName []protocol.NodeID
}
var (
LocalName = "<local>"
LocalID uint = 0
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[string]uint{"<local>": 0},
toName: []string{"<local>"},
toCid: map[protocol.NodeID]uint{LocalNodeID: LocalID},
toName: []protocol.NodeID{LocalNodeID},
}
}
func (m *Map) Get(name string) uint {
func (m *Map) Get(name protocol.NodeID) uint {
m.Lock()
defer m.Unlock()
@@ -36,7 +41,7 @@ func (m *Map) Get(name string) uint {
// Find a free slot to get a new ID
for i, n := range m.toName {
if n == "" {
if n == emptyNodeID {
m.toName[i] = name
m.toCid[name] = uint(i)
return uint(i)
@@ -50,19 +55,19 @@ func (m *Map) Get(name string) uint {
return cid
}
func (m *Map) Name(cid uint) string {
func (m *Map) Name(cid uint) protocol.NodeID {
m.Lock()
defer m.Unlock()
return m.toName[cid]
}
func (m *Map) Names() []string {
func (m *Map) Names() []protocol.NodeID {
m.Lock()
var names []string
var names []protocol.NodeID
for _, name := range m.toName {
if name != "" {
if name != emptyNodeID {
names = append(names, name)
}
}
@@ -71,11 +76,11 @@ func (m *Map) Names() []string {
return names
}
func (m *Map) Clear(name string) {
func (m *Map) Clear(name protocol.NodeID) {
m.Lock()
cid, ok := m.toCid[name]
if ok {
m.toName[cid] = ""
m.toName[cid] = emptyNodeID
delete(m.toCid, name)
}
m.Unlock()

View File

@@ -4,28 +4,35 @@
package cid
import "testing"
import (
"testing"
"github.com/calmh/syncthing/protocol"
)
func TestGet(t *testing.T) {
m := NewMap()
if i := m.Get("foo"); i != 1 {
fooID := protocol.NewNodeID([]byte("foo"))
barID := protocol.NewNodeID([]byte("bar"))
if i := m.Get(fooID); i != 1 {
t.Errorf("Unexpected id %d != 1", i)
}
if i := m.Get("bar"); i != 2 {
if i := m.Get(barID); i != 2 {
t.Errorf("Unexpected id %d != 2", i)
}
if i := m.Get("foo"); i != 1 {
if i := m.Get(fooID); i != 1 {
t.Errorf("Unexpected id %d != 1", i)
}
if i := m.Get("bar"); i != 2 {
if i := m.Get(barID); i != 2 {
t.Errorf("Unexpected id %d != 2", i)
}
if LocalID != 0 {
t.Error("LocalID should be 0")
}
if i := m.Get(LocalName); i != LocalID {
t.Errorf("Unexpected id %d != %c", i, LocalID)
if i := m.Get(LocalNodeID); i != LocalID {
t.Errorf("Unexpected id %d != %d", i, LocalID)
}
}