Bladeren bron

send peer update if needed on host update

Abhishek Kondur 2 jaren geleden
bovenliggende
commit
c3d691108d
2 gewijzigde bestanden met toevoegingen van 22 en 5 verwijderingen
  1. 15 4
      logic/hosts.go
  2. 7 1
      mq/handlers.go

+ 15 - 4
logic/hosts.go

@@ -133,19 +133,30 @@ func UpdateHost(newHost, currentHost *models.Host) {
 	}
 }
 
-func UpdateHostFromClient(newHost, currHost *models.Host) {
+// UpdateHostFromClient - used for host update recieved from netclient
+func UpdateHostFromClient(newHost, currHost *models.Host) (sendPeerUpdate bool) {
 
-	if newHost.ListenPort != 0 {
+	if newHost.ListenPort != 0 && currHost.ListenPort != newHost.ListenPort {
 		currHost.ListenPort = newHost.ListenPort
+		sendPeerUpdate = true
 	}
-	if newHost.ProxyListenPort != 0 {
+	if newHost.ProxyListenPort != 0 && currHost.ProxyListenPort != newHost.ProxyListenPort {
 		currHost.ProxyListenPort = newHost.ProxyListenPort
+		sendPeerUpdate = true
+	}
+	if currHost.ProxyEnabled != newHost.ProxyEnabled {
+		currHost.ProxyEnabled = newHost.ProxyEnabled
+		sendPeerUpdate = true
+	}
+	if currHost.EndpointIP.String() != newHost.EndpointIP.String() {
+		currHost.EndpointIP = newHost.EndpointIP
+		sendPeerUpdate = true
 	}
-	currHost.ProxyEnabled = newHost.ProxyEnabled
 	currHost.DaemonInstalled = newHost.DaemonInstalled
 	currHost.Debug = newHost.Debug
 	currHost.Verbosity = newHost.Verbosity
 	currHost.Version = newHost.Version
+	return
 }
 
 // UpsertHost - upserts into DB a given host model, does not check for existence*

+ 7 - 1
mq/handlers.go

@@ -146,11 +146,17 @@ func UpdateHost(client mqtt.Client, msg mqtt.Message) {
 		// 		logger.Log(1, "failed to reset failover list during node update", currentHost.ID.String(), currentHost.Network)
 		// 	}
 		// }
-		logic.UpdateHostFromClient(&newHost, currentHost)
+		sendPeerUpdate := logic.UpdateHostFromClient(&newHost, currentHost)
 		if err := logic.UpsertHost(&newHost); err != nil {
 			logger.Log(1, "error saving host", err.Error())
 			return
 		}
+		if sendPeerUpdate {
+			err := PublishPeerUpdate()
+			if err != nil {
+				logger.Log(1, "failed to send host peer update: ", err.Error())
+			}
+		}
 		logger.Log(1, "updated host", newHost.ID.String())
 	}()
 }