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" "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() { func main() {
var configPath, configFile string var configPath, configFile string
@ -119,17 +110,17 @@ func main() {
Password: config.Password, Password: config.Password,
NoTLS: true, NoTLS: true,
StartTLS: true, StartTLS: true,
Debug: false, Debug: true,
} }
// Connect to server. // Connect to server.
client, err = options.NewClient() client, err := options.NewClient()
if err != nil { if err != nil {
log.Fatal("Error: Can't connect to xmpp server: ", err) log.Fatal("Error: Can't connect to xmpp server: ", err)
} }
// Join the MUC // Join the MUC
_, err := client.JoinMUCNoHistory(config.Muc, config.MucNick) _, err = client.JoinMUCNoHistory(config.Muc, config.MucNick)
if err != nil { if err != nil {
log.Fatal("Error: Can't join MUC: ", err) log.Fatal("Error: Can't join MUC: ", err)
} }
@ -137,11 +128,15 @@ func main() {
// Starting goroutine to ping the server every 30 seconds. // Starting goroutine to ping the server every 30 seconds.
go pingServer(client, config.ServerAddress, config.BotJid) 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. // 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. // 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. // Set RefreshTime to 30 seconds if not defined.
if config.RefreshTime == 0 { 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. // 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 { for {
time.Sleep(30 * time.Second) time.Sleep(30 * time.Second)
// Send ping to own MUC participant to check we are still joined. // 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'/>") "get", "<ping xmlns='urn:xmpp:ping'/>")
if err != nil { if err != nil {
log.Fatal("Error: Can't send MUC self ping: ", err) log.Fatal("Error: Can't send MUC self ping: ", err)
} }
pingSent = time.Now() pingID <- id
pingReceived = false pingSent <- time.Now()
pingReceived <- false
pingSentTime := <-pingSent
pingReceivedBool := <-pingReceived
// Check for result IQ as long as there was no reply yet. // 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) time.Sleep(1 * time.Second)
if pingReceived == true { pingReceivedBool := <-pingReceived
if pingReceivedBool == true {
break break
} }
} }
// Quit if no ping reply was received. // Quit if no ping reply was received.
if pingReceived == false { if pingReceivedBool == false {
log.Fatal("MUC not available.") 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. for { // Receive stanzas. ToDo: Receive stanzas continiously without 30s delay.
stanza, err := client.Recv() stanza, err := client.Recv()
if err != nil { if err != nil {
@ -373,12 +374,13 @@ func processStanzas(client *xmpp.Client, muc string, mucNick string, feeds []str
// Reply to pings and disco queries. // Reply to pings and disco queries.
case xmpp.IQ: case xmpp.IQ:
if (v.Type == "error") && (v.ID == id) { ID := <-pingID
if (v.Type == "error") && (v.ID == ID) {
log.Fatal("MUC not available.") log.Fatal("MUC not available.")
} }
if (v.Type == "result") && (v.ID == id) { if (v.Type == "result") && (v.ID == ID) {
pingReceived = true pingReceived <- true
} }
if v.Type == "get" { if v.Type == "get" {