Skip to content

Commit 0147396

Browse files
committed
Refactor mDNS to use an interface
1 parent a4cd1f4 commit 0147396

File tree

4 files changed

+43
-19
lines changed

4 files changed

+43
-19
lines changed

service/hub.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ type connectionsHub struct {
7676
httpServer *http.Server
7777

7878
// Handling mDNS related tasks
79-
mdns *mdns
79+
mdns MdnsService
8080

8181
// the SPINE local device
8282
spineLocalDevice *spine.DeviceLocalImpl
@@ -87,7 +87,7 @@ type connectionsHub struct {
8787
muxMdns sync.Mutex
8888
}
8989

90-
func newConnectionsHub(serviceProvider serviceProvider, spineLocalDevice *spine.DeviceLocalImpl, configuration *Configuration, localService *ServiceDetails) *connectionsHub {
90+
func newConnectionsHub(serviceProvider serviceProvider, mdns MdnsService, spineLocalDevice *spine.DeviceLocalImpl, configuration *Configuration, localService *ServiceDetails) *connectionsHub {
9191
hub := &connectionsHub{
9292
connections: make(map[string]*ship.ShipConnection),
9393
connectionAttemptCounter: make(map[string]int),
@@ -97,7 +97,7 @@ func newConnectionsHub(serviceProvider serviceProvider, spineLocalDevice *spine.
9797
spineLocalDevice: spineLocalDevice,
9898
configuration: configuration,
9999
localService: localService,
100-
mdns: newMDNS(localService.SKI(), configuration),
100+
mdns: mdns,
101101
}
102102

103103
return hub
@@ -106,7 +106,7 @@ func newConnectionsHub(serviceProvider serviceProvider, spineLocalDevice *spine.
106106
// start the ConnectionsHub with all its services
107107
func (h *connectionsHub) start() {
108108
// start mDNS
109-
err := h.mdns.Setup()
109+
err := h.mdns.SetupMdnsService()
110110
if err != nil {
111111
logging.Log.Error("error during mdns setup:", err)
112112
}
@@ -118,7 +118,7 @@ func (h *connectionsHub) start() {
118118
}
119119
}()
120120

121-
if err := h.mdns.Announce(); err != nil {
121+
if err := h.mdns.AnnounceMdnsEntry(); err != nil {
122122
logging.Log.Error("error registering mDNS Service:", err)
123123
}
124124
}
@@ -168,7 +168,7 @@ func (h *connectionsHub) checkRestartMdnsSearch() {
168168
if countPairedServices > countConnections {
169169
// if this is not a CEM also start the mDNS announcement
170170
if h.localService.deviceType != model.DeviceTypeTypeEnergyManagementSystem {
171-
_ = h.mdns.Announce()
171+
_ = h.mdns.AnnounceMdnsEntry()
172172
}
173173

174174
logging.Log.Debug("restarting mdns search")
@@ -206,7 +206,7 @@ func (h *connectionsHub) registerConnection(connection *ship.ShipConnection) {
206206

207207
// shutdown mDNS if this is not a CEM
208208
if h.localService.deviceType != model.DeviceTypeTypeEnergyManagementSystem {
209-
h.mdns.Unannounce()
209+
h.mdns.UnannounceMdnsEntry()
210210
h.mdns.UnregisterMdnsSearch(h)
211211
}
212212
}
@@ -228,7 +228,7 @@ func (h *connectionsHub) shutdown() {
228228
h.muxCon.Lock()
229229
defer h.muxCon.Unlock()
230230

231-
h.mdns.shutdown()
231+
h.mdns.ShutdownMdnsService()
232232
for _, c := range h.connections {
233233
c.CloseConnection(false, "")
234234
}

service/hub_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"time"
66

77
"github.com/enbility/eebus-go/ship"
8-
"github.com/enbility/eebus-go/spine/model"
98
"github.com/stretchr/testify/assert"
109
"github.com/stretchr/testify/suite"
1110
)
@@ -48,13 +47,22 @@ func (s *HubSuite) RemoteSKIConnected(string) {}
4847
func (s *HubSuite) RemoteSKIDisconnected(string) {}
4948
func (s *HubSuite) ReportServiceShipID(string, string) {}
5049

50+
var _ MdnsService = (*HubSuite)(nil)
51+
52+
func (s *HubSuite) SetupMdnsService() error { return nil }
53+
func (s *HubSuite) ShutdownMdnsService() {}
54+
func (s *HubSuite) AnnounceMdnsEntry() error { return nil }
55+
func (s *HubSuite) UnannounceMdnsEntry() {}
56+
func (s *HubSuite) RegisterMdnsSearch(cb MdnsSearch) {}
57+
func (s *HubSuite) UnregisterMdnsSearch(cb MdnsSearch) {}
58+
5159
func (s *HubSuite) Test_NewConnectionsHub() {
5260
ski := "12af9e"
5361
localService := NewServiceDetails(ski)
5462
configuration := &Configuration{
5563
interfaces: []string{"en0"},
5664
}
57-
hub := newConnectionsHub(s, nil, configuration, localService)
65+
hub := newConnectionsHub(s, s, nil, configuration, localService)
5866
assert.NotNil(s.T(), hub)
5967

6068
hub.start()
@@ -114,10 +122,10 @@ func (s *HubSuite) Test_DisconnectSKI() {
114122
func (s *HubSuite) Test_RegisterConnection() {
115123
ski := "12af9e"
116124
localService := NewServiceDetails(ski)
117-
localService.SetDeviceType(model.DeviceTypeTypeEnergyManagementSystem) // this won't trigger mDNS announcement
118125

119126
sut := connectionsHub{
120127
connections: make(map[string]*ship.ShipConnection),
128+
mdns: s,
121129
localService: localService,
122130
}
123131

service/mdns.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,21 @@ type MdnsEntry struct {
2929
Addresses []net.IP // mandatory
3030
}
3131

32+
// implemented by hubConnection, used by mdns
3233
type MdnsSearch interface {
3334
ReportMdnsEntries(entries map[string]MdnsEntry)
3435
}
3536

37+
// implemented by mdns, used by hubConnection
38+
type MdnsService interface {
39+
SetupMdnsService() error
40+
ShutdownMdnsService()
41+
AnnounceMdnsEntry() error
42+
UnannounceMdnsEntry()
43+
RegisterMdnsSearch(cb MdnsSearch)
44+
UnregisterMdnsSearch(cb MdnsSearch)
45+
}
46+
3647
type mdns struct {
3748
configuration *Configuration
3849
ski string
@@ -69,14 +80,16 @@ func newMDNS(ski string, configuration *Configuration) *mdns {
6980
return m
7081
}
7182

72-
func (m *mdns) Setup() error {
83+
var _ MdnsService = (*mdns)(nil)
84+
85+
func (m *mdns) SetupMdnsService() error {
7386

7487
if av, err := m.setupAvahi(); err == nil {
7588
m.av = av
7689
}
7790

7891
// on startup always start mDNS announcement
79-
if err := m.Announce(); err != nil {
92+
if err := m.AnnounceMdnsEntry(); err != nil {
8093
return err
8194
}
8295

@@ -87,7 +100,7 @@ func (m *mdns) Setup() error {
87100

88101
<-signalC // wait for signal
89102

90-
m.Unannounce()
103+
m.UnannounceMdnsEntry()
91104
}()
92105

93106
return nil
@@ -141,7 +154,7 @@ func (m *mdns) interfaces() ([]net.Interface, []int32, error) {
141154
// Announces the service to the network via mDNS
142155
// A CEM service should always invoke this on startup
143156
// Any other service should only invoke this whenever it is not connected to a CEM service
144-
func (m *mdns) Announce() error {
157+
func (m *mdns) AnnounceMdnsEntry() error {
145158
if m.isAnnounced {
146159
return nil
147160
}
@@ -214,7 +227,7 @@ func (m *mdns) Announce() error {
214227
}
215228

216229
// Stop the mDNS announcement on the network
217-
func (m *mdns) Unannounce() {
230+
func (m *mdns) UnannounceMdnsEntry() {
218231
if !m.isAnnounced {
219232
return
220233
}
@@ -233,8 +246,8 @@ func (m *mdns) Unannounce() {
233246
}
234247

235248
// Shutdown all of mDNS
236-
func (m *mdns) shutdown() {
237-
m.Unannounce()
249+
func (m *mdns) ShutdownMdnsService() {
250+
m.UnannounceMdnsEntry()
238251

239252
if m.av != nil {
240253
m.av.Close()

service/service.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,11 @@ func (s *EEBUSService) Setup() error {
152152
entity := spine.NewEntityLocalImpl(s.spineLocalDevice, entityType, entityAddress)
153153
s.spineLocalDevice.AddEntity(entity)
154154

155+
// setup mDNS
156+
mdns := newMDNS(s.LocalService.SKI(), s.Configuration)
157+
155158
// Setup connections hub with mDNS and websocket connection handling
156-
s.connectionsHub = newConnectionsHub(s, s.spineLocalDevice, s.Configuration, s.LocalService)
159+
s.connectionsHub = newConnectionsHub(s, mdns, s.spineLocalDevice, s.Configuration, s.LocalService)
157160

158161
return nil
159162
}

0 commit comments

Comments
 (0)