Example #1
0
func producer(args []string) {

	flags := flag.NewFlagSet("producer", flag.ExitOnError)
	jidFlag := flags.String("jid", "", "JID")
	passFlag := flags.String("pass", "", "Password")
	insecureFlag := flags.Bool("insecure", false, "Allow insecure TLS")
	toFlag := flags.String("to", "", "Recipient")
	countFlag := flags.Int("count", 1, "Number of messages to send")
	flags.Parse(args)

	jid := must(xmpp.ParseJID(*jidFlag)).(xmpp.JID)
	addrs := must(xmpp.HomeServerAddrs(jid)).([]string)
	stream := must(xmpp.NewStream(addrs[0], nil)).(*xmpp.Stream)
	config := xmpp.ClientConfig{InsecureSkipVerify: *insecureFlag}
	x := must(xmpp.NewClientXMPP(stream, jid, *passFlag, &config)).(*xmpp.XMPP)

	x.Out <- xmpp.Presence{}

	go func() {
		count := *countFlag
		for i := 0; i < count; i++ {
			x.Out <- xmpp.Message{From: jid.String(), To: *toFlag, Body: strconv.Itoa(i)}
		}
		close(x.Out)
	}()

	for stanza := range x.In {
		switch v := stanza.(type) {
		case error:
			log.Fatal(v)
		default:
			log.Println(stanza)
		}
	}
}
Example #2
0
func consumer(args []string) {

	flags := flag.NewFlagSet("consumer", flag.ExitOnError)
	jidFlag := flags.String("jid", "", "JID")
	passFlag := flags.String("pass", "", "Password")
	insecureFlag := flags.Bool("insecure", false, "Allow insecure TLS")
	serverFlag := flags.String("server", "", "XMPP server address")
	flags.Parse(args)

	jid := must(xmpp.ParseJID(*jidFlag)).(xmpp.JID)
	var x *xmpp.XMPP
	if jid.Node == "" {
		stream := must(xmpp.NewStream(*serverFlag, nil)).(*xmpp.Stream)
		x = must(xmpp.NewComponentXMPP(stream, jid, *passFlag)).(*xmpp.XMPP)
	} else {
		addrs := must(xmpp.HomeServerAddrs(jid)).([]string)
		stream := must(xmpp.NewStream(addrs[0], nil)).(*xmpp.Stream)
		config := xmpp.ClientConfig{InsecureSkipVerify: *insecureFlag}
		x = must(xmpp.NewClientXMPP(stream, jid, *passFlag, &config)).(*xmpp.XMPP)
		x.Out <- xmpp.Presence{}
	}

	count := 0
	throughputCount := 0

	go func() {
		throughput := time.Tick(time.Second)
		total := time.Tick(time.Second * 5)
		for {
			select {
			case <-throughput:
				log.Printf("throughput: %d msgs/s\n", count-throughputCount)
				throughputCount = count
			case <-total:
				log.Printf("total: %d\n", count)
			}
		}
	}()

	for stanza := range x.In {
		switch v := stanza.(type) {
		case *xmpp.Message:
			count++
		case error:
			log.Fatal(v)
		}
	}
}
Example #3
0
func main() {

	flag.Parse()

	// Create stream and configure it as a client connection.
	jid := must(xmpp.ParseJID(*jid)).(xmpp.JID)
	stream := must(xmpp.NewStream(jid.Domain+":5222", &xmpp.StreamConfig{LogStanzas: true})).(*xmpp.Stream)
	client := must(xmpp.NewClientXMPP(stream, jid, *password, &xmpp.ClientConfig{InsecureSkipVerify: true})).(*xmpp.XMPP)

	log.Printf("Connection established for %s\n", client.JID)

	// Announce presence.
	client.Out <- xmpp.Presence{}

	// Filter messages into dedicated channel and start a goroutine to log them.
	_, messages := client.AddFilter(
		xmpp.MatcherFunc(
			func(v interface{}) bool {
				_, ok := v.(*xmpp.Message)
				return ok
			},
		),
	)
	go func() {
		for message := range messages {
			log.Printf("* message: %v\n", message)
		}
	}()

	// Log any stanzas that are not handled elsewhere.
	go func() {
		for x := range client.In {
			log.Printf("* recv: %v\n", x)
		}
	}()

	// Get disco#info for home server.
	info := &DiscoInfo{}
	iq := xmpp.Iq{Id: xmpp.UUID4(), Type: "get", To: client.JID.Domain}
	iq.PayloadEncode(info)
	reply, _ := client.SendRecv(&iq)
	reply.PayloadDecode(info)
	log.Printf("* info: %v\n", info)

	select {}
}
Example #4
0
func main() {

	// Parse args
	flag.Parse()
	jid, _ := xmpp.ParseJID(jid)

	// Lookup home server TCP addr if not explicitly set.
	if addr == "" {
		if addrs, err := xmpp.HomeServerAddrs(jid); err != nil {
			log.Fatal(err)
		} else {
			addr = addrs[0]
		}
	}

	// Create stream.
	stream, err := xmpp.NewStream(addr, &xmpp.StreamConfig{LogStanzas: debug})
	if err != nil {
		log.Fatal(err)
	}

	// Configure stream as client.
	config := xmpp.ClientConfig{InsecureSkipVerify: skipverify}
	x, err := xmpp.NewClientXMPP(stream, jid, pass, &config)
	if err != nil {
		log.Fatal(err)
	}

	// Signal presence.
	x.Out <- xmpp.Presence{}

	// Log anything that arrives.
	for stanza := range x.In {
		log.Printf("recv: %T %v", stanza, stanza)
	}
}