Added multiple client connections in go rutines
This commit is contained in:
26
main.go
26
main.go
@@ -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))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,37 +87,49 @@ 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)
|
||||||
xmppClient := XMPPClient{
|
|
||||||
Config: xmpp.Config{
|
|
||||||
|
|
||||||
TransportConfiguration: xmpp.TransportConfiguration{
|
for _, credential := range clientCredentials {
|
||||||
Address: appConfig.AppConfig.Service.Address + ":" + appConfig.AppConfig.Service.Port,
|
log.Printf("Host %v", credential.Host+":"+strconv.Itoa(credential.Port))
|
||||||
Domain: appConfig.AppConfig.Service.Domain,
|
log.Printf("Jid %v", credential.Jid+"@"+credential.Host)
|
||||||
TLSConfig: &tls.Config{InsecureSkipVerify: true},
|
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,
|
go func() {
|
||||||
Insecure: true,
|
|
||||||
},
|
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
|
// 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 := 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
|
err := client.Send(onlinePresencePacket)
|
||||||
for {
|
log.Printf("Sending online presence stanza: %v", err)
|
||||||
|
return err
|
||||||
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))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getClientConfigsFromFile(filePath string) ([]ClientCredentials, error) {
|
func getClientConfigsFromFile(filePath string) ([]ClientCredentials, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user