Exemple #1
0
func main() {
	filter := ""

	if len(os.Args) >= 2 {
		filter = strings.ToLower(os.Args[1])
	}

	conn, err := protocol.Connect()

	if err != nil {
		panic(err.Error())
	}

	snoopDatagrams := make(chan protocol.Datagram)
	decoderDatagrams := make(chan protocol.Datagram)

	go func() {
		for d := range conn.Datagrams {
			snoopDatagrams <- d
			decoderDatagrams <- d
		}

		close(snoopDatagrams)
		close(decoderDatagrams)
	}()

	msgs, errs := protocol.NewMessageDecoder(decoderDatagrams)

	for {
		d := <-snoopDatagrams

		out := color.New(color.FgWhite)

		select {
		case msg := <-msgs:
			t := strings.ToLower(fmt.Sprintf("%T", msg.Payload))
			if !strings.Contains(t, filter) {
				break
			}
			printDatagram(out, &d)
			out.Add(color.FgGreen)
			out.Printf("MSG:  %+v\n      %T %+v", msg.Header, msg.Payload, msg.Payload)
			fmt.Printf("\n\n")
		case err := <-errs:
			printDatagram(out, &d)
			out.Add(color.FgRed)
			out.Printf("ERR:  %s", err.Error())
			fmt.Printf("\n\n")
		}
	}
}
Exemple #2
0
func New() *client {
	c := &client{}
	if conn, err := proto.Connect(); err == nil {
		c.connection = conn
		c.connected = true
	}

	messages, errors := c.connection.Listen()

	c.Messages = messages
	c.Errors = errors

	return c
}
Exemple #3
0
func main() {
	conn, err := protocol.Connect()

	if err != nil {
		panic(err.Error())
	}

	datagrams := make(chan protocol.Datagram)
	bytes := make(chan []byte)

	go func() {
		for d := range conn.Datagrams {
			datagrams <- d
			bytes <- d.Data
		}

		close(datagrams)
		close(bytes)
	}()

	msgs, errs := protocol.NewMessageDecoder(datagrams)

	re := regexp.MustCompilePOSIX("^00000")

	for {
		b := <-bytes

		out := color.New(color.FgWhite)
		out.Printf("DATA: length=%d\n", len(b))
		out.Print(re.ReplaceAllString(hex.Dump(b), "      "))
		color.Set(color.Bold)

		select {
		case msg := <-msgs:
			out.Add(color.FgGreen)
			out.Printf("MSG:  %+v\n      %T %+v", msg.Header, msg.Payload, msg.Payload)
		case err := <-errs:
			out.Add(color.FgRed)
			out.Printf("ERR:  %s", err.Error())
		}
		fmt.Printf("\n\n")
	}
}