First experiments with channels (failed hard as expected :D).

This commit is contained in:
Martin Dosch 2019-05-31 21:14:49 +02:00
parent 8ac7a60770
commit 0f132cc972

View file

@ -16,15 +16,6 @@ import (
"github.com/mattn/go-xmpp"
)
// Variables defined globally as they are used by functions pingMUC
// and processStanzas.
var (
id string
err error
pingSent time.Time
pingReceived bool
)
func main() {
var configPath, configFile string
@ -119,17 +110,17 @@ func main() {
Password: config.Password,
NoTLS: true,
StartTLS: true,
Debug: false,
Debug: true,
}
// Connect to server.
client, err = options.NewClient()
client, err := options.NewClient()
if err != nil {
log.Fatal("Error: Can't connect to xmpp server: ", err)
}
// Join the MUC
_, err := client.JoinMUCNoHistory(config.Muc, config.MucNick)
_, err = client.JoinMUCNoHistory(config.Muc, config.MucNick)
if err != nil {
log.Fatal("Error: Can't join MUC: ", err)
}
@ -137,11 +128,15 @@ func main() {
// Starting goroutine to ping the server every 30 seconds.
go pingServer(client, config.ServerAddress, config.BotJid)
pingID := make(chan string)
pingSent := make(chan time.Time)
pingReceived := make(chan bool)
// Starting goroutine to ping the MUC every 30 seconds.
go pingMUC(client, config.BotJid, config.Muc, config.MucNick)
go pingMUC(client, config.BotJid, config.Muc, config.MucNick, pingID, pingSent, pingReceived)
// Starting goroutine to process received stanzas.
go processStanzas(client, config.Muc, config.MucNick, config.Feeds, config.Quiet, config.Contact)
go processStanzas(client, config.Muc, config.MucNick, config.Feeds, config.Quiet, config.Contact,
pingID, pingSent, pingReceived)
// Set RefreshTime to 30 seconds if not defined.
if config.RefreshTime == 0 {
@ -174,30 +169,35 @@ func main() {
}
// Send a ping every 30 seconds after last successful ping to check if the MUC is still available.
func pingMUC(client *xmpp.Client, botJid string, muc string, mucNick string) {
func pingMUC(client *xmpp.Client, botJid string, muc string, mucNick string, pingID chan string,
pingSent chan time.Time, pingReceived chan bool) {
for {
time.Sleep(30 * time.Second)
// Send ping to own MUC participant to check we are still joined.
id, err = client.RawInformation(botJid, muc+"/"+mucNick, sid.Id(),
id, err := client.RawInformation(botJid, muc+"/"+mucNick, sid.Id(),
"get", "<ping xmlns='urn:xmpp:ping'/>")
if err != nil {
log.Fatal("Error: Can't send MUC self ping: ", err)
}
pingSent = time.Now()
pingReceived = false
pingID <- id
pingSent <- time.Now()
pingReceived <- false
pingSentTime := <-pingSent
pingReceivedBool := <-pingReceived
// Check for result IQ as long as there was no reply yet.
for (time.Since(pingSent).Seconds() < 10.0) && (pingReceived == false) {
for (time.Since(pingSentTime).Seconds() < 10.0) && (pingReceivedBool == false) {
time.Sleep(1 * time.Second)
if pingReceived == true {
pingReceivedBool := <-pingReceived
if pingReceivedBool == true {
break
}
}
// Quit if no ping reply was received.
if pingReceived == false {
if pingReceivedBool == false {
log.Fatal("MUC not available.")
}
}
@ -216,7 +216,8 @@ func pingServer(client *xmpp.Client, server string, botJid string) {
}
}
func processStanzas(client *xmpp.Client, muc string, mucNick string, feeds []string, quiet bool, contact string) {
func processStanzas(client *xmpp.Client, muc string, mucNick string, feeds []string, quiet bool,
contact string, pingID chan string, pingSent chan time.Time, pingReceived chan bool) {
for { // Receive stanzas. ToDo: Receive stanzas continiously without 30s delay.
stanza, err := client.Recv()
if err != nil {
@ -373,12 +374,13 @@ func processStanzas(client *xmpp.Client, muc string, mucNick string, feeds []str
// Reply to pings and disco queries.
case xmpp.IQ:
if (v.Type == "error") && (v.ID == id) {
ID := <-pingID
if (v.Type == "error") && (v.ID == ID) {
log.Fatal("MUC not available.")
}
if (v.Type == "result") && (v.ID == id) {
pingReceived = true
if (v.Type == "result") && (v.ID == ID) {
pingReceived <- true
}
if v.Type == "get" {