Esempio n. 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")
		}
	}
}
Esempio n. 2
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")
	}
}