Reworked the ping functions and stanza processing.

This commit is contained in:
Martin Dosch 2018-08-08 11:46:58 +02:00
parent 3bdfea029e
commit 5a5014d449

View file

@ -16,9 +16,17 @@ 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 err error
var configPath, configFile string
type configuration struct {
@ -132,6 +140,9 @@ func main() {
// Starting goroutine to ping the MUC every 30 seconds.
go pingMUC(client, config.BotJid, config.Muc, config.MucNick)
// Starting goroutine to process received stanzas.
go processStanzas(client)
for {
// Check all configured feeds for new articles and send
// new articles to configured MUC.
@ -163,18 +174,45 @@ func pingMUC(client *xmpp.Client, botJid string, Muc string, MucNick string) {
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(err)
}
pingSent := time.Now()
pingReceived := false
pingSent = time.Now()
pingReceived = false
// Check for result IQ as long as there was no reply yet.
for (time.Since(pingSent).Seconds() < 10.0) && (pingReceived == false) {
// Receive stanzas. ToDo: Receive stanzas continiously without 30s delay.
time.Sleep(1 * time.Second)
if pingReceived == true {
break
}
}
// Quit if no ping reply was received.
if pingReceived == false {
log.Fatal("MUC not available.")
}
}
}
// Send a ping to the server every 30 seconds to check if the connection is still alive.
func pingServer(client *xmpp.Client, server string, botJid string) {
for {
time.Sleep(30 * time.Second)
// Send ping to server to check if connection still exists.
err := client.PingC2S(botJid, strings.Split(server, ":")[0])
if err != nil {
log.Fatal(err)
}
}
}
func processStanzas(client *xmpp.Client) {
for { // Receive stanzas. ToDo: Receive stanzas continiously without 30s delay.
stanza, err := client.Recv()
if err != nil {
log.Fatal(err)
@ -202,11 +240,17 @@ func pingMUC(client *xmpp.Client, botJid string, Muc string, MucNick string) {
if err != nil {
log.Fatal(err)
}
} else if strings.Contains(string(v.Query), "<ping xmlns='urn:xmpp:ping'/>") == true {
// Reply to pings.
_, err := client.RawInformation(client.JID(), v.From, v.ID, "result", "")
if err != nil {
log.Fatal(err)
}
} else {
// Send error replies for all other IQs.
_, err := client.RawInformation(client.JID(), v.From, v.ID, "error", string(v.Query)+
"<error type='cancel'><feature-not-implemented "+
"xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/><error/>")
_, err := client.RawInformation(client.JID(), v.From, v.ID, "error",
"<error type='cancel'><service-unavailable "+
"xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>")
if err != nil {
log.Fatal(err)
}
@ -217,22 +261,4 @@ func pingMUC(client *xmpp.Client, botJid string, Muc string, MucNick string) {
break
}
}
// Quit if no ping reply was received.
if pingReceived == false {
log.Fatal("MUC not available.")
}
}
}
// Send a ping to the server every 30 seconds to check if the connection is still alive.
func pingServer(client *xmpp.Client, server string, botJid string) {
for {
time.Sleep(30 * time.Second)
// Send ping to server to check if connection still exists.
err := client.PingC2S(botJid, strings.Split(server, ":")[0])
if err != nil {
log.Fatal(err)
}
}
}