From b1ad959b2c4d2e0b82a8a1d33ee388ad99e20f63 Mon Sep 17 00:00:00 2001 From: Andreas Linde Date: Fri, 5 Jan 2024 20:21:26 +0100 Subject: [PATCH] Move service interfaces into api file --- service/api.go | 100 ++++++++++++++++++++++++++++++++++++++++++ service/hub.go | 37 ---------------- service/mdns.go | 17 ------- service/mdns/types.go | 12 ----- service/service.go | 27 ------------ 5 files changed, 100 insertions(+), 93 deletions(-) create mode 100644 service/api.go diff --git a/service/api.go b/service/api.go new file mode 100644 index 00000000..9a5c9dfb --- /dev/null +++ b/service/api.go @@ -0,0 +1,100 @@ +package service + +import "net" + +/* EEBUSService */ + +//go:generate mockgen -destination=mock_service_test.go -package=service github.com/enbility/eebus-go/service EEBUSServiceHandler + +// interface for receiving data for specific events +type EEBUSServiceHandler interface { + // report all currently visible EEBUS services + VisibleRemoteServicesUpdated(service *EEBUSService, entries []RemoteService) + + // report a connection to a SKI + RemoteSKIConnected(service *EEBUSService, ski string) + + // report a disconnection to a SKI + RemoteSKIDisconnected(service *EEBUSService, ski string) + + // Provides the SHIP ID the remote service reported during the handshake process + // This needs to be persisted and passed on for future remote service connections + // when using `PairRemoteService` + ServiceShipIDUpdate(ski string, shipdID string) + + // Provides the current pairing state for the remote service + // This is called whenever the state changes and can be used to + // provide user information for the pairing/connection process + ServicePairingDetailUpdate(ski string, detail *ConnectionStateDetail) + + // return if the user is still able to trust the connection + AllowWaitingForTrust(ski string) bool +} + +/* Hub */ + +//go:generate mockgen -destination=mock_hub_test.go -package=service github.com/enbility/eebus-go/service ServiceProvider,ConnectionsHub + +// interface for reporting data from connectionsHub to the EEBUSService +type ServiceProvider interface { + // report a newly discovered remote EEBUS service + VisibleMDNSRecordsUpdated(entries []*MdnsEntry) + + // report a connection to a SKI + RemoteSKIConnected(ski string) + + // report a disconnection to a SKI + RemoteSKIDisconnected(ski string) + + // provide the SHIP ID received during SHIP handshake process + // the ID needs to be stored and then provided for remote services so it can be compared and verified + ServiceShipIDUpdate(ski string, shipID string) + + // provides the current handshake state for a given SKI + ServicePairingDetailUpdate(ski string, detail *ConnectionStateDetail) + + // return if the user is still able to trust the connection + AllowWaitingForTrust(ski string) bool +} + +type ConnectionsHub interface { + PairingDetailForSki(ski string) *ConnectionStateDetail + StartBrowseMdnsSearch() + StopBrowseMdnsSearch() + Start() + Shutdown() + ServiceForSKI(ski string) *ServiceDetails + RegisterRemoteSKI(ski string, enable bool) + InitiatePairingWithSKI(ski string) + CancelPairingWithSKI(ski string) + DisconnectSKI(ski string, reason string) +} + +/* Mdns */ + +//go:generate mockgen -destination=mock_mdns_test.go -package=service github.com/enbility/eebus-go/service MdnsSearch,MdnsService + +// implemented by hubConnection, used by mdns +type MdnsSearch interface { + ReportMdnsEntries(entries map[string]*MdnsEntry) +} + +// implemented by mdns, used by hubConnection +type MdnsService interface { + SetupMdnsService() error + ShutdownMdnsService() + AnnounceMdnsEntry() error + UnannounceMdnsEntry() + RegisterMdnsSearch(cb MdnsSearch) + UnregisterMdnsSearch(cb MdnsSearch) +} + +//go:generate mockery --name=MdnsProvider + +type MdnsProvider interface { + CheckAvailability() bool + Shutdown() + Announce(serviceName string, port int, txt []string) error + Unannounce() + ResolveEntries(cancelChan chan bool, callback func(elements map[string]string, name, host string, addresses []net.IP, port int, remove bool)) +} diff --git a/service/hub.go b/service/hub.go index 7928579c..d64ac931 100644 --- a/service/hub.go +++ b/service/hub.go @@ -40,43 +40,6 @@ var connectionInitiationDelayTimeRanges = []connectionInitiationDelayTimeRange{ {min: 10, max: 20}, } -//go:generate mockgen -destination=mock_hub_test.go -package=service github.com/enbility/eebus-go/service ServiceProvider,ConnectionsHub - -// interface for reporting data from connectionsHub to the EEBUSService -type ServiceProvider interface { - // report a newly discovered remote EEBUS service - VisibleMDNSRecordsUpdated(entries []*MdnsEntry) - - // report a connection to a SKI - RemoteSKIConnected(ski string) - - // report a disconnection to a SKI - RemoteSKIDisconnected(ski string) - - // provide the SHIP ID received during SHIP handshake process - // the ID needs to be stored and then provided for remote services so it can be compared and verified - ServiceShipIDUpdate(ski string, shipID string) - - // provides the current handshake state for a given SKI - ServicePairingDetailUpdate(ski string, detail *ConnectionStateDetail) - - // return if the user is still able to trust the connection - AllowWaitingForTrust(ski string) bool -} - -type ConnectionsHub interface { - PairingDetailForSki(ski string) *ConnectionStateDetail - StartBrowseMdnsSearch() - StopBrowseMdnsSearch() - Start() - Shutdown() - ServiceForSKI(ski string) *ServiceDetails - RegisterRemoteSKI(ski string, enable bool) - InitiatePairingWithSKI(ski string) - CancelPairingWithSKI(ski string) - DisconnectSKI(ski string, reason string) -} - // handling all connections to remote services type connectionsHubImpl struct { connections map[string]ship.ShipConnection diff --git a/service/mdns.go b/service/mdns.go index 369ffb84..21216187 100644 --- a/service/mdns.go +++ b/service/mdns.go @@ -29,23 +29,6 @@ type MdnsEntry struct { Addresses []net.IP // mandatory } -//go:generate mockgen -destination=mock_mdns_test.go -package=service github.com/enbility/eebus-go/service MdnsSearch,MdnsService - -// implemented by hubConnection, used by mdns -type MdnsSearch interface { - ReportMdnsEntries(entries map[string]*MdnsEntry) -} - -// implemented by mdns, used by hubConnection -type MdnsService interface { - SetupMdnsService() error - ShutdownMdnsService() - AnnounceMdnsEntry() error - UnannounceMdnsEntry() - RegisterMdnsSearch(cb MdnsSearch) - UnregisterMdnsSearch(cb MdnsSearch) -} - type mdnsManager struct { configuration *Configuration ski string diff --git a/service/mdns/types.go b/service/mdns/types.go index 97b520d2..b331744c 100644 --- a/service/mdns/types.go +++ b/service/mdns/types.go @@ -1,16 +1,4 @@ package mdns -import "net" - const shipZeroConfServiceType = "_ship._tcp" const shipZeroConfDomain = "local." - -//go:generate mockery --name=MdnsProvider - -type MdnsProvider interface { - CheckAvailability() bool - Shutdown() - Announce(serviceName string, port int, txt []string) error - Unannounce() - ResolveEntries(cancelChan chan bool, callback func(elements map[string]string, name, host string, addresses []net.IP, port int, remove bool)) -} diff --git a/service/service.go b/service/service.go index 6308a1d3..25ff602c 100644 --- a/service/service.go +++ b/service/service.go @@ -20,33 +20,6 @@ type RemoteService struct { Model string `json:"model"` } -//go:generate mockgen -destination=mock_service_test.go -package=service github.com/enbility/eebus-go/service EEBUSServiceHandler - -// interface for receiving data for specific events -type EEBUSServiceHandler interface { - // report all currently visible EEBUS services - VisibleRemoteServicesUpdated(service *EEBUSService, entries []RemoteService) - - // report a connection to a SKI - RemoteSKIConnected(service *EEBUSService, ski string) - - // report a disconnection to a SKI - RemoteSKIDisconnected(service *EEBUSService, ski string) - - // Provides the SHIP ID the remote service reported during the handshake process - // This needs to be persisted and passed on for future remote service connections - // when using `PairRemoteService` - ServiceShipIDUpdate(ski string, shipdID string) - - // Provides the current pairing state for the remote service - // This is called whenever the state changes and can be used to - // provide user information for the pairing/connection process - ServicePairingDetailUpdate(ski string, detail *ConnectionStateDetail) - - // return if the user is still able to trust the connection - AllowWaitingForTrust(ski string) bool -} - // A service is the central element of an EEBUS service // including its websocket server and a zeroconf service. type EEBUSService struct {