Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MessageHandler interface enhancements #18

Closed
stoyan-zoubev opened this issue Sep 19, 2022 · 2 comments · Fixed by #30
Closed

MessageHandler interface enhancements #18

stoyan-zoubev opened this issue Sep 19, 2022 · 2 comments · Fixed by #30
Assignees
Labels
task Single unit of work
Milestone

Comments

@stoyan-zoubev
Copy link
Contributor

stoyan-zoubev commented Sep 19, 2022

This is the current version of the MessageHandler interface:

type MessageHandler interface {
	Init(settings *config.AzureSettings, connSettings *config.AzureConnectionSettings) error
	HandleMessage(message *message.Message) ([]*message.Message, error)
	Name() string
	Topics() []string
}

First, it is used in both directions - cloud-to-device (command) messages and device-to-cloud (telemetry) messages. However, not all methods apply to both directions, e.g. communication between the device and Azure IoT Hub goes on with strictly-defined topics, so in case of cloud-to-device messaging MessageHandler.Topics() is never used. It is only used in case of device-to-cloud messages in order to specify local topics for subscriptions. It is better to have a clear separation between command and telemetry handler interfaces.

Second, the handler registries required handlers to be instantiates empty and Init method to receive settings. If pluggability is improved with [#17 Improve message handler pluggability] then this no longer be needed and handler settings can be passed to handler constructor methods. Init method may remain and receive only data that is not yet known upon handler creation - AzureConnectionSettings, or even better a subset of this structure with only useful data for message handlers - e.g. device connection info like device ID, Azure IoT hub name, hostname.

Additionally, on the Topics method - the underlying watermill library method uses type string, not []string. It is preferred to align the interface with the library by modifying the return type and avoid unnecessary conversions.

Proposal:

// MessageHandler represents the internal interface for implementing a message handler.
type messageHandler interface {
	Init(deviceInfo *config.DeviceInfo) error
	HandleMessage(message *message.Message) ([]*message.Message, error)
	Name() string
}

// CommandHandler represents the internal interface for implementing handlers for cloud-to-device messages from Azure IoT Hub.
type CommandHandler interface {
	messageHandler
}

// TelemetryHandler represents the internal interface for implementing handlers for device-to-cloud messages for Azure IoT Hub.
type TelemetryHandler interface {
	messageHandler
	Topic() string
}

where config.DeviceInfo is the device connection data in subset structure in config package:

// DeviceInfo contains the device related info.
type DeviceInfo struct {
	HubName  string
	DeviceID string
}

// AzureConnectionSettings contains the configuration data for establishing connection to the Azure IoT Hub.
type AzureConnectionSettings struct {
	*DeviceInfo

	DeviceCert    string
	DeviceKey     string
	TokenValidity time.Duration

	*SharedAccessKey
}

Also, the handlers.go source file may be moved directly into routing/messages/handlers directory, package handlers and get rid of common package.

@e-grigorov
Copy link
Contributor

  • The handlers should not be Watermill specific i.e. no type, doc etc. dependencies
  • DeviceInfo.HostName - the naming has to be improved, the host name doesn't seem to be device info, all names have to be revised

@e-grigorov e-grigorov added the task Single unit of work label Sep 19, 2022
@e-grigorov e-grigorov moved this to Todo in Eclipse Kanto Sep 19, 2022
@konstantina-gramatova konstantina-gramatova added this to the M3 milestone Sep 29, 2022
@stoyan-zoubev
Copy link
Contributor Author

stoyan-zoubev commented Oct 11, 2022

I agree with you, @e-grigorov. But I also have some comments:

  • Making the handlers not to be Watermill specific means defining a struct to represent the message in order to replace watermill's Message struct. I am OK with this but let's do it step by step. Just keep this issue with the proposed changes in the description in order to make the handler interfaces more usable, with relatively less number of changes. And in the future, we may go with removing the watermill dependency in the interface (if it is really worthy).
  • I will try to come up with a better name for the given structure, as a replacement for DeviceInfo. What about CloudConnectionInfo? Do you think it is better, shall I update the issue description? Any suggestions from your side are welcome :)

@stoyan-zoubev stoyan-zoubev moved this from Todo to In Progress in Eclipse Kanto Oct 13, 2022
stoyan-zoubev added a commit to SoftwareDefinedVehicle/kanto-azure-connector that referenced this issue Oct 13, 2022
- Separate interfaces for c2d and d2c messages - CommandHandler and TelemetryHandler
- Removed AzureSettings parameter from handler Init method, static parameters should be passed to handler constructors.
- MessageHandler.Init method receives cloud connection info only (this is data that may not be known at the time of handler creation) - Device ID, Hostname, etc.
- Simplified test methods

Signed-off-by: Stoyan Zoubev <[email protected]>
@e-grigorov e-grigorov linked a pull request Oct 14, 2022 that will close this issue
@e-grigorov e-grigorov moved this from In Progress to In Review in Eclipse Kanto Oct 14, 2022
stoyan-zoubev added a commit to SoftwareDefinedVehicle/kanto-azure-connector that referenced this issue Oct 14, 2022
Fixed PR review comments.
- CloudConnectionInfo is not a pointer inside AzureConnectionSettings
- fixed typo

Signed-off-by: Stoyan Zoubev <[email protected]>
stoyan-zoubev added a commit to SoftwareDefinedVehicle/kanto-azure-connector that referenced this issue Oct 14, 2022
Fixed PR review comments.
- CloudConnectionInfo renamed to RemoteConnectionInfo
- SharedAccessKey struct removed, not really needed (SharedAccessKeyName is not applicable for Azure IoT Hub device connection string.
- SharedAccessKey is now just a []byte field.

Signed-off-by: Stoyan Zoubev <[email protected]>
stoyan-zoubev added a commit to SoftwareDefinedVehicle/kanto-azure-connector that referenced this issue Oct 14, 2022
Fixed PR review comments.
- CloudConnectionInfo renamed to RemoteConnectionInfo
- SharedAccessKey struct removed, not really needed (SharedAccessKeyName is not applicable for Azure IoT Hub device connection string.
- SharedAccessKey is now just a []byte field.

Signed-off-by: Stoyan Zoubev <[email protected]>
stoyan-zoubev added a commit to SoftwareDefinedVehicle/kanto-azure-connector that referenced this issue Oct 14, 2022
Fixed PR review comments.
- CloudConnectionInfo renamed to RemoteConnectionInfo
- SharedAccessKey struct removed, not really needed (SharedAccessKeyName is not applicable for Azure IoT Hub device connection string.
- SharedAccessKey is now just a []byte field.

Signed-off-by: Stoyan Zoubev <[email protected]>
e-grigorov pushed a commit that referenced this issue Oct 14, 2022
[#18] MessageHandler interface enhancements

- Separate interfaces for c2d and d2c messages - CommandHandler and TelemetryHandler
- Removed AzureSettings parameter from handler Init method, static parameters should be passed to handler constructors.
- MessageHandler.Init method receives cloud connection info only (this is data that may not be known at the time of handler creation) - Device ID, Hostname, etc.
- Simplified test methods

Signed-off-by: Stoyan Zoubev <[email protected]>
Repository owner moved this from In Review to Done in Eclipse Kanto Oct 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
task Single unit of work
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants