examples/interact.go
author Chris Jones <christian.jones@sri.com>
Thu, 19 Jan 2012 14:18:39 -0600
changeset 76 caa722ab8a0f
parent 68 d693ecc11f29
child 77 edda99a69002
permissions -rw-r--r--
Fixed logging to use log rather than syslog.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     1
// Copyright 2011 The Go Authors.  All rights reserved.
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     2
// Use of this source code is governed by a BSD-style
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     3
// license that can be found in the LICENSE file.
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     4
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     5
package main
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     6
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     7
import (
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     8
	"cjyar/xmpp"
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     9
	"flag"
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 4
diff changeset
    10
	"fmt"
4
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    11
	"log"
76
caa722ab8a0f Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents: 68
diff changeset
    12
	"log/syslog"
4
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    13
	"os"
63
c7f2edd25f4a Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents: 57
diff changeset
    14
)
4
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    15
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    16
// Demonstrate the API, and allow the user to interact with an XMPP
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    17
// server via the terminal.
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    18
func main() {
76
caa722ab8a0f Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents: 68
diff changeset
    19
	if false {
caa722ab8a0f Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents: 68
diff changeset
    20
		xmpp.Loglevel = syslog.LOG_DEBUG
caa722ab8a0f Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents: 68
diff changeset
    21
		xmpp.Log = log.New(os.Stderr, "", 0)
caa722ab8a0f Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents: 68
diff changeset
    22
	}
caa722ab8a0f Fixed logging to use log rather than syslog.
Chris Jones <christian.jones@sri.com>
parents: 68
diff changeset
    23
4
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    24
	var jid xmpp.JID
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    25
	flag.Var(&jid, "jid", "JID to log in as")
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    26
	var pw *string = flag.String("pw", "", "password")
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    27
	flag.Parse()
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    28
	if jid.Domain == "" || *pw == "" {
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    29
		flag.Usage()
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    30
		os.Exit(2)
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    31
	}
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    32
36
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents: 33
diff changeset
    33
	c, err := xmpp.NewClient(&jid, *pw, nil)
4
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    34
	if err != nil {
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    35
		log.Fatalf("NewClient(%v): %v", jid, err)
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    36
	}
63
c7f2edd25f4a Intermediate commit. Fixing how we close our channels and sockets and shut down our goroutines.
Chris Jones <chris@cjones.org>
parents: 57
diff changeset
    37
	defer close(c.Out)
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 4
diff changeset
    38
33
571713f49494 Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents: 29
diff changeset
    39
	err = c.StartSession(true, &xmpp.Presence{})
29
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
    40
	if err != nil {
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
    41
		log.Fatalf("StartSession: %v", err)
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
    42
	}
57
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    43
	roster := xmpp.Roster(c)
33
571713f49494 Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents: 29
diff changeset
    44
	fmt.Printf("%d roster entries:\n", len(roster))
57
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    45
	for i, entry := range(roster) {
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    46
		fmt.Printf("%d: %v\n", i, entry)
33
571713f49494 Added roster retrieval to StartSession().
Chris Jones <chris@cjones.org>
parents: 29
diff changeset
    47
	}
29
a456133ed0ac Don't accept data on Client.Out until resource binding is
Chris Jones <chris@cjones.org>
parents: 26
diff changeset
    48
23
b5de44679389 Made the input and output channels of type Stanza rather than
Chris Jones <chris@cjones.org>
parents: 9
diff changeset
    49
	go func(ch <-chan xmpp.Stanza) {
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 4
diff changeset
    50
		for obj := range ch {
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 4
diff changeset
    51
			fmt.Printf("s: %v\n", obj)
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 4
diff changeset
    52
		}
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 4
diff changeset
    53
		fmt.Println("done reading")
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 4
diff changeset
    54
	}(c.In)
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 4
diff changeset
    55
68
d693ecc11f29 Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
    56
	p := make([]byte, 1024)
d693ecc11f29 Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
    57
	for {
d693ecc11f29 Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
    58
		nr, _ := os.Stdin.Read(p)
d693ecc11f29 Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
    59
		if nr == 0 {
d693ecc11f29 Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
    60
			break
d693ecc11f29 Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
    61
		}
d693ecc11f29 Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
    62
		s := string(p)
d693ecc11f29 Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
    63
		stan, err := xmpp.ParseStanza(s)
d693ecc11f29 Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
    64
		if err == nil {
d693ecc11f29 Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
    65
			c.Out <- stan
d693ecc11f29 Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
    66
		} else {
d693ecc11f29 Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
    67
			fmt.Printf("Parse error: %v\n", err)
d693ecc11f29 Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
    68
			break
d693ecc11f29 Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
    69
		}
d693ecc11f29 Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
    70
	}
d693ecc11f29 Restore this example program to its normal operation.
Chris Jones <chris@cjones.org>
parents: 63
diff changeset
    71
	fmt.Println("done sending")
4
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    72
}