Implements send / send raw

This commit is contained in:
Mickael Remond 2018-01-26 09:55:39 +01:00
parent 2cd8eed765
commit ad6e09a0f6
No known key found for this signature in database
GPG Key ID: E6F6045D79965AA3
4 changed files with 39 additions and 8 deletions

View File

@ -126,9 +126,24 @@ func (c *Client) Recv() <-chan interface{} {
return ch return ch
} }
// Send sends message text. // Send marshalls XMPP stanza and sends it to the server.
// TODO Move to Go XML Marshaller func (c *Client) Send(packet Packet) error {
func (c *Client) Send(packet string) error { data, err := xml.Marshal(packet)
if err != nil {
return errors.New("cannot marshal packet " + err.Error())
}
if _, err := fmt.Fprintf(c.conn, string(data)); err != nil {
return errors.New("cannot send packet " + err.Error())
}
return nil
}
// SendRaw sends an XMPP stanza as a string to the server.
// It can be invalid XML or XMPP content. In that case, the server will
// disconnect the client. It is up to the user of this method to
// carefully craft the XML content to produce valid XMPP.
func (c *Client) SendRaw(packet string) error {
fmt.Fprintf(c.Session.socketProxy, packet) // TODO handle errors fmt.Fprintf(c.Session.socketProxy, packet) // TODO handle errors
return nil return nil
} }

View File

@ -13,7 +13,13 @@ import (
) )
func main() { func main() {
options := xmpp.Options{Address: "localhost:5222", Jid: "test@localhost", Password: "test", PacketLogger: os.Stdout} options := xmpp.Options{
Address: "localhost:5222",
Jid: "test@localhost",
Password: "test",
PacketLogger: os.Stdout,
Insecure: true,
}
var client *xmpp.Client var client *xmpp.Client
var err error var err error
@ -34,7 +40,7 @@ func main() {
case *xmpp.Message: case *xmpp.Message:
fmt.Fprintf(os.Stdout, "Body = %s - from = %s\n", packet.Body, packet.From) fmt.Fprintf(os.Stdout, "Body = %s - from = %s\n", packet.Body, packet.From)
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From}, Body: packet.Body} reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From}, Body: packet.Body}
client.Send(reply.XMPPFormat()) client.Send(reply)
default: default:
fmt.Fprintf(os.Stdout, "Ignoring packet: %T\n", packet) fmt.Fprintf(os.Stdout, "Ignoring packet: %T\n", packet)
} }

View File

@ -77,7 +77,7 @@ func processIq(client *xmpp.Client, p *mpg123.Player, packet *xmpp.IQ) {
playSCURL(p, url) playSCURL(p, url)
setResponse := new(iot.ControlSetResponse) setResponse := new(iot.ControlSetResponse)
reply := xmpp.IQ{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "result", Id: packet.Id}, Payload: []xmpp.IQPayload{setResponse}} reply := xmpp.IQ{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "result", Id: packet.Id}, Payload: []xmpp.IQPayload{setResponse}}
client.Send(reply.XMPPFormat()) client.SendRaw(reply.XMPPFormat())
// TODO add Soundclound artist / title retrieval // TODO add Soundclound artist / title retrieval
sendUserTune(client, "Radiohead", "Spectre") sendUserTune(client, "Radiohead", "Spectre")
default: default:
@ -87,7 +87,7 @@ func processIq(client *xmpp.Client, p *mpg123.Player, packet *xmpp.IQ) {
func sendUserTune(client *xmpp.Client, artist string, title string) { func sendUserTune(client *xmpp.Client, artist string, title string) {
tune := pep.Tune{Artist: artist, Title: title} tune := pep.Tune{Artist: artist, Title: title}
client.Send(tune.XMPPFormat()) client.SendRaw(tune.XMPPFormat())
} }
func playSCURL(p *mpg123.Player, rawURL string) { func playSCURL(p *mpg123.Player, rawURL string) {

View File

@ -73,11 +73,12 @@ func (c *Component) Connect(connStr string) error {
} }
// ReadPacket reads next incoming XMPP packet // ReadPacket reads next incoming XMPP packet
// TODO use defined interface Packet
func (c *Component) ReadPacket() (Packet, error) { func (c *Component) ReadPacket() (Packet, error) {
// TODO use defined interface Packet
return next(c.decoder) return next(c.decoder)
} }
// Send marshalls XMPP stanza and sends it to the server.
func (c *Component) Send(packet Packet) error { func (c *Component) Send(packet Packet) error {
data, err := xml.Marshal(packet) data, err := xml.Marshal(packet)
if err != nil { if err != nil {
@ -90,6 +91,15 @@ func (c *Component) Send(packet Packet) error {
return nil return nil
} }
// SendRaw sends an XMPP stanza as a string to the server.
// It can be invalid XML or XMPP content. In that case, the server will
// disconnect the component. It is up to the user of this method to
// carefully craft the XML content to produce valid XMPP.
func (c *Component) SendRaw(packet string) error {
fmt.Fprintf(c.conn, packet) // TODO handle errors
return nil
}
// handshake generates an authentication token based on StreamID and shared secret. // handshake generates an authentication token based on StreamID and shared secret.
func (c *Component) handshake(streamId string) string { func (c *Component) handshake(streamId string) string {
// 1. Concatenate the Stream ID received from the server with the shared secret. // 1. Concatenate the Stream ID received from the server with the shared secret.