diff options
author | Jakob Borg <jakob@nym.se> | 2014-06-30 01:42:03 +0200 |
---|---|---|
committer | Jakob Borg <jakob@nym.se> | 2014-06-30 01:42:03 +0200 |
commit | 8f3effed32bce654110273a78759d659cccc86c2 (patch) | |
tree | ef15f8c46b69a7618a1976a48194d7e61c82c5a9 /cid | |
parent | fee8289c0ac62b88fa77423446faa882f7132796 (diff) |
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.
Diffstat (limited to 'cid')
-rw-r--r-- | cid/cid.go | 35 | ||||
-rw-r--r-- | cid/cid_test.go | 21 |
2 files changed, 34 insertions, 22 deletions
diff --git a/cid/cid.go b/cid/cid.go index a47a1240a5..22642b97c6 100644 --- a/cid/cid.go +++ b/cid/cid.go @@ -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() diff --git a/cid/cid_test.go b/cid/cid_test.go index 81a26dccdb..9be7e1662b 100644 --- a/cid/cid_test.go +++ b/cid/cid_test.go @@ -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) } } |