-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NOD-380] Implement MQTT client in api-server (#468)
* [NOD-380] Add MQTT to the project. * [NOD-380] Add MQTT params to config. * [NOD-380] Implement connecting to an mqtt broker. * [NOD-380] Fix a comment. * [NOD-380] Removed unnecessary option. * [NOD-380] Added comments to MQTT functions. * [NOD-380] Fix copy+paste error. * [NOD-380] Make it so that all the mqtt flags must be passed together. * [NOD-380] Use activeConfig instead of passing it everywhere.
- Loading branch information
1 parent
7d7df10
commit 1ce7f21
Showing
8 changed files
with
101 additions
and
22 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package mqtt | ||
|
||
import "github.com/daglabs/btcd/util/panics" | ||
import "github.com/daglabs/btcd/apiserver/logger" | ||
|
||
var ( | ||
log = logger.BackendLog.Logger("MQTT") | ||
spawn = panics.GoroutineWrapperFunc(log, logger.BackendLog) | ||
) |
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,50 @@ | ||
package mqtt | ||
|
||
import ( | ||
"errors" | ||
"github.com/daglabs/btcd/apiserver/config" | ||
mqtt "github.com/eclipse/paho.mqtt.golang" | ||
) | ||
|
||
// client is an instance of the MQTT client, in case we have an active connection | ||
var client mqtt.Client | ||
|
||
// GetClient returns an instance of the MQTT client, in case we have an active connection | ||
func GetClient() (mqtt.Client, error) { | ||
if client == nil { | ||
return nil, errors.New("MQTT is not connected") | ||
} | ||
return client, nil | ||
} | ||
|
||
// Connect initiates a connection to the MQTT server, if defined | ||
func Connect() error { | ||
cfg := config.ActiveConfig() | ||
if cfg.MQTTBrokerAddress == "" { | ||
// MQTT broker not defined -- nothing to do | ||
return nil | ||
} | ||
|
||
options := mqtt.NewClientOptions() | ||
options.AddBroker(cfg.MQTTBrokerAddress) | ||
options.SetUsername(cfg.MQTTUser) | ||
options.SetPassword(cfg.MQTTPassword) | ||
options.SetAutoReconnect(true) | ||
|
||
newClient := mqtt.NewClient(options) | ||
if token := newClient.Connect(); token.Wait() && token.Error() != nil { | ||
return token.Error() | ||
} | ||
client = newClient | ||
|
||
return nil | ||
} | ||
|
||
// Close closes the connection to the MQTT server, if previously connected | ||
func Close() { | ||
if client == nil { | ||
return | ||
} | ||
client.Disconnect(250) | ||
client = nil | ||
} |
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