Added execute single online presence status message
This commit is contained in:
@@ -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 |
|
| PRESENCE_STATUS_DELAY | NO | 2 min | Delay for online presence message loop |
|
||||||
| COMMAND_REPLY_DELAY | NO | 10 sec | Delay for command message reply |
|
| 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|
|
||||||
| |
|
| |
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ func Load() {
|
|||||||
GeneralOptions: GeneralOptions{
|
GeneralOptions: GeneralOptions{
|
||||||
PresenceStatusDelay: int64(getEnvInt("PRESENCE_STATUS_DELAY", 2) * 60),
|
PresenceStatusDelay: int64(getEnvInt("PRESENCE_STATUS_DELAY", 2) * 60),
|
||||||
CommandReplyDelay: getEnvInt("COMMAND_REPLY_DELAY", 10),
|
CommandReplyDelay: getEnvInt("COMMAND_REPLY_DELAY", 10),
|
||||||
ExecuteSingleStatusMessage: getEnvBool("EXECUTE_SIGLE_STATUS_MESSAGE"),
|
ExecuteSingleStatusMessage: getEnvBool("EXECUTE_SNIGLE_STATUS_MESSAGE"),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ func getEnvInt(key string, defaultValue int) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// getEnv extracts the bool value from environment variable
|
// getEnv extracts the bool value from environment variable
|
||||||
func getEnvBool(env string) bool {
|
func mustGetEnvBool(env string) bool {
|
||||||
envVal := mustGetEnv(env)
|
envVal := mustGetEnv(env)
|
||||||
value, err := strconv.ParseBool(envVal)
|
value, err := strconv.ParseBool(envVal)
|
||||||
|
|
||||||
@@ -45,6 +45,18 @@ func getEnvBool(env string) bool {
|
|||||||
return value
|
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
|
// mustGetEnv returns value for give key from environment
|
||||||
// if key is not present in environment function will panic
|
// if key is not present in environment function will panic
|
||||||
func mustGetEnv(key string) string {
|
func mustGetEnv(key string) string {
|
||||||
|
|||||||
27
main.go
27
main.go
@@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"time"
|
|
||||||
|
|
||||||
appConfig "bitbucket.org/outfrontmedia/rolling-stock-display-simulation-tool/config"
|
appConfig "bitbucket.org/outfrontmedia/rolling-stock-display-simulation-tool/config"
|
||||||
xmppService "bitbucket.org/outfrontmedia/rolling-stock-display-simulation-tool/services"
|
xmppService "bitbucket.org/outfrontmedia/rolling-stock-display-simulation-tool/services"
|
||||||
@@ -19,25 +18,13 @@ func main() {
|
|||||||
// Send online presence stanza
|
// Send online presence stanza
|
||||||
xmppServiceInstance := xmppService.Instance()
|
xmppServiceInstance := xmppService.Instance()
|
||||||
|
|
||||||
for {
|
// Execute send online presence status message for every client and termintate program
|
||||||
|
if appConfig.AppConfig.GeneralOptions.ExecuteSingleStatusMessage {
|
||||||
for _, xmppClient := range xmppServiceInstance.XMPPClients {
|
xmppServiceInstance.ExecuteSingleStatusMessage()
|
||||||
|
log.Println("ExecuteSingleStatusMessage finished, online presente status message sent sucessfuly")
|
||||||
// Send online presence stanza in go rutines
|
return
|
||||||
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))
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Keep the servbie running
|
||||||
|
xmppServiceInstance.RunOnlinePresenceStatusIndefinetly()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -223,6 +223,57 @@ func SendOnlinePresenceStanza(client *xmpp.Client, jid string) error {
|
|||||||
return nil
|
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) {
|
func getClientConfigsFromFile(filePath string) ([]ClientCredentials, error) {
|
||||||
|
|
||||||
var clientCredentials []ClientCredentials
|
var clientCredentials []ClientCredentials
|
||||||
|
|||||||
Reference in New Issue
Block a user