-
Notifications
You must be signed in to change notification settings - Fork 485
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(command): implement MessagingRouter
Save active requests in two maps using requestId as keys. This allows command service to know where to route the response (to external MQTT or internal MessageBus) Signed-off-by: Chris Hung <chris@iotechsys.com>
- Loading branch information
Chris Hung
committed
Sep 30, 2022
1 parent
2588d96
commit 91c61b8
Showing
6 changed files
with
157 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
internal/core/command/controller/messaging/mocks/MessagingRouter.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// Copyright (C) 2022 IOTech Ltd | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package messaging | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
) | ||
|
||
// MessagingRouter defines interface for Command Service to know | ||
// where to route the receiving device command response. | ||
type MessagingRouter interface { | ||
// ResponseTopic returns the responseTopicPrefix by requestId, and a boolean value | ||
// indicates its original source(external MQTT or internal MessageBus). | ||
ResponseTopic(requestId string) (string, bool, error) | ||
// SetResponseTopic sets the responseTopicPrefix with RequestId as the key | ||
SetResponseTopic(requestId string, topic string, external bool) | ||
} | ||
|
||
func NewMessagingRouter() MessagingRouter { | ||
return &router{ | ||
internalCommandRequestMap: make(map[string]string), | ||
externalCommandRequestMap: make(map[string]string), | ||
} | ||
} | ||
|
||
type router struct { | ||
mutex sync.Mutex | ||
internalCommandRequestMap map[string]string | ||
externalCommandRequestMap map[string]string | ||
} | ||
|
||
func (r *router) ResponseTopic(requestId string) (string, bool, error) { | ||
r.mutex.Lock() | ||
defer r.mutex.Unlock() | ||
|
||
topic, ok := r.externalCommandRequestMap[requestId] | ||
if ok { | ||
delete(r.externalCommandRequestMap, requestId) | ||
return topic, true, nil | ||
} | ||
|
||
topic, ok = r.internalCommandRequestMap[requestId] | ||
if ok { | ||
delete(r.internalCommandRequestMap, requestId) | ||
return topic, false, nil | ||
} | ||
|
||
return "", false, errors.New("requestId not found") | ||
} | ||
|
||
func (r *router) SetResponseTopic(requestId string, topic string, external bool) { | ||
r.mutex.Lock() | ||
defer r.mutex.Unlock() | ||
|
||
if external { | ||
r.externalCommandRequestMap[requestId] = topic | ||
return | ||
} | ||
|
||
r.internalCommandRequestMap[requestId] = topic | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters