Compare commits
14 Commits
initial-co
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| f72b8c0da1 | |||
| 7765cb32a9 | |||
| 2c495aa3e4 | |||
|
|
b53a553382 | ||
| f8f2adaf6f | |||
| b393655a5e | |||
|
|
7452a23ac6 | ||
| b35cb62e1e | |||
| 56d3ec3f90 | |||
| cbb8774382 | |||
|
|
c8627a42b2 | ||
| 272a8c43ac | |||
| c04cee256f | |||
| f693a3c5bb |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,2 +1,2 @@
|
|||||||
.vscode
|
.vscode/
|
||||||
input.json
|
input.json
|
||||||
4
.vscode/settings.json
vendored
4
.vscode/settings.json
vendored
@@ -1,4 +0,0 @@
|
|||||||
{
|
|
||||||
"go.gopath": "/home/nedim/go",
|
|
||||||
"go.useLanguageServer": true
|
|
||||||
}
|
|
||||||
41
README.md
41
README.md
@@ -1,17 +1,48 @@
|
|||||||
# xmpploadtesting
|
# rolling-stock-display-simulation-tool
|
||||||
A tool for testing xmpp servers load
|
|
||||||
|
A tool for connecting virtual devices to the Rolling Stock platform.
|
||||||
|
Devices need to be registered and credentials prepared in a JSON file
|
||||||
|
put in input.json (or specified in a variablle)
|
||||||
|
|
||||||
|
Sample input.json file structure:
|
||||||
|
|
||||||
|
```
|
||||||
|
[
|
||||||
|
{"username": "DEVICE197", "host": "rolling-stock-sandbox2-openfire.onsmartengineering.com", "port": 5222, "ping": 15, "password": "a32c695ccde9"},
|
||||||
|
{"username": "DEVICE198", "host": "rolling-stock-sandbox2-openfire.onsmartengineering.com", "port": 5222, "ping": 15, "password": "c93f60d35d51"},
|
||||||
|
{"username": "DEVICE199", "host": "rolling-stock-sandbox2-openfire.onsmartengineering.com", "port": 5222, "ping": 15, "password": "ddf4d41f846f"},
|
||||||
|
{"username": "DEVICE200", "host": "rolling-stock-sandbox2-openfire.onsmartengineering.com", "port": 5222, "ping": 15, "password": "07fd03b6fce0"}
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
The tool sends regular presence messages (with harcoded dummy versions content)
|
||||||
|
and properly responds with "unsupported command" to any command sent to it.
|
||||||
|
|
||||||
|
|
||||||
|
## Usage:
|
||||||
|
|
||||||
|
```./rolling-stock-display-simulation-tool```
|
||||||
|
|
||||||
|
## Building
|
||||||
|
|
||||||
|
If the target system is linux amd-64 based you can just copy the binary and use it.
|
||||||
|
Otherwise build the binary like every other golang binary and then copy it.
|
||||||
|
|
||||||
|
```go build```
|
||||||
|
|
||||||
|
|
||||||
## Environment variables
|
## Environment variables
|
||||||
|
|
||||||
| Variable | Required | Default Value | Description |
|
| Variable | Required | Default Value | Description |
|
||||||
| ------------------------------------------------- | -------- | --------------------------------- | ---------------------------------------------- |
|
| ------------------------------------------------- | -------- | --------------------------------- | -----------------------------------------------------------|
|
||||||
| **Credentials:** |
|
| **Credentials:** |
|
||||||
| |
|
| |
|
||||||
| CREDENTIALS_FILE_LOCATION | NO | input.json | Database username |
|
| CREDENTIALS_FILE_LOCATION | NO | input.json | Database username |
|
||||||
| |
|
| |
|
||||||
| **GeneralOptions:** |
|
| **GeneralOptions:** |
|
||||||
| |
|
| |
|
||||||
| PRESENCE_STATUS_DELAY | NO | 120000000000 - 2 mins | 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 online presence message loop |
|
| COMMAND_REPLY_DELAY | NO | 10 sec | Delay for command message reply |
|
||||||
|
| EXECUTE_SINGLE_STATUS_MESSAGE | NO | false | Send one presence stanza message for clients and terminate|
|
||||||
| |
|
| |
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +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_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 {
|
||||||
|
|||||||
@@ -23,4 +23,5 @@ type Credentials struct {
|
|||||||
type GeneralOptions struct {
|
type GeneralOptions struct {
|
||||||
PresenceStatusDelay int64
|
PresenceStatusDelay int64
|
||||||
CommandReplyDelay int
|
CommandReplyDelay int
|
||||||
|
ExecuteSingleStatusMessage bool
|
||||||
}
|
}
|
||||||
|
|||||||
8
go.mod
8
go.mod
@@ -1,11 +1,15 @@
|
|||||||
module github.com/xmpploadtesting
|
module bitbucket.org/outfrontmedia/rolling-stock-display-simulation-tool
|
||||||
|
|
||||||
go 1.17
|
go 1.17
|
||||||
|
|
||||||
require gosrc.io/xmpp v0.5.1
|
require (
|
||||||
|
github.com/twinj/uuid v1.0.0
|
||||||
|
gosrc.io/xmpp v0.5.1
|
||||||
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/google/uuid v1.1.1 // indirect
|
github.com/google/uuid v1.1.1 // indirect
|
||||||
|
github.com/twinj/uuid v1.0.0 // indirect
|
||||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 // indirect
|
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 // indirect
|
||||||
nhooyr.io/websocket v1.6.5 // indirect
|
nhooyr.io/websocket v1.6.5 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -57,6 +57,8 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
|
|||||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||||
|
github.com/twinj/uuid v1.0.0 h1:fzz7COZnDrXGTAOHGuUGYd6sG+JMq+AoE7+Jlu0przk=
|
||||||
|
github.com/twinj/uuid v1.0.0/go.mod h1:mMgcE1RHFUFqe5AfiwlINXisXfDGro23fWdPUfOMjRY=
|
||||||
github.com/twitchyliquid64/golang-asm v0.0.0-20190126203739-365674df15fc/go.mod h1:NoCfSFWosfqMqmmD7hApkirIK9ozpHjxRnRxs1l413A=
|
github.com/twitchyliquid64/golang-asm v0.0.0-20190126203739-365674df15fc/go.mod h1:NoCfSFWosfqMqmmD7hApkirIK9ozpHjxRnRxs1l413A=
|
||||||
go.coder.com/go-tools v0.0.0-20190317003359-0c6a35b74a16/go.mod h1:iKV5yK9t+J5nG9O3uF6KYdPEz3dyfMyB15MN1rbQ8Qw=
|
go.coder.com/go-tools v0.0.0-20190317003359-0c6a35b74a16/go.mod h1:iKV5yK9t+J5nG9O3uF6KYdPEz3dyfMyB15MN1rbQ8Qw=
|
||||||
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
|
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
|
||||||
|
|||||||
3
just1.json
Normal file
3
just1.json
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
[
|
||||||
|
{"username": "DEVICE1", "host": "rolling-stock-sandbox2-openfire.onsmartengineering.com", "port": 5222, "ping": 15, "password": "7905efdb805e"}
|
||||||
|
]
|
||||||
29
main.go
29
main.go
@@ -2,10 +2,9 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"time"
|
|
||||||
|
|
||||||
appConfig "github.com/xmpploadtesting/config"
|
appConfig "bitbucket.org/outfrontmedia/rolling-stock-display-simulation-tool/config"
|
||||||
xmppService "github.com/xmpploadtesting/services"
|
xmppService "bitbucket.org/outfrontmedia/rolling-stock-display-simulation-tool/services"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -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
|
|
||||||
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
}(xmppClient)
|
// Keep the servbie running
|
||||||
|
xmppServiceInstance.RunOnlinePresenceStatusIndefinetly()
|
||||||
}
|
|
||||||
// Delay before sending another message
|
|
||||||
time.Sleep(time.Duration(appConfig.AppConfig.GeneralOptions.PresenceStatusDelay))
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
363
services/xmppService.go
Normal file
363
services/xmppService.go
Normal file
@@ -0,0 +1,363 @@
|
|||||||
|
package services
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"math/rand"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
appConfig "bitbucket.org/outfrontmedia/rolling-stock-display-simulation-tool/config"
|
||||||
|
"github.com/twinj/uuid"
|
||||||
|
"gosrc.io/xmpp"
|
||||||
|
"gosrc.io/xmpp/stanza"
|
||||||
|
)
|
||||||
|
|
||||||
|
const rolingStockPresenceMessage = `ROLLING STOCK PRESENCE MESSAGE
|
||||||
|
{
|
||||||
|
"serial": "79101X02Y0029450001CJ2000",
|
||||||
|
"model": "VEN032FSNWM00",
|
||||||
|
"displayOrientation": "left",
|
||||||
|
"macAddress": "1c:c0:e1:42:5a:c4",
|
||||||
|
"node": "v8.16.2",
|
||||||
|
"nodeJS": "v8.16.2",
|
||||||
|
"fbi": "2.10-2ubuntu1",
|
||||||
|
"libgif7": "5.1.4-2ubuntu0.1",
|
||||||
|
"bsEdgeServer": "13.3.9-1",
|
||||||
|
"initopciVersion": "0.4~24",
|
||||||
|
"bspInstallVersion": "1.0~36",
|
||||||
|
"awsConfigsVersion": "2021.21.26-e7c5738",
|
||||||
|
"outGuard": "2021.36.23-dev-028972a",
|
||||||
|
"val": "1.1.11~129",
|
||||||
|
"broadSignPlayer": "13.3.9-1",
|
||||||
|
"displayUnitExternalID": "L-0141-M7-7281-Right-3SM-G02-02-79101X02Y0020040003EJ2001",
|
||||||
|
"privateIPaddress": "10.1.1.94",
|
||||||
|
"linux": {
|
||||||
|
"os": "Ubuntu 18.04.2 LTS",
|
||||||
|
"kernel": "4.18.20+10+opci",
|
||||||
|
"architecture": "x86-64"
|
||||||
|
},
|
||||||
|
"digiID": "digi-00000000-00000000-0040FFFF-FF801D30"
|
||||||
|
}`
|
||||||
|
|
||||||
|
const commandMessageExample = `{"device_jid":"device1@rolling-stock-sandbox2-openfire.onsmartengineering.com",
|
||||||
|
"message_id":"df26acfa-bdd4-40f4-a980-0638bbdb096e-1632473349400",
|
||||||
|
"api_version":1,
|
||||||
|
"from_id":"agent@rolling-stock-sandbox2-openfire.onsmartengineering.com",
|
||||||
|
"commands":[{"name":"demo_command","uuid":"0388ffd0-3f69-4600-b545-ed19f6378a0a",
|
||||||
|
"params":{"user_id":"21","api_version":1,"command":"demo_command","player_id":"8","arg":"senad.uka@toptal.com help"}}]}`
|
||||||
|
|
||||||
|
const commandResponse = `{"api_version": 1,
|
||||||
|
"message_id": "766318f3-d1d3-4359-bf2d-e69d508733f4-1632410186320",
|
||||||
|
"responses": [{"params": {"message": "Unsupported command. Type help for the list of supported commands", "response_code": "SUCCESS"},
|
||||||
|
"uuid": "5a041416-4184-4529-a7d3-7bfdd9fc1739",
|
||||||
|
"name": "demo_command"}],
|
||||||
|
"player_id": "3"}`
|
||||||
|
|
||||||
|
// CommandMessage -
|
||||||
|
type CommandMessage struct {
|
||||||
|
APIVersion int `json:"api_version"`
|
||||||
|
MessageID string `json:"message_id"`
|
||||||
|
Commands []Command `json:"commands"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command -
|
||||||
|
type Command struct {
|
||||||
|
Params CommandParams `json:"params"`
|
||||||
|
UUID string `json:"uuid"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CommandParams -
|
||||||
|
type CommandParams struct {
|
||||||
|
Command string `json:"message"`
|
||||||
|
PlayerID string `json:"player_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CommandMessageResponse -
|
||||||
|
type CommandMessageResponse struct {
|
||||||
|
APIVersion int `json:"api_version"`
|
||||||
|
MessageID string `json:"message_id"`
|
||||||
|
Responses []Response `json:"responses"`
|
||||||
|
PlayerID string `json:"player_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Response -
|
||||||
|
type Response struct {
|
||||||
|
Params ResponseParams `json:"params"`
|
||||||
|
UUID string `json:"uuid"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResponseParams -
|
||||||
|
type ResponseParams struct {
|
||||||
|
Message string `json:"message"`
|
||||||
|
ResponseCode string `json:"response_code"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// XMPPService - struct containing
|
||||||
|
type XMPPService struct {
|
||||||
|
Router *xmpp.Router
|
||||||
|
XMPPClients []XMPPClient
|
||||||
|
}
|
||||||
|
|
||||||
|
// XMPPClient - struct containing client and client configuration
|
||||||
|
type XMPPClient struct {
|
||||||
|
Config xmpp.Config
|
||||||
|
Client *xmpp.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
// ClientCredentials - credentials info for client configuration
|
||||||
|
type ClientCredentials struct {
|
||||||
|
Jid string `json:"username"`
|
||||||
|
Credential string `json:"password"`
|
||||||
|
Host string `json:"host"`
|
||||||
|
Port int `json:"port"`
|
||||||
|
Ping int `json:"ping"`
|
||||||
|
}
|
||||||
|
|
||||||
|
const format = "2006/01/02 15:04:05"
|
||||||
|
|
||||||
|
// Instance - get instance of xmpp service
|
||||||
|
func Instance() *XMPPService {
|
||||||
|
return &xmppService
|
||||||
|
}
|
||||||
|
|
||||||
|
var xmppService XMPPService
|
||||||
|
|
||||||
|
// Init Initialise XMPP servie, and router
|
||||||
|
func Init() {
|
||||||
|
|
||||||
|
xmppService = XMPPService{
|
||||||
|
Router: xmpp.NewRouter(),
|
||||||
|
}
|
||||||
|
xmppService.Router.HandleFunc("message", handleMessage)
|
||||||
|
|
||||||
|
//Get Client credentials from file
|
||||||
|
clientCredentials, err := getClientConfigsFromFile(appConfig.AppConfig.Credentials.CredentialsFileLocation)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Unable to load credentials from file path %v, ERROR : %v TERMINATING", appConfig.AppConfig.Credentials.CredentialsFileLocation, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Client credentials length %v", len(clientCredentials))
|
||||||
|
|
||||||
|
xmppService.XMPPClients = make([]XMPPClient, 0)
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
|
for _, credential := range clientCredentials {
|
||||||
|
wg.Add(1)
|
||||||
|
log.Printf("Host %v", credential.Host+":"+strconv.Itoa(credential.Port))
|
||||||
|
log.Printf("Jid %v", credential.Jid+"@"+credential.Host)
|
||||||
|
log.Printf("Port %v", credential.Port)
|
||||||
|
log.Printf("Credential %v", credential.Credential)
|
||||||
|
|
||||||
|
xmppClient := XMPPClient{
|
||||||
|
Config: xmpp.Config{
|
||||||
|
|
||||||
|
TransportConfiguration: xmpp.TransportConfiguration{
|
||||||
|
Address: credential.Host + ":" + strconv.Itoa(credential.Port),
|
||||||
|
Domain: credential.Host,
|
||||||
|
TLSConfig: &tls.Config{InsecureSkipVerify: true},
|
||||||
|
},
|
||||||
|
Jid: credential.Jid + "@" + credential.Host,
|
||||||
|
Credential: xmpp.Password(credential.Credential),
|
||||||
|
StreamLogger: os.Stdout,
|
||||||
|
Insecure: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
statusMesageDelay := int64(rand.Intn(appConfig.AppConfig.GeneralOptions.CommandReplyDelay))
|
||||||
|
log.Printf("Delaying client connection for: %v secconds", statusMesageDelay)
|
||||||
|
// Delay For two seccond to allow all clients to connect
|
||||||
|
time.Sleep(time.Duration(statusMesageDelay * 1000000000))
|
||||||
|
|
||||||
|
client, err := xmpp.NewClient(&xmppClient.Config, xmppService.Router, errorHandler)
|
||||||
|
|
||||||
|
// Client connection
|
||||||
|
if err := client.Connect(); err != nil {
|
||||||
|
msg := fmt.Sprintf("XMPP connection failed: %v", err)
|
||||||
|
|
||||||
|
fmt.Printf("Failed to connect to server. Exiting... %v", msg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Unable to initialise client for %v", xmppClient.Config.Jid)
|
||||||
|
}
|
||||||
|
xmppClient.Client = client
|
||||||
|
xmppService.XMPPClients = append(xmppService.XMPPClients, xmppClient)
|
||||||
|
}()
|
||||||
|
|
||||||
|
}
|
||||||
|
// Wait untill all client connections are open sucessfuly
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// SendOnlinePresenceStanza - send online presence stnza with data, to server
|
||||||
|
func SendOnlinePresenceStanza(client *xmpp.Client, jid string) error {
|
||||||
|
|
||||||
|
statusMesageDelay := int64(rand.Intn(appConfig.AppConfig.GeneralOptions.CommandReplyDelay))
|
||||||
|
log.Printf("Delaying online stanza presence message for: %v, secconds", statusMesageDelay*1000000000)
|
||||||
|
// Delay For two seccond to allow all clients to connect
|
||||||
|
time.Sleep(time.Duration(statusMesageDelay * 1000000000))
|
||||||
|
|
||||||
|
onlinePresencePacket := stanza.NewPresence(stanza.Attrs{From: jid, Type: stanza.StanzaType(stanza.PresenceShowChat)})
|
||||||
|
onlinePresencePacket.Status = rolingStockPresenceMessage
|
||||||
|
|
||||||
|
err := client.Send(onlinePresencePacket)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("<%v> sent online status", jid)
|
||||||
|
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
|
||||||
|
|
||||||
|
f, err := os.Open(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return clientCredentials, err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
byteValue, err := ioutil.ReadAll(f)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return clientCredentials, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(byteValue, &clientCredentials)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return clientCredentials, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return clientCredentials, nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleMessage(s xmpp.Sender, p stanza.Packet) {
|
||||||
|
|
||||||
|
commandMessageResponse := CommandMessageResponse{}
|
||||||
|
err := json.Unmarshal([]byte(commandResponse), &commandMessageResponse)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("unable to unmarshal Command Meassage Response: %v", commandResponse)
|
||||||
|
}
|
||||||
|
|
||||||
|
msg, ok := p.(stanza.Message)
|
||||||
|
if !ok {
|
||||||
|
log.Printf(" message not OK")
|
||||||
|
_, _ = fmt.Fprintf(os.Stdout, "Ignoring packet: %T\n", p)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(msg.Body) == 0 {
|
||||||
|
log.Printf("<%v> received empty message, and will not respond", msg.To)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
commandMessage := CommandMessage{}
|
||||||
|
err = json.Unmarshal([]byte(msg.Body), &commandMessage)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("<%v> unable to unmarshal Command Meassage: %v", msg.To, msg.Body)
|
||||||
|
}
|
||||||
|
if len(commandMessageResponse.Responses) > 0 || len(commandMessage.Commands) > 0 {
|
||||||
|
// messageIDUUID := uuid.NewV4().String()
|
||||||
|
commandMessageUUID := uuid.NewV4().String()
|
||||||
|
commandMessageResponse.Responses[0].UUID = commandMessageUUID
|
||||||
|
commandMessageResponse.PlayerID = commandMessage.Commands[0].Params.PlayerID
|
||||||
|
commandMessageResponse.MessageID = commandMessage.MessageID
|
||||||
|
|
||||||
|
} else {
|
||||||
|
log.Printf("<%v> There are no commands inside command message : %v", msg.To, commandMessage)
|
||||||
|
}
|
||||||
|
|
||||||
|
m, err := json.Marshal(commandMessageResponse)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("<%v> unable to marshal Command Meassage response : %v", msg.To, commandMessageResponse)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("<%v> received following message: %v", msg.To, msg.Body)
|
||||||
|
commandReplyDelay := int64(rand.Intn(appConfig.AppConfig.GeneralOptions.CommandReplyDelay))
|
||||||
|
log.Printf("Waiting FOR: %v", commandReplyDelay*1000000000)
|
||||||
|
// Delay For two seccond to allow all clients to connect
|
||||||
|
time.Sleep(time.Duration(commandReplyDelay * 1000000000))
|
||||||
|
|
||||||
|
reply := stanza.Message{Attrs: stanza.Attrs{To: msg.From}, Body: string(m)}
|
||||||
|
err = s.Send(reply)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error sending to %v message %v", msg.From, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func errorHandler(err error) {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
}
|
||||||
@@ -1,218 +0,0 @@
|
|||||||
package services
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
|
||||||
"math/rand"
|
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
appConfig "github.com/xmpploadtesting/config"
|
|
||||||
"gosrc.io/xmpp"
|
|
||||||
"gosrc.io/xmpp/stanza"
|
|
||||||
"mellium.im/xmpp"
|
|
||||||
)
|
|
||||||
|
|
||||||
const rolingStockPresenceMessage = `ROLLING STOCK PRESENCE MESSAGE
|
|
||||||
{
|
|
||||||
"serial": "79101X02Y0029450001CJ2000",
|
|
||||||
"model": "VEN032FSNWM00",
|
|
||||||
"displayOrientation": "left",
|
|
||||||
"macAddress": "1c:c0:e1:42:5a:c4",
|
|
||||||
"node": "v8.16.2",
|
|
||||||
"nodeJS": "v8.16.2",
|
|
||||||
"fbi": "2.10-2ubuntu1",
|
|
||||||
"libgif7": "5.1.4-2ubuntu0.1",
|
|
||||||
"bsEdgeServer": "13.3.9-1",
|
|
||||||
"initopciVersion": "0.4~24",
|
|
||||||
"bspInstallVersion": "1.0~36",
|
|
||||||
"awsConfigsVersion": "2021.21.26-e7c5738",
|
|
||||||
"outGuard": "2021.36.23-dev-028972a",
|
|
||||||
"val": "1.1.11~129",
|
|
||||||
"broadSignPlayer": "13.3.9-1",
|
|
||||||
"displayUnitExternalID": "L-0141-M7-7281-Right-3SM-G02-02-79101X02Y0020040003EJ2001",
|
|
||||||
"privateIPaddress": "10.1.1.94",
|
|
||||||
"linux": {
|
|
||||||
"os": "Ubuntu 18.04.2 LTS",
|
|
||||||
"kernel": "4.18.20+10+opci",
|
|
||||||
"architecture": "x86-64"
|
|
||||||
},
|
|
||||||
"digiID": "digi-00000000-00000000-0040FFFF-FF801D30"
|
|
||||||
}`
|
|
||||||
|
|
||||||
// XMPPService - struct containing
|
|
||||||
type XMPPService struct {
|
|
||||||
Router *xmpp.Router
|
|
||||||
XMPPClients []XMPPClient
|
|
||||||
}
|
|
||||||
|
|
||||||
// XMPPClient - struct containing client and client configuration
|
|
||||||
type XMPPClient struct {
|
|
||||||
Config xmpp.Config
|
|
||||||
Client *xmpp.Client
|
|
||||||
}
|
|
||||||
|
|
||||||
// ClientCredentials - credentials info for client configuration
|
|
||||||
type ClientCredentials struct {
|
|
||||||
Jid string `json:"username"`
|
|
||||||
Credential string `json:"password"`
|
|
||||||
Host string `json:"host"`
|
|
||||||
Port int `json:"port"`
|
|
||||||
Ping int `json:"ping"`
|
|
||||||
}
|
|
||||||
|
|
||||||
const format = "2006/01/02 15:04:05"
|
|
||||||
|
|
||||||
// Instance - get instance of xmpp service
|
|
||||||
func Instance() *XMPPService {
|
|
||||||
return &xmppService
|
|
||||||
}
|
|
||||||
|
|
||||||
var xmppService XMPPService
|
|
||||||
|
|
||||||
// Init Initialise XMPP servie, and router
|
|
||||||
func Init() {
|
|
||||||
|
|
||||||
xmppService = XMPPService{
|
|
||||||
Router: xmpp.NewRouter(),
|
|
||||||
}
|
|
||||||
xmppService.Router.HandleFunc("message", handleMessage)
|
|
||||||
|
|
||||||
//Get Client credentials from file
|
|
||||||
clientCredentials, err := getClientConfigsFromFile(appConfig.AppConfig.Credentials.CredentialsFileLocation)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Unable to load credentials from file path %v, ERROR : %v TERMINATING", appConfig.AppConfig.Credentials.CredentialsFileLocation, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("Client credentials length %v", len(clientCredentials))
|
|
||||||
|
|
||||||
xmppService.XMPPClients = make([]XMPPClient, 0)
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
|
|
||||||
for _, credential := range clientCredentials {
|
|
||||||
wg.Add(1)
|
|
||||||
log.Printf("Host %v", credential.Host+":"+strconv.Itoa(credential.Port))
|
|
||||||
log.Printf("Jid %v", credential.Jid+"@"+credential.Host)
|
|
||||||
log.Printf("Port %v", credential.Port)
|
|
||||||
log.Printf("Credential %v", credential.Credential)
|
|
||||||
|
|
||||||
xmppClient := XMPPClient{
|
|
||||||
Config: xmpp.Config{
|
|
||||||
|
|
||||||
TransportConfiguration: xmpp.TransportConfiguration{
|
|
||||||
Address: credential.Host + ":" + strconv.Itoa(credential.Port),
|
|
||||||
Domain: credential.Host,
|
|
||||||
// TLSConfig: &tls.Config{InsecureSkipVerify: false},
|
|
||||||
},
|
|
||||||
Jid: credential.Jid + "@" + credential.Host,
|
|
||||||
Credential: xmpp.Password(credential.Credential),
|
|
||||||
StreamLogger: os.Stdout,
|
|
||||||
Insecure: true,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer wg.Done()
|
|
||||||
|
|
||||||
client, err := xmpp.NewClient(&xmppClient.Config, xmppService.Router, errorHandler)
|
|
||||||
|
|
||||||
// Client connection
|
|
||||||
if err := client.Connect(); err != nil {
|
|
||||||
msg := fmt.Sprintf("XMPP connection failed: %v", err)
|
|
||||||
|
|
||||||
fmt.Printf("Failed to connect to server. Exiting... %v", msg)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Unable to initialise client for %v", xmppClient.Config.Jid)
|
|
||||||
}
|
|
||||||
xmppClient.Client = client
|
|
||||||
xmppService.XMPPClients = append(xmppService.XMPPClients, xmppClient)
|
|
||||||
}()
|
|
||||||
|
|
||||||
}
|
|
||||||
// // Delay For two seccond to allow all clients to connect
|
|
||||||
// time.Sleep(time.Duration(2000000000))
|
|
||||||
// Wait untill all client connections are open sucessfuly
|
|
||||||
wg.Wait()
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// SendOnlinePresenceStanza - send online presence stnza with data, to server
|
|
||||||
func SendOnlinePresenceStanza(client *xmpp.Client, jid string) error {
|
|
||||||
onlinePresencePacket := stanza.NewPresence(stanza.Attrs{From: jid, Type: stanza.StanzaType(stanza.PresenceShowChat)})
|
|
||||||
onlinePresencePacket.Status = rolingStockPresenceMessage
|
|
||||||
|
|
||||||
err := client.Send(onlinePresencePacket)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("<%v> sent online status", jid)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getClientConfigsFromFile(filePath string) ([]ClientCredentials, error) {
|
|
||||||
|
|
||||||
var clientCredentials []ClientCredentials
|
|
||||||
|
|
||||||
f, err := os.Open(filePath)
|
|
||||||
if err != nil {
|
|
||||||
return clientCredentials, err
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
byteValue, err := ioutil.ReadAll(f)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return clientCredentials, err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = json.Unmarshal(byteValue, &clientCredentials)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return clientCredentials, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return clientCredentials, nil
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleMessage(s xmpp.Sender, p stanza.Packet) {
|
|
||||||
|
|
||||||
msg, ok := p.(stanza.Message)
|
|
||||||
if !ok {
|
|
||||||
log.Printf(" message not OK")
|
|
||||||
_, _ = fmt.Fprintf(os.Stdout, "Ignoring packet: %T\n", p)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(msg.Body) == 0 {
|
|
||||||
log.Printf("<%v> received empty message, and will not respond", msg.To)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("<%v> received following message: %v", msg.To, msg.Body)
|
|
||||||
commandReplyDelay := int64(rand.Intn(appConfig.AppConfig.GeneralOptions.CommandReplyDelay))
|
|
||||||
log.Printf("Waiting FOR: %v", commandReplyDelay*1000000000)
|
|
||||||
// Delay For two second to allow all clients to connect
|
|
||||||
time.Sleep(time.Duration(commandReplyDelay * 1000000000))
|
|
||||||
|
|
||||||
reply := stanza.Message{Attrs: stanza.Attrs{To: msg.From}, Body: `{"status": "OK"}`}
|
|
||||||
err := s.Send(reply)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error sending to %v message %v", msg.From, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func errorHandler(err error) {
|
|
||||||
fmt.Println(err.Error())
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user