mirror of
https://github.com/FluuxIO/go-xmpp.git
synced 2024-11-24 03:22:01 -08:00
Cleanup and add test for IOT control set parsing
This commit is contained in:
parent
d6bedfb033
commit
07b0d2d14d
@ -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 {
|
||||
|
@ -1,4 +1,4 @@
|
||||
package xmpp
|
||||
package xmpp // import "fluux.io/xmpp"
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
|
@ -1,4 +1,4 @@
|
||||
package xmpp
|
||||
package xmpp // import "fluux.io/xmpp"
|
||||
|
||||
import "testing"
|
||||
|
||||
|
28
control_test.go
Normal file
28
control_test.go
Normal 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
2
doc.go
@ -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
22
iq.go
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user