Cleanup and add test for IOT control set parsing

This commit is contained in:
Mickael Remond 2018-02-13 23:04:13 +01:00
parent d6bedfb033
commit 07b0d2d14d
No known key found for this signature in database
GPG key ID: E6F6045D79965AA3
6 changed files with 38 additions and 32 deletions

View file

@ -40,11 +40,11 @@ func main() {
for packet := range client.Recv() {
switch packet := packet.(type) {
case *xmpp.Message:
processMessage(client, p, packet)
case *xmpp.IQ:
processIq(client, p, packet)
case *xmpp.Presence:
case xmpp.Message:
processMessage(client, p, &packet)
case xmpp.IQ:
processIq(client, p, &packet)
case xmpp.Presence:
// Do nothing with received presence
default:
fmt.Fprintf(os.Stdout, "Ignoring packet: %T\n", packet)
@ -77,7 +77,7 @@ func processIq(client *xmpp.Client, p *mpg123.Player, packet *xmpp.IQ) {
playSCURL(p, url)
setResponse := new(iot.ControlSetResponse)
reply := xmpp.IQ{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "result", Id: packet.Id}, Payload: []xmpp.IQPayload{setResponse}}
client.SendRaw(reply.XMPPFormat())
client.Send(reply)
// TODO add Soundclound artist / title retrieval
sendUserTune(client, "Radiohead", "Spectre")
default:
@ -100,7 +100,7 @@ func playSCURL(p *mpg123.Player, rawURL string) {
func connectXmpp(jid string, password string, address string) (client *xmpp.Client, err error) {
xmppOptions := xmpp.Options{Address: address,
Jid: jid, Password: password, PacketLogger: os.Stdout,
Jid: jid, Password: password, PacketLogger: os.Stdout, Insecure: true,
Retry: 10}
if client, err = xmpp.NewClient(xmppOptions); err != nil {

View file

@ -1,4 +1,4 @@
package xmpp
package xmpp // import "fluux.io/xmpp"
import (
"crypto/sha1"

View file

@ -1,4 +1,4 @@
package xmpp
package xmpp // import "fluux.io/xmpp"
import "testing"

28
control_test.go Normal file
View file

@ -0,0 +1,28 @@
package xmpp // import "fluux.io/xmpp"
import (
"encoding/xml"
"testing"
"fluux.io/xmpp/iot"
)
func TestControlSet(t *testing.T) {
packet := `
<iq to='test@localhost/jukebox' from='admin@localhost/mbp' type='set' id='2'>
<set xmlns='urn:xmpp:iot:control' xml:lang='en'>
<string name='action' value='play'/>
<string name='url' value='https://soundcloud.com/radiohead/spectre'/>
</set>
</iq>`
parsedIQ := IQ{}
data := []byte(packet)
if err := xml.Unmarshal(data, &parsedIQ); err != nil {
t.Errorf("Unmarshal(%s) returned error", data)
}
if cs, ok := parsedIQ.Payload[0].(*iot.ControlSet); !ok {
t.Errorf("Paylod is not an iot control set: %v", cs)
}
}

2
doc.go
View file

@ -29,4 +29,4 @@ Fluux XMPP has been primarily tested with ejabberd (https://www.ejabberd.im)
but it should work with any XMPP compliant server.
*/
package xmpp
package xmpp // import "fluux.io/xmpp"

22
iq.go
View file

@ -2,7 +2,6 @@ package xmpp // import "fluux.io/xmpp"
import (
"encoding/xml"
"fmt"
"reflect"
"strconv"
@ -236,27 +235,6 @@ func (iq *IQ) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
}
}
// XMPPFormat returns the string representation of the XMPP packet.
// TODO: Should I simply rely on xml.Marshal ?
func (iq *IQ) XMPPFormat() string {
if iq.Payload != nil {
var payload []byte
var err error
if payload, err = xml.Marshal(iq.Payload); err != nil {
return fmt.Sprintf("<iq to='%s' type='%s' id='%s' xml:lang='en'>"+
"</iq>",
iq.To, iq.Type, iq.Id)
}
return fmt.Sprintf("<iq to='%s' type='%s' id='%s' xml:lang='en'>"+
"%s</iq>",
iq.To, iq.Type, iq.Id, payload)
}
return fmt.Sprintf("<iq to='%s' type='%s' id='%s' xml:lang='en'>"+
"%s</iq>",
iq.To, iq.Type, iq.Id,
iq.RawXML)
}
// ============================================================================
// Generic IQ Payload