lib: Remove HelloResult type, same as Hello (#7015)

This commit is contained in:
Simon Frei 2020-09-29 13:17:38 +02:00 committed by GitHub
parent fb3281b647
commit 1b1d38183d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 28 additions and 36 deletions

View File

@ -149,9 +149,9 @@ func (m *mockedModel) DownloadProgress(deviceID protocol.DeviceID, folder string
return nil 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 return nil
} }

View File

@ -202,9 +202,9 @@ type genericListener interface {
type Model interface { type Model interface {
protocol.Model protocol.Model
AddConnection(conn Connection, hello protocol.HelloResult) AddConnection(conn Connection, hello protocol.Hello)
Connection(remoteID protocol.DeviceID) (Connection, bool) 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 GetHello(protocol.DeviceID) protocol.HelloIntf
} }

View File

@ -196,7 +196,7 @@ func (f *fakeConnection) sendIndexUpdate() {
func addFakeConn(m *model, dev protocol.DeviceID) *fakeConnection { func addFakeConn(m *model, dev protocol.DeviceID) *fakeConnection {
fc := &fakeConnection{id: dev, model: m} fc := &fakeConnection{id: dev, model: m}
m.AddConnection(fc, protocol.HelloResult{}) m.AddConnection(fc, protocol.Hello{})
m.ClusterConfig(dev, protocol.ClusterConfig{ m.ClusterConfig(dev, protocol.ClusterConfig{
Folders: []protocol.Folder{ Folders: []protocol.Folder{

View File

@ -147,7 +147,7 @@ type model struct {
conn map[protocol.DeviceID]connections.Connection conn map[protocol.DeviceID]connections.Connection
connRequestLimiters map[protocol.DeviceID]*byteSemaphore connRequestLimiters map[protocol.DeviceID]*byteSemaphore
closed map[protocol.DeviceID]chan struct{} closed map[protocol.DeviceID]chan struct{}
helloMessages map[protocol.DeviceID]protocol.HelloResult helloMessages map[protocol.DeviceID]protocol.Hello
deviceDownloads map[protocol.DeviceID]*deviceDownloadState deviceDownloads map[protocol.DeviceID]*deviceDownloadState
remotePausedFolders map[protocol.DeviceID][]string // deviceID -> folders 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), conn: make(map[protocol.DeviceID]connections.Connection),
connRequestLimiters: make(map[protocol.DeviceID]*byteSemaphore), connRequestLimiters: make(map[protocol.DeviceID]*byteSemaphore),
closed: make(map[protocol.DeviceID]chan struct{}), 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), deviceDownloads: make(map[protocol.DeviceID]*deviceDownloadState),
remotePausedFolders: make(map[protocol.DeviceID][]string), 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. // OnHello is called when an device connects to us.
// This allows us to extract some information from the Hello message // This allows us to extract some information from the Hello message
// and add it to a list of known devices ahead of any checks. // 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) { if m.cfg.IgnoredDevice(remoteID) {
return errDeviceIgnored 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 // 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 // be sent to the connected peer, thereafter index updates whenever the local
// folder changes. // 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() deviceID := conn.ID()
device, ok := m.cfg.Device(deviceID) device, ok := m.cfg.Device(deviceID)
if !ok { if !ok {

View File

@ -126,7 +126,7 @@ func newState(cfg config.Configuration) *model {
m := setupModel(wcfg) m := setupModel(wcfg)
for _, dev := range cfg.Devices { 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 return m
@ -254,7 +254,7 @@ func BenchmarkRequestOut(b *testing.B) {
for _, f := range files { for _, f := range files {
fc.addFile(f.Name, 0644, protocol.FileInfoTypeFile, []byte("some data to return")) 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) m.Index(device1, "default", files)
b.ResetTimer() b.ResetTimer()
@ -292,7 +292,7 @@ func BenchmarkRequestInSingleFile(b *testing.B) {
} }
func TestDeviceRename(t *testing.T) { func TestDeviceRename(t *testing.T) {
hello := protocol.HelloResult{ hello := protocol.Hello{
ClientName: "syncthing", ClientName: "syncthing",
ClientVersion: "v0.9.4", ClientVersion: "v0.9.4",
} }
@ -2313,9 +2313,9 @@ func TestSharedWithClearedOnDisconnect(t *testing.T) {
defer cleanupModel(m) defer cleanupModel(m)
conn1 := &fakeConnection{id: device1, model: m} conn1 := &fakeConnection{id: device1, model: m}
m.AddConnection(conn1, protocol.HelloResult{}) m.AddConnection(conn1, protocol.Hello{})
conn2 := &fakeConnection{id: device2, model: m} conn2 := &fakeConnection{id: device2, model: m}
m.AddConnection(conn2, protocol.HelloResult{}) m.AddConnection(conn2, protocol.Hello{})
m.ClusterConfig(device1, protocol.ClusterConfig{ m.ClusterConfig(device1, protocol.ClusterConfig{
Folders: []protocol.Folder{ Folders: []protocol.Folder{
@ -3331,7 +3331,7 @@ func TestConnCloseOnRestart(t *testing.T) {
br := &testutils.BlockingRW{} br := &testutils.BlockingRW{}
nw := &testutils.NoopRW{} 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() m.pmut.RLock()
if len(m.closed) != 1 { if len(m.closed) != 1 {
t.Fatalf("Expected just one conn (len(m.conn) == %v)", len(m.conn)) 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} fc1 := &fakeConnection{id: device1, model: m}
fc2 := &fakeConnection{id: device2, model: m} fc2 := &fakeConnection{id: device2, model: m}
m.AddConnection(fc1, protocol.HelloResult{}) m.AddConnection(fc1, protocol.Hello{})
m.AddConnection(fc2, protocol.HelloResult{}) m.AddConnection(fc2, protocol.Hello{})
t.Log("Applying config change") t.Log("Applying config change")

View File

@ -16,14 +16,6 @@ type HelloIntf interface {
Marshal() ([]byte, error) 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 ( var (
// ErrTooOldVersion is returned by ExchangeHello when the other side // ErrTooOldVersion is returned by ExchangeHello when the other side
// speaks an older, incompatible version of the protocol. // 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") 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 { if err := writeHello(c, h); err != nil {
return HelloResult{}, err return Hello{}, err
} }
return readHello(c) 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) header := make([]byte, 4)
if _, err := io.ReadFull(c, header); err != nil { if _, err := io.ReadFull(c, header); err != nil {
return HelloResult{}, err return Hello{}, err
} }
switch binary.BigEndian.Uint32(header) { switch binary.BigEndian.Uint32(header) {
case HelloMessageMagic: case HelloMessageMagic:
// This is a v0.14 Hello message in proto format // This is a v0.14 Hello message in proto format
if _, err := io.ReadFull(c, header[:2]); err != nil { if _, err := io.ReadFull(c, header[:2]); err != nil {
return HelloResult{}, err return Hello{}, err
} }
msgSize := binary.BigEndian.Uint16(header[:2]) msgSize := binary.BigEndian.Uint16(header[:2])
if msgSize > 32767 { if msgSize > 32767 {
return HelloResult{}, errors.New("hello message too big") return Hello{}, errors.New("hello message too big")
} }
buf := make([]byte, msgSize) buf := make([]byte, msgSize)
if _, err := io.ReadFull(c, buf); err != nil { if _, err := io.ReadFull(c, buf); err != nil {
return HelloResult{}, err return Hello{}, err
} }
var hello Hello var hello Hello
if err := hello.Unmarshal(buf); err != nil { 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: case 0x00010001, 0x00010000, Version13HelloMagic:
// This is the first word of an older cluster config message or an // This is the first word of an older cluster config message or an
// old magic number. (Version 0, message ID 1, message type 0, // old magic number. (Version 0, message ID 1, message type 0,
// compression enabled or disabled) // compression enabled or disabled)
return HelloResult{}, ErrTooOldVersion return Hello{}, ErrTooOldVersion
} }
return HelloResult{}, ErrUnknownMagic return Hello{}, ErrUnknownMagic
} }
func writeHello(c io.Writer, h HelloIntf) error { func writeHello(c io.Writer, h HelloIntf) error {