Skip to content

Commit f040988

Browse files
committed
Redesign auto accept
- Allow to change auto accept during runtime - Update the mDNS Entry when changing the auto accept value - Provide the proper register value of remote services - A few renamings
1 parent 26a90b4 commit f040988

File tree

11 files changed

+93
-48
lines changed

11 files changed

+93
-48
lines changed

api/hub.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ type HubInterface interface {
1919
// Provide the current pairing state for a SKI
2020
PairingDetailForSki(ski string) *ConnectionStateDetail
2121

22+
// Enables or disables to automatically accept incoming pairing and connection requests
23+
//
24+
// Default: false
25+
SetAutoAccept(bool)
26+
2227
// Pair with the SKI
2328
RegisterRemoteSKI(ski string)
2429

api/servicedetails.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ type ServiceDetails struct {
2626
// The EEBUS device type of the device model
2727
deviceType string
2828

29-
// Flags if the service auto auto accepts other services
30-
registerAutoAccept bool
29+
// Flags if the service auto accepts other services
30+
autoAccept bool
3131

3232
// Flags if the service is trusted and should be reconnected to
3333
// Should be enabled after the connection process resulted
@@ -100,18 +100,18 @@ func (s *ServiceDetails) SetDeviceType(deviceType string) {
100100
s.deviceType = deviceType
101101
}
102102

103-
func (s *ServiceDetails) RegisterAutoAccept() bool {
103+
func (s *ServiceDetails) AutoAccept() bool {
104104
s.mux.Lock()
105105
defer s.mux.Unlock()
106106

107-
return s.registerAutoAccept
107+
return s.autoAccept
108108
}
109109

110-
func (s *ServiceDetails) SetRegisterAutoAccept(value bool) {
110+
func (s *ServiceDetails) SetAutoAccept(value bool) {
111111
s.mux.Lock()
112112
defer s.mux.Unlock()
113113

114-
s.registerAutoAccept = value
114+
s.autoAccept = value
115115
}
116116

117117
func (s *ServiceDetails) Trusted() bool {

api/servicedetails_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func (s *ServiceDetailsSuite) Test_ServiceDetails() {
3939
details.SetDeviceType("devicetype")
4040
assert.Equal(s.T(), "devicetype", details.DeviceType())
4141

42-
details.SetRegisterAutoAccept(true)
43-
assert.Equal(s.T(), true, details.RegisterAutoAccept())
42+
details.SetAutoAccept(true)
43+
assert.Equal(s.T(), true, details.AutoAccept())
4444

4545
details.SetTrusted(true)
4646
assert.Equal(s.T(), true, details.Trusted())

hub/hub.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ type Hub struct {
4040

4141
hubReader api.HubReaderInterface
4242

43+
autoaccept bool
44+
4345
// The list of known remote services
4446
remoteServices map[string]*api.ServiceDetails
4547

hub/hub_mdns.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ func (h *Hub) ReportMdnsEntries(entries map[string]*api.MdnsEntry) {
3232
continue
3333
}
3434

35+
service.SetAutoAccept(entry.Register)
36+
3537
// patch the addresses list if an IPv4 address was provided
3638
if service.IPv4() != "" {
3739
if ip := net.ParseIP(service.IPv4()); ip != nil {

hub/hub_pairing.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,23 @@ func (h *Hub) mapShipMessageExchangeState(state model.ShipMessageExchangeState,
6363
return connState
6464
}
6565

66+
func (h *Hub) SetAutoAccept(autoaccept bool) {
67+
h.muxReg.Lock()
68+
defer h.muxReg.Unlock()
69+
70+
h.autoaccept = autoaccept
71+
72+
h.mdns.SetAutoAccept(autoaccept)
73+
}
74+
75+
// check if auto accept is true
76+
func (h *Hub) IsAutoAcceptEnabled() bool {
77+
h.muxReg.Lock()
78+
defer h.muxReg.Unlock()
79+
80+
return h.autoaccept
81+
}
82+
6683
func (h *Hub) checkHasStarted() bool {
6784
h.muxStarted.Lock()
6885
defer h.muxStarted.Unlock()

hub/hub_shipconnection.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ func (h *Hub) IsRemoteServiceForSKIPaired(ski string) bool {
1717
return service.Trusted()
1818
}
1919

20-
// check if auto accept is true
21-
func (h *Hub) IsAutoAcceptEnabled() bool {
22-
return h.localService.RegisterAutoAccept()
23-
}
24-
2520
// report closing of a connection and if handshake did complete
2621
func (h *Hub) HandleConnectionClosed(connection api.ShipConnectionInterface, handshakeCompleted bool) {
2722
remoteSki := connection.RemoteSKI()

hub/hub_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ func (s *HubSuite) Test_NewConnectionsHub() {
128128
hub.Shutdown()
129129
}
130130

131+
func (s *HubSuite) Test_AutoAccept() {
132+
s.sut.SetAutoAccept(true)
133+
value := s.sut.IsAutoAcceptEnabled()
134+
assert.True(s.T(), value)
135+
136+
s.sut.SetAutoAccept(false)
137+
value = s.sut.IsAutoAcceptEnabled()
138+
assert.False(s.T(), value)
139+
}
140+
131141
func (s *HubSuite) Test_SetupRemoteDevice() {
132142
ski := "12af9e"
133143
localService := api.NewServiceDetails(ski)

mdns/mdns.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func (m *MdnsManager) Shutdown() {
191191
// A CEM service should always invoke this on startup
192192
// Any other service should only invoke this whenever it is not connected to a CEM service
193193
func (m *MdnsManager) AnnounceMdnsEntry() error {
194-
if m.isAnnounced || m.mdnsProvider == nil {
194+
if m.mdnsProvider == nil {
195195
return nil
196196
}
197197

@@ -236,7 +236,16 @@ func (m *MdnsManager) UnannounceMdnsEntry() {
236236

237237
func (m *MdnsManager) SetAutoAccept(accept bool) {
238238
m.autoaccept = accept
239-
// TODO: if changed, also change the mDNS registry
239+
240+
// if announcement is off, don't enforce a new announcement
241+
if !m.isAnnounced {
242+
return
243+
}
244+
245+
// Update the announcement as autoaccept changed
246+
if err := m.AnnounceMdnsEntry(); err != nil {
247+
logging.Log().Debug("mdns: changing mdns entry failed", err)
248+
}
240249
}
241250

242251
func (m *MdnsManager) mdnsEntries() map[string]*api.MdnsEntry {

mdns/mdns_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func (s *MdnsSuite) BeforeTest(suiteName, testName string) {
3131
s.mdnsService = mocks.NewMdnsInterface(s.T())
3232

3333
s.mdnsSearch = mocks.NewMdnsReportInterface(s.T())
34+
s.mdnsSearch.On("ReportMdnsEntries", mock.Anything).Maybe().Return()
3435

3536
s.mdnsProvider = mocks.NewMdnsProviderInterface(s.T())
3637
s.mdnsProvider.On("ResolveEntries", mock.Anything, mock.Anything).Maybe().Return()
@@ -63,6 +64,10 @@ func (s *MdnsSuite) Test_GoZeroConfOnly() {
6364

6465
err := s.sut.Start(s.mdnsSearch)
6566
assert.Nil(s.T(), err)
67+
assert.False(s.T(), s.sut.autoaccept)
68+
69+
s.sut.SetAutoAccept(true)
70+
assert.True(s.T(), s.sut.autoaccept)
6671
}
6772

6873
func (s *MdnsSuite) Test_Start() {

0 commit comments

Comments
 (0)