Added multiple client connections in go rutines

This commit is contained in:
2021-09-16 10:18:44 +02:00
parent 9095638867
commit 1d2a8068dd
2 changed files with 69 additions and 38 deletions

26
main.go
View File

@@ -1,6 +1,9 @@
package main
import (
"log"
"time"
appConfig "github.com/xmpploadtesting/config"
xmppService "github.com/xmpploadtesting/services"
)
@@ -15,5 +18,26 @@ func main() {
// Send online presence stanza
xmppServiceInstance := xmppService.Instance()
xmppService.SendOnlinePresenceStanza(xmppServiceInstance.XMPPClients[0].Client, xmppServiceInstance.XMPPClients[0].Config.Jid)
for {
for _, xmppClient := range xmppServiceInstance.XMPPClients {
// Send online presence stanza in go rutines
go func(xmppClient xmppService.XMPPClient) {
log.Printf("online presence stanza FOR %v", xmppClient.Config.Jid)
err := xmppService.SendOnlinePresenceStanza(xmppClient.Client, xmppClient.Config.Jid)
if err != nil {
log.Printf("There was an error while sending online presence stanza %v", err)
}
}(xmppClient)
}
// Delay before sending another message
time.Sleep(time.Duration(appConfig.AppConfig.GeneralOptions.DelayBetweenMassages))
}
}

View File

@@ -7,7 +7,7 @@ import (
"io/ioutil"
"log"
"os"
"time"
"strconv"
appConfig "github.com/xmpploadtesting/config"
"gosrc.io/xmpp"
@@ -71,7 +71,7 @@ var xmppService XMPPService
// Init Initialise XMPP servie, and router
func Init() {
// TODO load configs and init clients from the config file
xmppService = XMPPService{
Router: xmpp.NewRouter(),
}
@@ -87,37 +87,49 @@ func Init() {
log.Printf("Client credentials length %v", len(clientCredentials))
xmppService.XMPPClients = make([]XMPPClient, 0)
xmppClient := XMPPClient{
Config: xmpp.Config{
TransportConfiguration: xmpp.TransportConfiguration{
Address: appConfig.AppConfig.Service.Address + ":" + appConfig.AppConfig.Service.Port,
Domain: appConfig.AppConfig.Service.Domain,
TLSConfig: &tls.Config{InsecureSkipVerify: true},
for _, credential := range clientCredentials {
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,
},
Jid: "admin@localhost",
Credential: xmpp.Password("openadmin123"),
StreamLogger: os.Stdout,
Insecure: true,
},
}
go func() {
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)
}()
}
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)
}
// SendOnlinePresenceStanza - send online presence stnza with data, to server
@@ -125,14 +137,9 @@ func SendOnlinePresenceStanza(client *xmpp.Client, jid string) error {
onlinePresencePacket := stanza.NewPresence(stanza.Attrs{From: jid, Type: stanza.StanzaType(stanza.PresenceShowChat)})
onlinePresencePacket.Status = rolingStockPresenceMessage
//TODO All clients should send messages at once
for {
err := client.Send(onlinePresencePacket)
log.Printf("Sending online presence stanza: %v", err)
// Delay before sending another message
time.Sleep(time.Duration(appConfig.AppConfig.GeneralOptions.DelayBetweenMassages))
}
err := client.Send(onlinePresencePacket)
log.Printf("Sending online presence stanza: %v", err)
return err
}
func getClientConfigsFromFile(filePath string) ([]ClientCredentials, error) {