Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow send raw XML #2

Closed
remss opened this issue Apr 17, 2013 · 5 comments
Closed

Allow send raw XML #2

remss opened this issue Apr 17, 2013 · 5 comments

Comments

@remss
Copy link

remss commented Apr 17, 2013

Currently to send a stanza we can use Stanza.Send(), but it has to be a Struct to be XML encoded and sent.
I see that Stanza.send() can do the job, but it's a private method. It would be nice that it become public.

@atomatt
Copy link
Collaborator

atomatt commented Apr 22, 2013

Do you mean Stream.[sS]end()? If so, it's not quite as simple as making send() public as the Stream instance is hidden behind the an XMPP's In and Out channels.

I have no real objection to allowing a []byte stanza if it's useful. Do you have a particular use case for it? Note that it does give the application the ability to really mess up the underlying XML stream, but that's the application's fault ;).

@remss
Copy link
Author

remss commented Apr 22, 2013

My need is to provide in my component a "console mode" that sends custom XML stanza read from STDIN. Very useful to dev/debug.

Here the code to do that in my app:

go func() {
    fmt.Printf("Console = On\n")
    reader := bufio.NewReader(os.Stdin)
    lines := []string{}
    for {
        line, err := reader.ReadString('\n')
        if err != nil {
            fmt.Printf(err.Error())
            lines = []string{}
            continue
        }
        if line == "\n" { // empty line found, sends XML stanza !
            stanza := strings.Join(lines, "")
            lines = []string{}
            if strings.Trim(stanza, "\n") != "" {
                go stream.SendRaw([]byte(stanza))
            }
        } else {
            lines = append(lines, line)
        }
    }
}()

With this simple change in go-xmpp/src/xmpp/stream.go

func (stream *Stream) SendRaw(b []byte) error {
    return stream.send(b)
}

I agree with the application's responsibility in case of invalid stanza, but hey, "With great power comes great responsibility" ! :-)

@atomatt
Copy link
Collaborator

atomatt commented May 22, 2013

Sorry for sitting on this for so long. I think you're right - it won't hurt to expose a method to send a complete stanza. However the current code is slightly wrong (mostly in terms of debug logging) at the moment. I'll see what I can do.

Just something I noticed about the above example … a XML stream is inherently sequential and xmpp.Stream does nothing to serialise access to the stream, so starting a new goroutine for each stanza - go stream.SendRaw([]byte(stanza)) - could result in things getting mixed up.

@remss
Copy link
Author

remss commented May 23, 2013

I see. We may change the Stream.Send() method to handle []byte type for v parameter in addition to classic stanza structures ? In this case the outgoing stanza flow will be preserved.

Below a patch proposal in go-xmpp/src/xmpp/stream.go

// Send a stanza. Used to write a complete, top-level element.
func (stream *Stream) Send(v interface{}) error {
    bytes, ok := v.([]byte)
    if ok {
        return stream.send(bytes)
    }
    if stream.config.LogStanzas {
        bytes, err := xml.Marshal(v)
        if err != nil {
            return err
        }
        return stream.send(bytes)
    }
    enc := xml.NewEncoder(stream.conn)
    return enc.Encode(v)
}

Then i could use comp.Out <- []byte(stanza) insted of go stream.SendRaw([]byte(stanza))

@remss
Copy link
Author

remss commented Sep 29, 2016

Opened issue for too long. I close it since I do not work with this library any more.

@remss remss closed this as completed Sep 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants