examples/interact.go
author Chris Jones <chris@cjones.org>
Thu, 29 Dec 2011 09:48:36 -0700
changeset 28 78961db80bae
parent 26 4d0a369079ce
child 29 a456133ed0ac
permissions -rw-r--r--
Added Client.StartSession().
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"
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    12
	"os"
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    13
	)
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    14
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    15
// 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
    16
// server via the terminal.
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    17
func main() {
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    18
	var jid xmpp.JID
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    19
	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
    20
	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
    21
	flag.Parse()
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    22
	if jid.Domain == "" || *pw == "" {
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    23
		flag.Usage()
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    24
		os.Exit(2)
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    25
	}
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    26
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    27
	c, err := xmpp.NewClient(&jid, *pw)
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    28
	if err != nil {
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    29
		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
    30
	}
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    31
	defer c.Close()
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 4
diff changeset
    32
23
b5de44679389 Made the input and output channels of type Stanza rather than
Chris Jones <chris@cjones.org>
parents: 9
diff changeset
    33
	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
    34
		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
    35
			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
    36
		}
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 4
diff changeset
    37
		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
    38
	}(c.In)
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 4
diff changeset
    39
9
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 6
diff changeset
    40
	p := make([]byte, 1024)
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 6
diff changeset
    41
	for {
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 6
diff changeset
    42
		nr, _ := os.Stdin.Read(p)
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 6
diff changeset
    43
		if nr == 0 {
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 6
diff changeset
    44
			break
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 6
diff changeset
    45
		}
4fe926b03827 Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents: 6
diff changeset
    46
		s := string(p)
26
4d0a369079ce Removed the TextOut channel.
Chris Jones <chris@cjones.org>
parents: 23
diff changeset
    47
		stan, err := xmpp.ParseStanza(s)
4d0a369079ce Removed the TextOut channel.
Chris Jones <chris@cjones.org>
parents: 23
diff changeset
    48
		if err == nil {
4d0a369079ce Removed the TextOut channel.
Chris Jones <chris@cjones.org>
parents: 23
diff changeset
    49
			c.Out <- stan
4d0a369079ce Removed the TextOut channel.
Chris Jones <chris@cjones.org>
parents: 23
diff changeset
    50
		} else {
4d0a369079ce Removed the TextOut channel.
Chris Jones <chris@cjones.org>
parents: 23
diff changeset
    51
			fmt.Printf("Parse error: %v\n", err)
4d0a369079ce Removed the TextOut channel.
Chris Jones <chris@cjones.org>
parents: 23
diff changeset
    52
			break
4d0a369079ce Removed the TextOut channel.
Chris Jones <chris@cjones.org>
parents: 23
diff changeset
    53
		}
6
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 4
diff changeset
    54
	}
8e425e340ca1 Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents: 4
diff changeset
    55
	fmt.Println("done sending")
4
a8fbec71a194 Added an interactive test and made Client implement io.Closer.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    56
}