|
@@ -20,9 +20,10 @@ const MQ_DISCONNECT = 250
|
|
const MQ_TIMEOUT = 30
|
|
const MQ_TIMEOUT = 30
|
|
|
|
|
|
var peer_force_send = 0
|
|
var peer_force_send = 0
|
|
|
|
+var mqclient mqtt.Client
|
|
|
|
|
|
// SetupMQTT creates a connection to broker and return client
|
|
// SetupMQTT creates a connection to broker and return client
|
|
-func SetupMQTT(publish bool) mqtt.Client {
|
|
|
|
|
|
+func SetupMQTT() {
|
|
opts := mqtt.NewClientOptions()
|
|
opts := mqtt.NewClientOptions()
|
|
broker, secure := servercfg.GetMessageQueueEndpoint()
|
|
broker, secure := servercfg.GetMessageQueueEndpoint()
|
|
opts.AddBroker(broker)
|
|
opts.AddBroker(broker)
|
|
@@ -37,28 +38,25 @@ func SetupMQTT(publish bool) mqtt.Client {
|
|
opts.SetKeepAlive(time.Minute)
|
|
opts.SetKeepAlive(time.Minute)
|
|
opts.SetWriteTimeout(time.Minute)
|
|
opts.SetWriteTimeout(time.Minute)
|
|
opts.SetOnConnectHandler(func(client mqtt.Client) {
|
|
opts.SetOnConnectHandler(func(client mqtt.Client) {
|
|
- if !publish {
|
|
|
|
- if token := client.Subscribe("ping/#", 2, mqtt.MessageHandler(Ping)); token.WaitTimeout(MQ_TIMEOUT*time.Second) && token.Error() != nil {
|
|
|
|
- client.Disconnect(240)
|
|
|
|
- logger.Log(0, "ping subscription failed")
|
|
|
|
- }
|
|
|
|
- if token := client.Subscribe("update/#", 0, mqtt.MessageHandler(UpdateNode)); token.WaitTimeout(MQ_TIMEOUT*time.Second) && token.Error() != nil {
|
|
|
|
- client.Disconnect(240)
|
|
|
|
- logger.Log(0, "node update subscription failed")
|
|
|
|
- }
|
|
|
|
- if token := client.Subscribe("signal/#", 0, mqtt.MessageHandler(ClientPeerUpdate)); token.WaitTimeout(MQ_TIMEOUT*time.Second) && token.Error() != nil {
|
|
|
|
- client.Disconnect(240)
|
|
|
|
- logger.Log(0, "node client subscription failed")
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- opts.SetOrderMatters(true)
|
|
|
|
- opts.SetResumeSubs(true)
|
|
|
|
|
|
+ if token := client.Subscribe("ping/#", 2, mqtt.MessageHandler(Ping)); token.WaitTimeout(MQ_TIMEOUT*time.Second) && token.Error() != nil {
|
|
|
|
+ client.Disconnect(240)
|
|
|
|
+ logger.Log(0, "ping subscription failed")
|
|
}
|
|
}
|
|
|
|
+ if token := client.Subscribe("update/#", 0, mqtt.MessageHandler(UpdateNode)); token.WaitTimeout(MQ_TIMEOUT*time.Second) && token.Error() != nil {
|
|
|
|
+ client.Disconnect(240)
|
|
|
|
+ logger.Log(0, "node update subscription failed")
|
|
|
|
+ }
|
|
|
|
+ if token := client.Subscribe("signal/#", 0, mqtt.MessageHandler(ClientPeerUpdate)); token.WaitTimeout(MQ_TIMEOUT*time.Second) && token.Error() != nil {
|
|
|
|
+ client.Disconnect(240)
|
|
|
|
+ logger.Log(0, "node client subscription failed")
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ opts.SetOrderMatters(true)
|
|
|
|
+ opts.SetResumeSubs(true)
|
|
})
|
|
})
|
|
- client := mqtt.NewClient(opts)
|
|
|
|
tperiod := time.Now().Add(10 * time.Second)
|
|
tperiod := time.Now().Add(10 * time.Second)
|
|
for {
|
|
for {
|
|
- if token := client.Connect(); !token.WaitTimeout(MQ_TIMEOUT*time.Second) || token.Error() != nil {
|
|
|
|
|
|
+ if token := mqclient.Connect(); !token.WaitTimeout(MQ_TIMEOUT*time.Second) || token.Error() != nil {
|
|
logger.Log(2, "unable to connect to broker, retrying ...")
|
|
logger.Log(2, "unable to connect to broker, retrying ...")
|
|
if time.Now().After(tperiod) {
|
|
if time.Now().After(tperiod) {
|
|
if token.Error() == nil {
|
|
if token.Error() == nil {
|
|
@@ -72,10 +70,7 @@ func SetupMQTT(publish bool) mqtt.Client {
|
|
}
|
|
}
|
|
time.Sleep(2 * time.Second)
|
|
time.Sleep(2 * time.Second)
|
|
}
|
|
}
|
|
- if !publish {
|
|
|
|
- logger.Log(0, "successfully connected to mq broker")
|
|
|
|
- }
|
|
|
|
- return client
|
|
|
|
|
|
+ logger.Log(0, "successfully connected to mq broker")
|
|
}
|
|
}
|
|
|
|
|
|
// Keepalive -- periodically pings all nodes to let them know server is still alive and doing well
|
|
// Keepalive -- periodically pings all nodes to let them know server is still alive and doing well
|