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 package main
import ( import (
"log"
"time"
appConfig "github.com/xmpploadtesting/config" appConfig "github.com/xmpploadtesting/config"
xmppService "github.com/xmpploadtesting/services" xmppService "github.com/xmpploadtesting/services"
) )
@@ -15,5 +18,26 @@ func main() {
// Send online presence stanza // Send online presence stanza
xmppServiceInstance := xmppService.Instance() 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" "io/ioutil"
"log" "log"
"os" "os"
"time" "strconv"
appConfig "github.com/xmpploadtesting/config" appConfig "github.com/xmpploadtesting/config"
"gosrc.io/xmpp" "gosrc.io/xmpp"
@@ -71,7 +71,7 @@ var xmppService XMPPService
// Init Initialise XMPP servie, and router // Init Initialise XMPP servie, and router
func Init() { func Init() {
// TODO load configs and init clients from the config file
xmppService = XMPPService{ xmppService = XMPPService{
Router: xmpp.NewRouter(), Router: xmpp.NewRouter(),
} }
@@ -87,21 +87,30 @@ func Init() {
log.Printf("Client credentials length %v", len(clientCredentials)) log.Printf("Client credentials length %v", len(clientCredentials))
xmppService.XMPPClients = make([]XMPPClient, 0) xmppService.XMPPClients = make([]XMPPClient, 0)
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{ xmppClient := XMPPClient{
Config: xmpp.Config{ Config: xmpp.Config{
TransportConfiguration: xmpp.TransportConfiguration{ TransportConfiguration: xmpp.TransportConfiguration{
Address: appConfig.AppConfig.Service.Address + ":" + appConfig.AppConfig.Service.Port, Address: credential.Host + ":" + strconv.Itoa(credential.Port),
Domain: appConfig.AppConfig.Service.Domain, Domain: credential.Host,
TLSConfig: &tls.Config{InsecureSkipVerify: true}, TLSConfig: &tls.Config{InsecureSkipVerify: true},
}, },
Jid: "admin@localhost", Jid: credential.Jid + "@" + credential.Host,
Credential: xmpp.Password("openadmin123"), Credential: xmpp.Password(credential.Credential),
StreamLogger: os.Stdout, StreamLogger: os.Stdout,
Insecure: true, Insecure: true,
}, },
} }
go func() {
client, err := xmpp.NewClient(&xmppClient.Config, xmppService.Router, errorHandler) client, err := xmpp.NewClient(&xmppClient.Config, xmppService.Router, errorHandler)
// Client connection // Client connection
@@ -117,6 +126,9 @@ func Init() {
} }
xmppClient.Client = client xmppClient.Client = client
xmppService.XMPPClients = append(xmppService.XMPPClients, xmppClient) xmppService.XMPPClients = append(xmppService.XMPPClients, xmppClient)
}()
}
} }
@@ -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 := stanza.NewPresence(stanza.Attrs{From: jid, Type: stanza.StanzaType(stanza.PresenceShowChat)})
onlinePresencePacket.Status = rolingStockPresenceMessage onlinePresencePacket.Status = rolingStockPresenceMessage
//TODO All clients should send messages at once
for {
err := client.Send(onlinePresencePacket) err := client.Send(onlinePresencePacket)
log.Printf("Sending online presence stanza: %v", err) log.Printf("Sending online presence stanza: %v", err)
// Delay before sending another message return err
time.Sleep(time.Duration(appConfig.AppConfig.GeneralOptions.DelayBetweenMassages))
}
} }
func getClientConfigsFromFile(filePath string) ([]ClientCredentials, error) { func getClientConfigsFromFile(filePath string) ([]ClientCredentials, error) {