From 1b1d38183dd72d16eea6537252773355502e8bb3 Mon Sep 17 00:00:00 2001 From: Simon Frei Date: Tue, 29 Sep 2020 13:17:38 +0200 Subject: [PATCH] lib: Remove HelloResult type, same as Hello (#7015) --- lib/api/mocked_model_test.go | 4 ++-- lib/connections/structs.go | 4 ++-- lib/model/fakeconns_test.go | 2 +- lib/model/model.go | 8 ++++---- lib/model/model_test.go | 16 ++++++++-------- lib/protocol/hello.go | 30 +++++++++++------------------- 6 files changed, 28 insertions(+), 36 deletions(-) diff --git a/lib/api/mocked_model_test.go b/lib/api/mocked_model_test.go index 553507c4c..735e0049f 100644 --- a/lib/api/mocked_model_test.go +++ b/lib/api/mocked_model_test.go @@ -149,9 +149,9 @@ func (m *mockedModel) DownloadProgress(deviceID protocol.DeviceID, folder string return nil } -func (m *mockedModel) AddConnection(conn connections.Connection, hello protocol.HelloResult) {} +func (m *mockedModel) AddConnection(conn connections.Connection, hello protocol.Hello) {} -func (m *mockedModel) OnHello(protocol.DeviceID, net.Addr, protocol.HelloResult) error { +func (m *mockedModel) OnHello(protocol.DeviceID, net.Addr, protocol.Hello) error { return nil } diff --git a/lib/connections/structs.go b/lib/connections/structs.go index 4bfe7e05e..d270d13bf 100644 --- a/lib/connections/structs.go +++ b/lib/connections/structs.go @@ -202,9 +202,9 @@ type genericListener interface { type Model interface { protocol.Model - AddConnection(conn Connection, hello protocol.HelloResult) + AddConnection(conn Connection, hello protocol.Hello) Connection(remoteID protocol.DeviceID) (Connection, bool) - OnHello(protocol.DeviceID, net.Addr, protocol.HelloResult) error + OnHello(protocol.DeviceID, net.Addr, protocol.Hello) error GetHello(protocol.DeviceID) protocol.HelloIntf } diff --git a/lib/model/fakeconns_test.go b/lib/model/fakeconns_test.go index e0ece9130..455d191e2 100644 --- a/lib/model/fakeconns_test.go +++ b/lib/model/fakeconns_test.go @@ -196,7 +196,7 @@ func (f *fakeConnection) sendIndexUpdate() { func addFakeConn(m *model, dev protocol.DeviceID) *fakeConnection { fc := &fakeConnection{id: dev, model: m} - m.AddConnection(fc, protocol.HelloResult{}) + m.AddConnection(fc, protocol.Hello{}) m.ClusterConfig(dev, protocol.ClusterConfig{ Folders: []protocol.Folder{ diff --git a/lib/model/model.go b/lib/model/model.go index 40ca3c424..9c51a0a10 100644 --- a/lib/model/model.go +++ b/lib/model/model.go @@ -147,7 +147,7 @@ type model struct { conn map[protocol.DeviceID]connections.Connection connRequestLimiters map[protocol.DeviceID]*byteSemaphore closed map[protocol.DeviceID]chan struct{} - helloMessages map[protocol.DeviceID]protocol.HelloResult + helloMessages map[protocol.DeviceID]protocol.Hello deviceDownloads map[protocol.DeviceID]*deviceDownloadState remotePausedFolders map[protocol.DeviceID][]string // deviceID -> folders @@ -219,7 +219,7 @@ func NewModel(cfg config.Wrapper, id protocol.DeviceID, clientName, clientVersio conn: make(map[protocol.DeviceID]connections.Connection), connRequestLimiters: make(map[protocol.DeviceID]*byteSemaphore), closed: make(map[protocol.DeviceID]chan struct{}), - helloMessages: make(map[protocol.DeviceID]protocol.HelloResult), + helloMessages: make(map[protocol.DeviceID]protocol.Hello), deviceDownloads: make(map[protocol.DeviceID]*deviceDownloadState), remotePausedFolders: make(map[protocol.DeviceID][]string), } @@ -1750,7 +1750,7 @@ func (m *model) SetIgnores(folder string, content []string) error { // OnHello is called when an device connects to us. // This allows us to extract some information from the Hello message // and add it to a list of known devices ahead of any checks. -func (m *model) OnHello(remoteID protocol.DeviceID, addr net.Addr, hello protocol.HelloResult) error { +func (m *model) OnHello(remoteID protocol.DeviceID, addr net.Addr, hello protocol.Hello) error { if m.cfg.IgnoredDevice(remoteID) { return errDeviceIgnored } @@ -1799,7 +1799,7 @@ func (m *model) GetHello(id protocol.DeviceID) protocol.HelloIntf { // AddConnection adds a new peer connection to the model. An initial index will // be sent to the connected peer, thereafter index updates whenever the local // folder changes. -func (m *model) AddConnection(conn connections.Connection, hello protocol.HelloResult) { +func (m *model) AddConnection(conn connections.Connection, hello protocol.Hello) { deviceID := conn.ID() device, ok := m.cfg.Device(deviceID) if !ok { diff --git a/lib/model/model_test.go b/lib/model/model_test.go index 885adc0fe..8b6cf18c0 100644 --- a/lib/model/model_test.go +++ b/lib/model/model_test.go @@ -126,7 +126,7 @@ func newState(cfg config.Configuration) *model { m := setupModel(wcfg) for _, dev := range cfg.Devices { - m.AddConnection(&fakeConnection{id: dev.DeviceID, model: m}, protocol.HelloResult{}) + m.AddConnection(&fakeConnection{id: dev.DeviceID, model: m}, protocol.Hello{}) } return m @@ -254,7 +254,7 @@ func BenchmarkRequestOut(b *testing.B) { for _, f := range files { fc.addFile(f.Name, 0644, protocol.FileInfoTypeFile, []byte("some data to return")) } - m.AddConnection(fc, protocol.HelloResult{}) + m.AddConnection(fc, protocol.Hello{}) m.Index(device1, "default", files) b.ResetTimer() @@ -292,7 +292,7 @@ func BenchmarkRequestInSingleFile(b *testing.B) { } func TestDeviceRename(t *testing.T) { - hello := protocol.HelloResult{ + hello := protocol.Hello{ ClientName: "syncthing", ClientVersion: "v0.9.4", } @@ -2313,9 +2313,9 @@ func TestSharedWithClearedOnDisconnect(t *testing.T) { defer cleanupModel(m) conn1 := &fakeConnection{id: device1, model: m} - m.AddConnection(conn1, protocol.HelloResult{}) + m.AddConnection(conn1, protocol.Hello{}) conn2 := &fakeConnection{id: device2, model: m} - m.AddConnection(conn2, protocol.HelloResult{}) + m.AddConnection(conn2, protocol.Hello{}) m.ClusterConfig(device1, protocol.ClusterConfig{ Folders: []protocol.Folder{ @@ -3331,7 +3331,7 @@ func TestConnCloseOnRestart(t *testing.T) { br := &testutils.BlockingRW{} nw := &testutils.NoopRW{} - m.AddConnection(newFakeProtoConn(protocol.NewConnection(device1, br, nw, m, "testConn", protocol.CompressNever)), protocol.HelloResult{}) + m.AddConnection(newFakeProtoConn(protocol.NewConnection(device1, br, nw, m, "testConn", protocol.CompressNever)), protocol.Hello{}) m.pmut.RLock() if len(m.closed) != 1 { t.Fatalf("Expected just one conn (len(m.conn) == %v)", len(m.conn)) @@ -4001,8 +4001,8 @@ func testConfigChangeClosesConnections(t *testing.T, expectFirstClosed, expectSe fc1 := &fakeConnection{id: device1, model: m} fc2 := &fakeConnection{id: device2, model: m} - m.AddConnection(fc1, protocol.HelloResult{}) - m.AddConnection(fc2, protocol.HelloResult{}) + m.AddConnection(fc1, protocol.Hello{}) + m.AddConnection(fc2, protocol.Hello{}) t.Log("Applying config change") diff --git a/lib/protocol/hello.go b/lib/protocol/hello.go index 593a16e4e..a62e20620 100644 --- a/lib/protocol/hello.go +++ b/lib/protocol/hello.go @@ -16,14 +16,6 @@ type HelloIntf interface { Marshal() ([]byte, error) } -// The HelloResult is the non version specific interpretation of the other -// side's Hello message. -type HelloResult struct { - DeviceName string - ClientName string - ClientVersion string -} - var ( // ErrTooOldVersion is returned by ExchangeHello when the other side // speaks an older, incompatible version of the protocol. @@ -33,9 +25,9 @@ var ( ErrUnknownMagic = errors.New("the remote device speaks an unknown (newer?) version of the protocol") ) -func ExchangeHello(c io.ReadWriter, h HelloIntf) (HelloResult, error) { +func ExchangeHello(c io.ReadWriter, h HelloIntf) (Hello, error) { if err := writeHello(c, h); err != nil { - return HelloResult{}, err + return Hello{}, err } return readHello(c) } @@ -51,41 +43,41 @@ func IsVersionMismatch(err error) bool { } } -func readHello(c io.Reader) (HelloResult, error) { +func readHello(c io.Reader) (Hello, error) { header := make([]byte, 4) if _, err := io.ReadFull(c, header); err != nil { - return HelloResult{}, err + return Hello{}, err } switch binary.BigEndian.Uint32(header) { case HelloMessageMagic: // This is a v0.14 Hello message in proto format if _, err := io.ReadFull(c, header[:2]); err != nil { - return HelloResult{}, err + return Hello{}, err } msgSize := binary.BigEndian.Uint16(header[:2]) if msgSize > 32767 { - return HelloResult{}, errors.New("hello message too big") + return Hello{}, errors.New("hello message too big") } buf := make([]byte, msgSize) if _, err := io.ReadFull(c, buf); err != nil { - return HelloResult{}, err + return Hello{}, err } var hello Hello if err := hello.Unmarshal(buf); err != nil { - return HelloResult{}, err + return Hello{}, err } - return HelloResult(hello), nil + return Hello(hello), nil case 0x00010001, 0x00010000, Version13HelloMagic: // This is the first word of an older cluster config message or an // old magic number. (Version 0, message ID 1, message type 0, // compression enabled or disabled) - return HelloResult{}, ErrTooOldVersion + return Hello{}, ErrTooOldVersion } - return HelloResult{}, ErrUnknownMagic + return Hello{}, ErrUnknownMagic } func writeHello(c io.Writer, h HelloIntf) error {