From f72b8c0da1a714c4ae1007d99c5418a066a0a163 Mon Sep 17 00:00:00 2001 From: "nedim.uka" Date: Mon, 27 Sep 2021 19:23:30 +0200 Subject: [PATCH] Added execute single online presence status message --- README.md | 2 +- config/config.go | 2 +- config/helpers.go | 14 ++++++++++- main.go | 27 ++++++---------------- services/xmppService.go | 51 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 73 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 099cad4..483c297 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,6 @@ Otherwise build the binary like every other golang binary and then copy it. | | | PRESENCE_STATUS_DELAY | NO | 2 min | Delay for online presence message loop | | COMMAND_REPLY_DELAY | NO | 10 sec | Delay for command message reply | -| EXECUTE_SIGLE_STATUS_MESSAGE | NO | false | Send one presence stanza message for clients and terminate| +| EXECUTE_SINGLE_STATUS_MESSAGE | NO | false | Send one presence stanza message for clients and terminate| | | diff --git a/config/config.go b/config/config.go index cf5fb7a..a201d32 100644 --- a/config/config.go +++ b/config/config.go @@ -17,7 +17,7 @@ func Load() { GeneralOptions: GeneralOptions{ PresenceStatusDelay: int64(getEnvInt("PRESENCE_STATUS_DELAY", 2) * 60), CommandReplyDelay: getEnvInt("COMMAND_REPLY_DELAY", 10), - ExecuteSingleStatusMessage: getEnvBool("EXECUTE_SIGLE_STATUS_MESSAGE"), + ExecuteSingleStatusMessage: getEnvBool("EXECUTE_SNIGLE_STATUS_MESSAGE"), }, } } diff --git a/config/helpers.go b/config/helpers.go index 50ea031..e1fcb75 100644 --- a/config/helpers.go +++ b/config/helpers.go @@ -34,7 +34,7 @@ func getEnvInt(key string, defaultValue int) int { } // getEnv extracts the bool value from environment variable -func getEnvBool(env string) bool { +func mustGetEnvBool(env string) bool { envVal := mustGetEnv(env) value, err := strconv.ParseBool(envVal) @@ -45,6 +45,18 @@ func getEnvBool(env string) bool { return value } +// getEnv extracts the bool value from environment variable +func getEnvBool(env string) bool { + envVal := getEnv(env, "false") + value, err := strconv.ParseBool(envVal) + + if err != nil { + value = false + } + + return value +} + // mustGetEnv returns value for give key from environment // if key is not present in environment function will panic func mustGetEnv(key string) string { diff --git a/main.go b/main.go index d8c6e31..74ef792 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,6 @@ package main import ( "log" - "time" appConfig "bitbucket.org/outfrontmedia/rolling-stock-display-simulation-tool/config" xmppService "bitbucket.org/outfrontmedia/rolling-stock-display-simulation-tool/services" @@ -19,25 +18,13 @@ func main() { // Send online presence stanza xmppServiceInstance := xmppService.Instance() - for { - - for _, xmppClient := range xmppServiceInstance.XMPPClients { - - // Send online presence stanza in go rutines - go func(xmppClient xmppService.XMPPClient) { - err := xmppService.SendOnlinePresenceStanza(xmppClient.Client, xmppClient.Config.Jid) - - if err != nil { - log.Printf("There was an error while sending online presence stanza %v", err) - return - } - - }(xmppClient) - - } - // Delay before sending another message - time.Sleep(time.Duration(appConfig.AppConfig.GeneralOptions.PresenceStatusDelay * 1000000000)) - + // Execute send online presence status message for every client and termintate program + if appConfig.AppConfig.GeneralOptions.ExecuteSingleStatusMessage { + xmppServiceInstance.ExecuteSingleStatusMessage() + log.Println("ExecuteSingleStatusMessage finished, online presente status message sent sucessfuly") + return } + // Keep the servbie running + xmppServiceInstance.RunOnlinePresenceStatusIndefinetly() } diff --git a/services/xmppService.go b/services/xmppService.go index 81b4b1c..7d7b51c 100644 --- a/services/xmppService.go +++ b/services/xmppService.go @@ -223,6 +223,57 @@ func SendOnlinePresenceStanza(client *xmpp.Client, jid string) error { return nil } +// RunOnlinePresenceStatusIndefinetly - keep sending online presence status messages indefinetly for all clients with delay +func (xmppService *XMPPService) RunOnlinePresenceStatusIndefinetly() { + + for { + for _, xmppClient := range xmppService.XMPPClients { + + // Send online presence stanza in go rutines + go func(xmppClient XMPPClient) { + err := SendOnlinePresenceStanza(xmppClient.Client, xmppClient.Config.Jid) + + if err != nil { + log.Printf("There was an error while sending online presence stanza %v", err) + return + } + + }(xmppClient) + + } + + // Delay before sending another message + time.Sleep(time.Duration(appConfig.AppConfig.GeneralOptions.PresenceStatusDelay * 1000000000)) + + } + +} + +// ExecuteSingleStatusMessage - Wait for all clients to send online presence stanza messages just once +func (xmppService *XMPPService) ExecuteSingleStatusMessage() { + + var wg sync.WaitGroup + + for _, xmppClient := range xmppService.XMPPClients { + wg.Add(1) + + // Send online presence stanza in go rutines + go func(xmppClient XMPPClient) { + defer wg.Done() + err := SendOnlinePresenceStanza(xmppClient.Client, xmppClient.Config.Jid) + + if err != nil { + log.Printf("There was an error while sending online presence stanza %v", err) + return + } + + }(xmppClient) + } + + // Wait until all clients sent their online prezence staza messages + wg.Wait() +} + func getClientConfigsFromFile(filePath string) ([]ClientCredentials, error) { var clientCredentials []ClientCredentials