@@ -17,7 +17,6 @@ import (
1717 "github.com/enbility/eebus-go/ship"
1818 "github.com/enbility/eebus-go/spine"
1919 "github.com/enbility/eebus-go/spine/model"
20- "github.com/enbility/eebus-go/util"
2120 "github.com/gorilla/websocket"
2221)
2322
@@ -71,13 +70,13 @@ type connectionsHub struct {
7170 serviceProvider serviceProvider
7271
7372 // The list of paired devices
74- pairedServices []ServiceDetails
73+ pairedServices []* ServiceDetails
7574
7675 // The web server for handling incoming websocket connections
7776 httpServer * http.Server
7877
7978 // Handling mDNS related tasks
80- mdns * mdns
79+ mdns MdnsService
8180
8281 // the SPINE local device
8382 spineLocalDevice * spine.DeviceLocalImpl
@@ -88,40 +87,38 @@ type connectionsHub struct {
8887 muxMdns sync.Mutex
8988}
9089
91- func newConnectionsHub (serviceProvider serviceProvider , spineLocalDevice * spine.DeviceLocalImpl , configuration * Configuration , localService * ServiceDetails ) ( * connectionsHub , error ) {
90+ func newConnectionsHub (serviceProvider serviceProvider , mdns MdnsService , spineLocalDevice * spine.DeviceLocalImpl , configuration * Configuration , localService * ServiceDetails ) * connectionsHub {
9291 hub := & connectionsHub {
9392 connections : make (map [string ]* ship.ShipConnection ),
9493 connectionAttemptCounter : make (map [string ]int ),
9594 connectionAttemptRunning : make (map [string ]bool ),
96- pairedServices : make ([]ServiceDetails , 0 ),
95+ pairedServices : make ([]* ServiceDetails , 0 ),
9796 serviceProvider : serviceProvider ,
9897 spineLocalDevice : spineLocalDevice ,
9998 configuration : configuration ,
10099 localService : localService ,
100+ mdns : mdns ,
101101 }
102102
103- localService .SKI = util .NormalizeSKI (localService .SKI )
104-
105- mdns , err := newMDNS (localService .SKI , configuration )
106- if err != nil {
107- return nil , err
108- }
109-
110- hub .mdns = mdns
111-
112- return hub , nil
103+ return hub
113104}
114105
115106// start the ConnectionsHub with all its services
116107func (h * connectionsHub ) start () {
108+ // start mDNS
109+ err := h .mdns .SetupMdnsService ()
110+ if err != nil {
111+ logging .Log .Error ("error during mdns setup:" , err )
112+ }
113+
117114 // start the websocket server
118115 go func () {
119116 if err := h .startWebsocketServer (); err != nil {
120117 logging .Log .Error ("error during websocket server starting:" , err )
121118 }
122119 }()
123120
124- if err := h .mdns .Announce (); err != nil {
121+ if err := h .mdns .AnnounceMdnsEntry (); err != nil {
125122 logging .Log .Error ("error registering mDNS Service:" , err )
126123 }
127124}
@@ -171,7 +168,7 @@ func (h *connectionsHub) checkRestartMdnsSearch() {
171168 if countPairedServices > countConnections {
172169 // if this is not a CEM also start the mDNS announcement
173170 if h .localService .deviceType != model .DeviceTypeTypeEnergyManagementSystem {
174- _ = h .mdns .Announce ()
171+ _ = h .mdns .AnnounceMdnsEntry ()
175172 }
176173
177174 logging .Log .Debug ("restarting mdns search" )
@@ -209,7 +206,7 @@ func (h *connectionsHub) registerConnection(connection *ship.ShipConnection) {
209206
210207 // shutdown mDNS if this is not a CEM
211208 if h .localService .deviceType != model .DeviceTypeTypeEnergyManagementSystem {
212- h .mdns .Unannounce ()
209+ h .mdns .UnannounceMdnsEntry ()
213210 h .mdns .UnregisterMdnsSearch (h )
214211 }
215212}
@@ -231,7 +228,7 @@ func (h *connectionsHub) shutdown() {
231228 h .muxCon .Lock ()
232229 defer h .muxCon .Unlock ()
233230
234- h .mdns .shutdown ()
231+ h .mdns .ShutdownMdnsService ()
235232 for _ , c := range h .connections {
236233 c .CloseConnection (false , "" )
237234 }
@@ -328,31 +325,29 @@ func (h *connectionsHub) ServeHTTP(w http.ResponseWriter, r *http.Request) {
328325 return
329326 }
330327
331- ski = util .NormalizeSKI (ski )
332- logging .Log .Debug ("incoming connection request from" , ski )
328+ // normalize the incoming SKI
329+ remoteService := NewServiceDetails (ski )
330+ logging .Log .Debug ("incoming connection request from" , remoteService .SKI ())
333331
334332 // Check if the remote service is paired
335- _ , err = h .PairedServiceForSKI (ski )
333+ _ , err = h .PairedServiceForSKI (remoteService . SKI () )
336334 if err != nil {
337335 logging .Log .Debug ("ski" , ski , "is not paired, closing the connection" )
338336 return
339337 }
340338
341- remoteService := & ServiceDetails {
342- SKI : ski ,
343- }
344339 // check if we already know this remote service
345- if remoteS , err := h .PairedServiceForSKI (ski ); err == nil {
340+ if remoteS , err := h .PairedServiceForSKI (remoteService . SKI () ); err == nil {
346341 remoteService = remoteS
347342 }
348343
349344 // don't allow a second connection
350- if ! h .keepThisConnection (conn , true , remoteService . SKI ) {
345+ if ! h .keepThisConnection (conn , true , remoteService ) {
351346 return
352347 }
353348
354- dataHandler := ship .NewWebsocketConnection (conn , remoteService .SKI )
355- shipConnection := ship .NewConnectionHandler (h , dataHandler , h .spineLocalDevice , ship .ShipRoleServer , h .localService .ShipID , remoteService .SKI , remoteService .ShipID )
349+ dataHandler := ship .NewWebsocketConnection (conn , remoteService .SKI () )
350+ shipConnection := ship .NewConnectionHandler (h , dataHandler , h .spineLocalDevice , ship .ShipRoleServer , h .localService .ShipID () , remoteService .SKI () , remoteService .ShipID () )
356351 shipConnection .Run ()
357352
358353 h .registerConnection (shipConnection )
@@ -362,7 +357,7 @@ func (h *connectionsHub) ServeHTTP(w http.ResponseWriter, r *http.Request) {
362357//
363358// returns error contains a reason for failing the connection or nil if no further tries should be processed
364359func (h * connectionsHub ) connectFoundService (remoteService * ServiceDetails , host , port string ) error {
365- if h .isSkiConnected (remoteService .SKI ) {
360+ if h .isSkiConnected (remoteService .SKI () ) {
366361 return nil
367362 }
368363
@@ -391,33 +386,33 @@ func (h *connectionsHub) connectFoundService(remoteService *ServiceDetails, host
391386
392387 if len (remoteCerts ) == 0 || remoteCerts [0 ].SubjectKeyId == nil {
393388 // Close connection as we couldn't get the remote SKI
394- errorString := fmt .Sprintf ("closing connection to %s: could not get remote SKI from certificate" , remoteService .SKI )
389+ errorString := fmt .Sprintf ("closing connection to %s: could not get remote SKI from certificate" , remoteService .SKI () )
395390 conn .Close ()
396391 return errors .New (errorString )
397392 }
398393
399394 if _ , err := skiFromCertificate (remoteCerts [0 ]); err != nil {
400395 // Close connection as the remote SKI can't be correct
401- errorString := fmt .Sprintf ("closing connection to %s: %s" , remoteService .SKI , err )
396+ errorString := fmt .Sprintf ("closing connection to %s: %s" , remoteService .SKI () , err )
402397 conn .Close ()
403398 return errors .New (errorString )
404399 }
405400
406401 remoteSKI := fmt .Sprintf ("%0x" , remoteCerts [0 ].SubjectKeyId )
407402
408- if remoteSKI != remoteService .SKI {
409- errorString := fmt .Sprintf ("closing connection to %s: SKI does not match %s" , remoteService .SKI , remoteSKI )
403+ if remoteSKI != remoteService .SKI () {
404+ errorString := fmt .Sprintf ("closing connection to %s: SKI does not match %s" , remoteService .SKI () , remoteSKI )
410405 conn .Close ()
411406 return errors .New (errorString )
412407 }
413408
414- if ! h .keepThisConnection (conn , false , remoteService . SKI ) {
415- errorString := fmt .Sprintf ("closing connection to %s: ignoring this connection" , remoteService .SKI )
409+ if ! h .keepThisConnection (conn , false , remoteService ) {
410+ errorString := fmt .Sprintf ("closing connection to %s: ignoring this connection" , remoteService .SKI () )
416411 return errors .New (errorString )
417412 }
418413
419- dataHandler := ship .NewWebsocketConnection (conn , remoteService .SKI )
420- shipConnection := ship .NewConnectionHandler (h , dataHandler , h .spineLocalDevice , ship .ShipRoleClient , h .localService .ShipID , remoteService .SKI , remoteService .ShipID )
414+ dataHandler := ship .NewWebsocketConnection (conn , remoteService .SKI () )
415+ shipConnection := ship .NewConnectionHandler (h , dataHandler , h .spineLocalDevice , ship .ShipRoleClient , h .localService .ShipID () , remoteService .SKI () , remoteService .ShipID () )
421416 shipConnection .Run ()
422417
423418 h .registerConnection (shipConnection )
@@ -430,7 +425,7 @@ func (h *connectionsHub) connectFoundService(remoteService *ServiceDetails, host
430425//
431426// returns true if this connection is fine to be continue
432427// returns false if this connection should not be established or kept
433- func (h * connectionsHub ) keepThisConnection (conn * websocket.Conn , incomingRequest bool , remoteSKI string ) bool {
428+ func (h * connectionsHub ) keepThisConnection (conn * websocket.Conn , incomingRequest bool , remoteService * ServiceDetails ) bool {
434429 // SHIP 12.2.2 defines:
435430 // prevent double connections with SKI Comparison
436431 // the node with the hight SKI value kees the most recent connection and
@@ -439,16 +434,17 @@ func (h *connectionsHub) keepThisConnection(conn *websocket.Conn, incomingReques
439434 // This is hard to implement without any flaws. Therefor I chose a
440435 // different approach: The connection initiated by the higher SKI will be kept
441436
437+ remoteSKI := remoteService .SKI ()
442438 existingC := h .connectionForSKI (remoteSKI )
443439 if existingC == nil {
444440 return true
445441 }
446442
447443 keep := false
448444 if incomingRequest {
449- keep = remoteSKI > h .localService .SKI
445+ keep = remoteSKI > h .localService .SKI ()
450446 } else {
451- keep = h .localService .SKI > remoteSKI
447+ keep = h .localService .SKI () > remoteSKI
452448 }
453449
454450 if keep {
@@ -475,29 +471,26 @@ func (h *connectionsHub) keepThisConnection(conn *websocket.Conn, incomingReques
475471func (h * connectionsHub ) PairedServiceForSKI (ski string ) (* ServiceDetails , error ) {
476472 h .muxReg .Lock ()
477473 defer h .muxReg .Unlock ()
474+
478475 for _ , service := range h .pairedServices {
479- if service .SKI == ski {
480- return & service , nil
476+ if service .SKI () == ski {
477+ return service , nil
481478 }
482479 }
483- return & ServiceDetails {} , fmt .Errorf ("no registered service found for SKI %s" , ski )
480+ return nil , fmt .Errorf ("no registered service found for SKI %s" , ski )
484481}
485482
486483// Adds a new device to the list of known devices which can be connected to
487484// and connect it if it is currently not connected
488- func (h * connectionsHub ) PairRemoteService (service ServiceDetails ) {
489-
490- // standardize the provided SKI strings
491- service .SKI = util .NormalizeSKI (service .SKI )
492-
485+ func (h * connectionsHub ) PairRemoteService (service * ServiceDetails ) {
493486 // check if it is already registered
494- if _ , err := h .PairedServiceForSKI (service .SKI ); err != nil {
487+ if _ , err := h .PairedServiceForSKI (service .SKI () ); err != nil {
495488 h .muxReg .Lock ()
496489 h .pairedServices = append (h .pairedServices , service )
497490 h .muxReg .Unlock ()
498491 }
499492
500- if ! h .isSkiConnected (service .SKI ) {
493+ if ! h .isSkiConnected (service .SKI () ) {
501494 h .mdns .RegisterMdnsSearch (h )
502495 }
503496}
@@ -507,11 +500,11 @@ func (h *connectionsHub) PairRemoteService(service ServiceDetails) {
507500func (h * connectionsHub ) UnpairRemoteService (ski string ) error {
508501 h .removeConnectionAttemptCounter (ski )
509502
510- newRegisteredDevice := make ([]ServiceDetails , 0 )
503+ newRegisteredDevice := make ([]* ServiceDetails , 0 )
511504
512505 h .muxReg .Lock ()
513506 for _ , device := range h .pairedServices {
514- if device .SKI != ski {
507+ if device .SKI () != ski {
515508 newRegisteredDevice = append (newRegisteredDevice , device )
516509 }
517510 }
@@ -543,8 +536,8 @@ func (h *connectionsHub) ReportMdnsEntries(entries map[string]MdnsEntry) {
543536 }
544537
545538 // patch the addresses list if an IPv4 address was provided
546- if remoteService .IPv4 != "" {
547- if ip := net .ParseIP (remoteService .IPv4 ); ip != nil {
539+ if remoteService .IPv4 () != "" {
540+ if ip := net .ParseIP (remoteService .IPv4 () ); ip != nil {
548541 entry .Addresses = []net.IP {ip }
549542 }
550543 }
@@ -682,7 +675,7 @@ func (h *connectionsHub) getConnectionInitiationDelayTime(ski string) (int, time
682675
683676 // seed with the local SKI for initializing rand
684677 i := new (big.Int )
685- hex := fmt .Sprintf ("0x%s" , h .localService .SKI )
678+ hex := fmt .Sprintf ("0x%s" , h .localService .SKI () )
686679 if _ , err := fmt .Sscan (hex , i ); err == nil {
687680 rand .Seed (i .Int64 () + time .Now ().UnixNano ())
688681 } else {
0 commit comments