|
@@ -36,42 +36,39 @@ type cachedMessage struct {
|
|
|
|
|
|
// Daemon runs netclient daemon from command line
|
|
|
func Daemon() error {
|
|
|
+ var exists = struct{}{}
|
|
|
+ serverSet := make(map[string]struct{})
|
|
|
// == initial pull of all networks ==
|
|
|
networks, _ := ncutils.GetSystemNetworks()
|
|
|
for _, network := range networks {
|
|
|
+ cfg := config.ClientConfig{}
|
|
|
+ cfg.Network = network
|
|
|
+ cfg.ReadConfig()
|
|
|
+ serverSet[cfg.Server.Server] = exists
|
|
|
//temporary code --- remove in version v0.13.0
|
|
|
removeHostDNS(network, ncutils.IsWindows())
|
|
|
// end of code to be removed in version v0.13.0
|
|
|
- var cfg config.ClientConfig
|
|
|
- cfg.Network = network
|
|
|
- cfg.ReadConfig()
|
|
|
initialPull(cfg.Network)
|
|
|
}
|
|
|
|
|
|
- // == get all the comms networks on machine ==
|
|
|
- commsNetworks, err := getCommsNetworks(networks[:])
|
|
|
- if err != nil {
|
|
|
- return errors.New("no comm networks exist")
|
|
|
- }
|
|
|
-
|
|
|
- // == subscribe to all nodes on each comms network on machine ==
|
|
|
- for currCommsNet := range commsNetworks {
|
|
|
- logger.Log(1, "started comms network daemon, ", currCommsNet)
|
|
|
+ // == subscribe to all nodes for each on machine ==
|
|
|
+ for server := range serverSet {
|
|
|
+ logger.Log(1, "started daemon for server ", server)
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
- networkcontext.Store(currCommsNet, cancel)
|
|
|
- go messageQueue(ctx, currCommsNet)
|
|
|
+ networkcontext.Store(server, cancel)
|
|
|
+ go messageQueue(ctx, server)
|
|
|
}
|
|
|
|
|
|
// == add waitgroup and cancel for checkin routine ==
|
|
|
wg := sync.WaitGroup{}
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
wg.Add(1)
|
|
|
- go Checkin(ctx, &wg, commsNetworks)
|
|
|
+ go Checkin(ctx, &wg, serverSet)
|
|
|
quit := make(chan os.Signal, 1)
|
|
|
- signal.Notify(quit, syscall.SIGTERM, os.Interrupt, os.Kill)
|
|
|
+ signal.Notify(quit, syscall.SIGTERM, os.Interrupt)
|
|
|
<-quit
|
|
|
- for currCommsNet := range commsNetworks {
|
|
|
- if cancel, ok := networkcontext.Load(currCommsNet); ok {
|
|
|
+ for server := range serverSet {
|
|
|
+ if cancel, ok := networkcontext.Load(server); ok {
|
|
|
cancel.(context.CancelFunc)()
|
|
|
}
|
|
|
}
|
|
@@ -101,16 +98,14 @@ func UpdateKeys(nodeCfg *config.ClientConfig, client mqtt.Client) error {
|
|
|
}
|
|
|
|
|
|
nodeCfg.Node.PublicKey = key.PublicKey().String()
|
|
|
- var commsCfg = getCommsCfgByNode(&nodeCfg.Node)
|
|
|
- PublishNodeUpdate(&commsCfg, nodeCfg)
|
|
|
+ PublishNodeUpdate(nodeCfg)
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
// PingServer -- checks if server is reachable
|
|
|
// use commsCfg only*
|
|
|
-func PingServer(commsCfg *config.ClientConfig) error {
|
|
|
- node := getServerAddress(commsCfg)
|
|
|
- pinger, err := ping.NewPinger(node)
|
|
|
+func PingServer(cfg *config.ClientConfig) error {
|
|
|
+ pinger, err := ping.NewPinger(cfg.Server.Server)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
@@ -120,6 +115,7 @@ func PingServer(commsCfg *config.ClientConfig) error {
|
|
|
if stats.PacketLoss == 100 {
|
|
|
return errors.New("ping error")
|
|
|
}
|
|
|
+ logger.Log(3, "ping of server", cfg.Server.Server, "was successful")
|
|
|
return nil
|
|
|
}
|
|
|
|
|
@@ -140,16 +136,12 @@ func setSubscriptions(client mqtt.Client, nodeCfg *config.ClientConfig) {
|
|
|
logger.Log(0, token.Error().Error())
|
|
|
return
|
|
|
}
|
|
|
- if nodeCfg.DebugOn {
|
|
|
- logger.Log(0, fmt.Sprintf("subscribed to node updates for node %s update/%s/%s", nodeCfg.Node.Name, nodeCfg.Node.Network, nodeCfg.Node.ID))
|
|
|
- }
|
|
|
+ logger.Log(3, fmt.Sprintf("subscribed to node updates for node %s update/%s/%s", nodeCfg.Node.Name, nodeCfg.Node.Network, nodeCfg.Node.ID))
|
|
|
if token := client.Subscribe(fmt.Sprintf("peers/%s/%s", nodeCfg.Node.Network, nodeCfg.Node.ID), 0, mqtt.MessageHandler(UpdatePeers)); token.Wait() && token.Error() != nil {
|
|
|
logger.Log(0, token.Error().Error())
|
|
|
return
|
|
|
}
|
|
|
- if nodeCfg.DebugOn {
|
|
|
- logger.Log(0, fmt.Sprintf("subscribed to peer updates for node %s peers/%s/%s", nodeCfg.Node.Name, nodeCfg.Node.Network, nodeCfg.Node.ID))
|
|
|
- }
|
|
|
+ logger.Log(3, fmt.Sprintf("subscribed to peer updates for node %s peers/%s/%s", nodeCfg.Node.Name, nodeCfg.Node.Network, nodeCfg.Node.ID))
|
|
|
}
|
|
|
|
|
|
// on a delete usually, pass in the nodecfg to unsubscribe client broker communications
|
|
@@ -171,23 +163,109 @@ func unsubscribeNode(client mqtt.Client, nodeCfg *config.ClientConfig) {
|
|
|
}
|
|
|
|
|
|
// sets up Message Queue and subsribes/publishes updates to/from server
|
|
|
-// the client should subscribe to ALL nodes that exist on unique comms network locally
|
|
|
-func messageQueue(ctx context.Context, commsNet string) {
|
|
|
- var commsCfg config.ClientConfig
|
|
|
- commsCfg.Network = commsNet
|
|
|
- commsCfg.ReadConfig()
|
|
|
- logger.Log(0, "netclient daemon started for network: ", commsNet)
|
|
|
- client := setupMQTT(&commsCfg, false)
|
|
|
+// the client should subscribe to ALL nodes that exist on server locally
|
|
|
+func messageQueue(ctx context.Context, server string) {
|
|
|
+ logger.Log(0, "netclient daemon started for server: ", server)
|
|
|
+ client := setupMQTTSub(server)
|
|
|
defer client.Disconnect(250)
|
|
|
<-ctx.Done()
|
|
|
- logger.Log(0, "shutting down daemon for comms network ", commsNet)
|
|
|
+ logger.Log(0, "shutting down daemon for server ", server)
|
|
|
+}
|
|
|
+
|
|
|
+// setupMQTTSub creates a connection to broker and subscribes to topic
|
|
|
+// utilizes comms client configs to setup connections
|
|
|
+func setupMQTTSub(server string) mqtt.Client {
|
|
|
+ opts := mqtt.NewClientOptions()
|
|
|
+ opts.AddBroker(server + ":1883") // TODO get the appropriate port of the comms mq server
|
|
|
+ opts.ClientID = ncutils.MakeRandomString(23) // helps avoid id duplication on broker
|
|
|
+ opts.SetDefaultPublishHandler(All)
|
|
|
+ opts.SetAutoReconnect(true)
|
|
|
+ opts.SetConnectRetry(true)
|
|
|
+ opts.SetConnectRetryInterval(time.Second << 2)
|
|
|
+ opts.SetKeepAlive(time.Minute >> 1)
|
|
|
+ opts.SetWriteTimeout(time.Minute)
|
|
|
+ opts.SetOnConnectHandler(func(client mqtt.Client) {
|
|
|
+ networks, err := ncutils.GetSystemNetworks()
|
|
|
+ if err != nil {
|
|
|
+ logger.Log(0, "error retriving networks ", err.Error())
|
|
|
+ }
|
|
|
+ for _, network := range networks {
|
|
|
+ var currNodeCfg config.ClientConfig
|
|
|
+ currNodeCfg.Network = network
|
|
|
+ currNodeCfg.ReadConfig()
|
|
|
+ if currNodeCfg.Server.Server == server {
|
|
|
+ setSubscriptions(client, &currNodeCfg)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ opts.SetOrderMatters(true)
|
|
|
+ opts.SetResumeSubs(true)
|
|
|
+ opts.SetConnectionLostHandler(func(c mqtt.Client, e error) {
|
|
|
+ logger.Log(0, "detected broker connection lost, running pull for all nodes")
|
|
|
+ networks, err := ncutils.GetSystemNetworks()
|
|
|
+ if err != nil {
|
|
|
+ logger.Log(0, "error retriving networks ", err.Error())
|
|
|
+ }
|
|
|
+ for _, network := range networks {
|
|
|
+ var cfg config.ClientConfig
|
|
|
+ cfg.Network = network
|
|
|
+ cfg.ReadConfig()
|
|
|
+ _, err := Pull(cfg.Node.Network, true)
|
|
|
+ if err != nil {
|
|
|
+ logger.Log(0, "could not run pull, server unreachable: ", err.Error())
|
|
|
+ logger.Log(0, "waiting to retry...")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // don't think following log message is accurate
|
|
|
+ //logger.Log(0, "connection re-established with mqtt server")
|
|
|
+ })
|
|
|
+
|
|
|
+ client := mqtt.NewClient(opts)
|
|
|
+ tperiod := time.Now().Add(12 * time.Second)
|
|
|
+ for {
|
|
|
+ //if after 12 seconds, try a gRPC pull on the last try
|
|
|
+ if time.Now().After(tperiod) {
|
|
|
+ networks, err := ncutils.GetSystemNetworks()
|
|
|
+ if err != nil {
|
|
|
+ logger.Log(0, "error retriving networks ", err.Error())
|
|
|
+ }
|
|
|
+ for _, network := range networks {
|
|
|
+ var cfg config.ClientConfig
|
|
|
+ cfg.Network = network
|
|
|
+ cfg.ReadConfig()
|
|
|
+ if cfg.Server.Server == server {
|
|
|
+ _, err := Pull(cfg.Node.Network, true)
|
|
|
+ if err != nil {
|
|
|
+ logger.Log(0, "could not run pull, exiting ", cfg.Node.Network, " setup: ", err.Error())
|
|
|
+ return client
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ time.Sleep(time.Second)
|
|
|
+ }
|
|
|
+ if token := client.Connect(); token.Wait() && token.Error() != nil {
|
|
|
+ logger.Log(0, "unable to connect to broker, retrying ...")
|
|
|
+ if time.Now().After(tperiod) {
|
|
|
+ logger.Log(0, "could not connect to broker, exiting ", server, " setup: ", token.Error().Error())
|
|
|
+ if strings.Contains(token.Error().Error(), "connectex") || strings.Contains(token.Error().Error(), "i/o timeout") {
|
|
|
+ logger.Log(0, "connection issue detected.. restarting daemon")
|
|
|
+ daemon.Restart()
|
|
|
+ }
|
|
|
+ return client
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ time.Sleep(2 * time.Second)
|
|
|
+ }
|
|
|
+ return client
|
|
|
}
|
|
|
|
|
|
// setupMQTT creates a connection to broker and return client
|
|
|
// utilizes comms client configs to setup connections
|
|
|
-func setupMQTT(commsCfg *config.ClientConfig, publish bool) mqtt.Client {
|
|
|
+func setupMQTT(cfg *config.ClientConfig, publish bool) mqtt.Client {
|
|
|
opts := mqtt.NewClientOptions()
|
|
|
- server := getServerAddress(commsCfg)
|
|
|
+ server := cfg.Server.Server
|
|
|
opts.AddBroker(server + ":1883") // TODO get the appropriate port of the comms mq server
|
|
|
opts.ClientID = ncutils.MakeRandomString(23) // helps avoid id duplication on broker
|
|
|
opts.SetDefaultPublishHandler(All)
|
|
@@ -213,8 +291,8 @@ func setupMQTT(commsCfg *config.ClientConfig, publish bool) mqtt.Client {
|
|
|
opts.SetOrderMatters(true)
|
|
|
opts.SetResumeSubs(true)
|
|
|
opts.SetConnectionLostHandler(func(c mqtt.Client, e error) {
|
|
|
- logger.Log(0, "detected broker connection lost, running pull for ", commsCfg.Node.Network)
|
|
|
- _, err := Pull(commsCfg.Node.Network, true)
|
|
|
+ logger.Log(0, "detected broker connection lost, running pull for ", cfg.Node.Network)
|
|
|
+ _, err := Pull(cfg.Node.Network, true)
|
|
|
if err != nil {
|
|
|
logger.Log(0, "could not run pull, server unreachable: ", err.Error())
|
|
|
logger.Log(0, "waiting to retry...")
|
|
@@ -227,10 +305,10 @@ func setupMQTT(commsCfg *config.ClientConfig, publish bool) mqtt.Client {
|
|
|
for {
|
|
|
//if after 12 seconds, try a gRPC pull on the last try
|
|
|
if time.Now().After(tperiod) {
|
|
|
- logger.Log(0, "running pull for ", commsCfg.Node.Network)
|
|
|
- _, err := Pull(commsCfg.Node.Network, true)
|
|
|
+ logger.Log(0, "running pull for ", cfg.Node.Network)
|
|
|
+ _, err := Pull(cfg.Node.Network, true)
|
|
|
if err != nil {
|
|
|
- logger.Log(0, "could not run pull, exiting ", commsCfg.Node.Network, " setup: ", err.Error())
|
|
|
+ logger.Log(0, "could not run pull, exiting ", cfg.Node.Network, " setup: ", err.Error())
|
|
|
return client
|
|
|
}
|
|
|
time.Sleep(time.Second)
|
|
@@ -238,10 +316,10 @@ func setupMQTT(commsCfg *config.ClientConfig, publish bool) mqtt.Client {
|
|
|
if token := client.Connect(); token.Wait() && token.Error() != nil {
|
|
|
logger.Log(0, "unable to connect to broker, retrying ...")
|
|
|
if time.Now().After(tperiod) {
|
|
|
- logger.Log(0, "could not connect to broker, exiting ", commsCfg.Node.Network, " setup: ", token.Error().Error())
|
|
|
+ logger.Log(0, "could not connect to broker, exiting ", cfg.Node.Network, " setup: ", token.Error().Error())
|
|
|
if strings.Contains(token.Error().Error(), "connectex") || strings.Contains(token.Error().Error(), "i/o timeout") {
|
|
|
logger.Log(0, "connection issue detected.. pulling and restarting daemon")
|
|
|
- Pull(commsCfg.Node.Network, true)
|
|
|
+ Pull(cfg.Node.Network, true)
|
|
|
daemon.Restart()
|
|
|
}
|
|
|
return client
|
|
@@ -255,8 +333,8 @@ func setupMQTT(commsCfg *config.ClientConfig, publish bool) mqtt.Client {
|
|
|
}
|
|
|
|
|
|
// publishes a message to server to update peers on this peer's behalf
|
|
|
-func publishSignal(commsCfg, nodeCfg *config.ClientConfig, signal byte) error {
|
|
|
- if err := publish(commsCfg, nodeCfg, fmt.Sprintf("signal/%s", nodeCfg.Node.ID), []byte{signal}, 1); err != nil {
|
|
|
+func publishSignal(nodeCfg *config.ClientConfig, signal byte) error {
|
|
|
+ if err := publish(nodeCfg, fmt.Sprintf("signal/%s", nodeCfg.Node.ID), []byte{signal}, 1); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
return nil
|
|
@@ -324,24 +402,6 @@ func getServerAddress(cfg *config.ClientConfig) string {
|
|
|
return server.Address
|
|
|
}
|
|
|
|
|
|
-func getCommsNetworks(networks []string) (map[string]bool, error) {
|
|
|
- var cfg config.ClientConfig
|
|
|
- var response = make(map[string]bool, 1)
|
|
|
- for _, network := range networks {
|
|
|
- cfg.Network = network
|
|
|
- cfg.ReadConfig()
|
|
|
- response[cfg.Node.CommID] = true
|
|
|
- }
|
|
|
- return response, nil
|
|
|
-}
|
|
|
-
|
|
|
-func getCommsCfgByNode(node *models.Node) config.ClientConfig {
|
|
|
- var commsCfg config.ClientConfig
|
|
|
- commsCfg.Network = node.CommID
|
|
|
- commsCfg.ReadConfig()
|
|
|
- return commsCfg
|
|
|
-}
|
|
|
-
|
|
|
// == Message Caches ==
|
|
|
|
|
|
func insert(network, which, cache string) {
|