Send discovery packet every time a new label is added
This commit is contained in:
		
							parent
							
								
									a0abda5196
								
							
						
					
					
						commit
						8493c7cb40
					
				
					 2 changed files with 43 additions and 11 deletions
				
			
		|  | @ -63,6 +63,9 @@ func addLabelsHandler(c echo.Context) error { | ||||||
| 		return c.String(http.StatusBadRequest, err.Error()) | 		return c.String(http.StatusBadRequest, err.Error()) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	// Update the other nodes with this new change
 | ||||||
|  | 	sendDiscoveryPacket() | ||||||
|  | 
 | ||||||
| 	return c.String(http.StatusOK, "OK") | 	return c.String(http.StatusOK, "OK") | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -84,5 +87,8 @@ func deleteLabelsHandler(c echo.Context) error { | ||||||
| 		return c.String(http.StatusBadRequest, err.Error()) | 		return c.String(http.StatusBadRequest, err.Error()) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	// Update the other nodes with this new change
 | ||||||
|  | 	sendDiscoveryPacket() | ||||||
|  | 
 | ||||||
| 	return c.String(http.StatusOK, "OK") | 	return c.String(http.StatusOK, "OK") | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -24,6 +24,7 @@ var localHost server.LocalHost | ||||||
| var config Config | var config Config | ||||||
| 
 | 
 | ||||||
| var shuttingDown bool | var shuttingDown bool | ||||||
|  | var sendDiscoveryPacketTrigger chan bool = make(chan bool) | ||||||
| 
 | 
 | ||||||
| func init() { | func init() { | ||||||
| 	// Load config from environment variables
 | 	// Load config from environment variables
 | ||||||
|  | @ -88,19 +89,30 @@ func sendGoodbyePacket() { | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // sendDisoveryPacket sends discovery packet regularly so the network know we exist
 | // sendDiscoveryPacket sends a single discovery packet out
 | ||||||
| func sendDiscoveryPacket() { | func sendDiscoveryPacket() { | ||||||
| 	for { | 	sendDiscoveryPacketTrigger <- true | ||||||
| 		discovery, err := localHost.GetIdentification() | } | ||||||
| 		if err != nil { |  | ||||||
| 			log.Printf("sending discovery identification error: %v\n", err) |  | ||||||
| 		} |  | ||||||
| 
 | 
 | ||||||
| 		err = driver.SendDiscoveryPacket(discovery) | // sendDisoveryPacket sends discovery packet to the driver which passes it to the
 | ||||||
| 		if err != nil { | // other nodes. By this it propagates any change that happens in the local discovery struct.
 | ||||||
| 			log.Println(err) | // Every tune trigger is triggered it sends one message.
 | ||||||
|  | func sendDiscoveryPacketTask(trigger chan bool) { | ||||||
|  | 	for { | ||||||
|  | 		// We are waiting for the trigger
 | ||||||
|  | 		<-trigger | ||||||
|  | 
 | ||||||
|  | 		if !shuttingDown { | ||||||
|  | 			discovery, err := localHost.GetIdentification() | ||||||
|  | 			if err != nil { | ||||||
|  | 				log.Println("sending discovery identification error: %v", err) | ||||||
|  | 			} | ||||||
|  | 
 | ||||||
|  | 			err = driver.SendDiscoveryPacket(discovery) | ||||||
|  | 			if err != nil { | ||||||
|  | 				log.Println(err.Error()) | ||||||
|  | 			} | ||||||
| 		} | 		} | ||||||
| 		time.Sleep(time.Duration(config.KeepAlive) * time.Second) |  | ||||||
| 
 | 
 | ||||||
| 		if shuttingDown { | 		if shuttingDown { | ||||||
| 			break | 			break | ||||||
|  | @ -145,7 +157,21 @@ func main() { | ||||||
| 
 | 
 | ||||||
| 	// If config.Register is false this instance won't be registered with other nodes
 | 	// If config.Register is false this instance won't be registered with other nodes
 | ||||||
| 	if config.Register { | 	if config.Register { | ||||||
| 		go sendDiscoveryPacket() | 		// This is background process that sends the message
 | ||||||
|  | 		go sendDiscoveryPacketTask(sendDiscoveryPacketTrigger) | ||||||
|  | 
 | ||||||
|  | 		// This triggers the process
 | ||||||
|  | 		go func() { | ||||||
|  | 			for { | ||||||
|  | 				sendDiscoveryPacket() | ||||||
|  | 
 | ||||||
|  | 				time.Sleep(time.Duration(config.KeepAlive) * time.Second) | ||||||
|  | 
 | ||||||
|  | 				if shuttingDown { | ||||||
|  | 					break | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 		}() | ||||||
| 	} else { | 	} else { | ||||||
| 		log.Println("standalone mode, I won't register myself") | 		log.Println("standalone mode, I won't register myself") | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue