<commit_before>package device

import (
	"bytes"
	"fmt"
	"os"
	"sync"
	"time"

	"github.com/Comcast/webpa-common/logging"
	"github.com/Comcast/webpa-common/wrp"
)

func expectMessage(c Connection) (*wrp.Message, error) {
	var frame bytes.Buffer
	if ok, err := c.Read(&frame); !ok || err != nil {
		return nil, fmt.Errorf("Read failed: %s", err)
	}

	var (
		message = new(wrp.Message)
		decoder = wrp.NewDecoder(&frame, wrp.Msgpack)
	)

	if err := decoder.Decode(message); err != nil {
		return nil, fmt.Errorf("Could not decode message: %s", err)
	}

	return message, nil
}

func writeMessage(m *wrp.Message, c Connection) error {
	if frame, err := c.NextWriter(); err != nil {
		return err
	} else {
		encoder := wrp.NewEncoder(frame, wrp.Msgpack)
		if err := encoder.Encode(m); err != nil {
			return err
		}

		return frame.Close()
	}
}

func ExampleManagerTransaction() {
	var (
		transactionComplete = new(sync.WaitGroup)
		disconnected        = new(sync.WaitGroup)
	)

	transactionComplete.Add(1)
	disconnected.Add(1)

	var (
		options = &Options{
			Logger:    logging.DefaultLogger(),
			AuthDelay: 250 * time.Millisecond,
			Listeners: []Listener{
				func(e *Event) {
					switch e.Type {
					case Connect:
						fmt.Printf("%s connected\n", e.Device.ID())
					case MessageSent:
						if e.Message.MessageType() == wrp.AuthMessageType {
							fmt.Println("auth status sent")
						} else {
							fmt.Println("message sent")
						}
					case TransactionComplete:
						fmt.Println("response received")
						transactionComplete.Done()
					case Disconnect:
						fmt.Printf("%s disconnected\n", e.Device.ID())
						disconnected.Done()
					}
				},
			},
		}

		manager, server, websocketURL = startWebsocketServer(options)

		dialer                   = NewDialer(options, nil)
		connection, _, dialError = dialer.Dial(
			websocketURL,
			"mac:111122223333",
			nil,
		)
	)

	defer server.Close()
	if dialError != nil {
		fmt.Fprintf(os.Stderr, "Dial error: %s\n", dialError)
		return
	}

	// grab the auth status message that should be coming
	if message, err := expectMessage(connection); err != nil {
		fmt.Fprintf(os.Stderr, err.Error())
		return
	} else if message.Type != wrp.AuthMessageType {
		fmt.Fprintf(os.Stderr, "Expected auth status, but got: %s", message.Type)
		return
	} else if message.Status == nil {
		fmt.Fprintf(os.Stderr, "No auth status code")
		return
	} else if *message.Status != wrp.AuthStatusAuthorized {
		fmt.Fprintf(os.Stderr, "Expected authorized, but got: %d", *message.Status)
		return
	} else {
		fmt.Println("auth status received")
	}

	// spawn a go routine to respond to our routed message
	go func() {
		if message, err := expectMessage(connection); err != nil {
			fmt.Fprintf(os.Stderr, err.Error())
			return
		} else if message.Type != wrp.SimpleRequestResponseMessageType {
			fmt.Fprintf(os.Stderr, "Expected request/response, but got: %s", message.Type)
			return
		} else {
			fmt.Println("message received")
			deviceResponse := *message
			deviceResponse.Source = message.Destination
			deviceResponse.Destination = message.Source<commit_msg>Updated the message type constants<commit_after>package device

import (
	"bytes"
	"fmt"
	"os"
	"sync"
	"time"

	"github.com/Comcast/webpa-common/logging"
	"github.com/Comcast/webpa-common/wrp"
)

func expectMessage(c Connection) (*wrp.Message, error) {
	var frame bytes.Buffer
	if ok, err := c.Read(&frame); !ok || err != nil {
		return nil, fmt.Errorf("Read failed: %s", err)
	}

	var (
		message = new(wrp.Message)
		decoder = wrp.NewDecoder(&frame, wrp.Msgpack)
	)

	if err := decoder.Decode(message); err != nil {
		return nil, fmt.Errorf("Could not decode message: %s", err)
	}

	return message, nil
}

func writeMessage(m *wrp.Message, c Connection) error {
	if frame, err := c.NextWriter(); err != nil {
		return err
	} else {
		encoder := wrp.NewEncoder(frame, wrp.Msgpack)
		if err := encoder.Encode(m); err != nil {
			return err
		}

		return frame.Close()
	}
}

func ExampleManagerTransaction() {
	var (
		transactionComplete = new(sync.WaitGroup)
		disconnected        = new(sync.WaitGroup)
	)

	transactionComplete.Add(1)
	disconnected.Add(1)

	var (
		options = &Options{
			Logger:    logging.DefaultLogger(),
			AuthDelay: 250 * time.Millisecond,
			Listeners: []Listener{
				func(e *Event) {
					switch e.Type {
					case Connect:
						fmt.Printf("%s connected\n", e.Device.ID())
					case MessageSent:
						if e.Message.MessageType() == wrp.AuthorizationStatusMessageType {
							fmt.Println("auth status sent")
						} else {
							fmt.Println("message sent")
						}
					case TransactionComplete:
						fmt.Println("response received")
						transactionComplete.Done()
					case Disconnect:
						fmt.Printf("%s disconnected\n", e.Device.ID())
						disconnected.Done()
					}
				},
			},
		}

		manager, server, websocketURL = startWebsocketServer(options)

		dialer                   = NewDialer(options, nil)
		connection, _, dialError = dialer.Dial(
			websocketURL,
			"mac:111122223333",
			nil,
		)
	)

	defer server.Close()
	if dialError != nil {
		fmt.Fprintf(os.Stderr, "Dial error: %s\n", dialError)
		return
	}

	// grab the auth status message that should be coming
	if message, err := expectMessage(connection); err != nil {
		fmt.Fprintf(os.Stderr, err.Error())
		return
	} else if message.Type != wrp.AuthorizationStatusMessageType {
		fmt.Fprintf(os.Stderr, "Expected auth status, but got: %s", message.Type)
		return
	} else if message.Status == nil {
		fmt.Fprintf(os.Stderr, "No auth status code")
		return
	} else if *message.Status != wrp.AuthStatusAuthorized {
		fmt.Fprintf(os.Stderr, "Expected authorized, but got: %d", *message.Status)
		return
	} else {
		fmt.Println("auth status received")
	}

	// spawn a go routine to respond to our routed message
	go func() {
		if message, err := expectMessage(connection); err != nil {
			fmt.Fprintf(os.Stderr, err.Error())
			return
		} else if message.Type != wrp.SimpleRequestResponseMessageType {
			fmt.Fprintf(os.Stderr, "Expected request/response, but got: %s", message.Type)
			return
		} else {
			fmt.Println("message received")
			deviceResponse := *message
			deviceResponse.Source = message.Destination
			deviceResponse.Destination = message.Source
{'fraction_non_alphanumeric': 0.13335460200988994, 'fraction_numerical': 0.005423512521933323, 'mean_word_length': 3.3571924947880474, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '497659', 'n_tokens_mistral': 2411, 'n_tokens_neox': 2074, 'n_words': 509}