<commit_before>package gott

const (
	CONNECT     = uint8(1)
	CONNACK     = uint8(2)
	PUBLISH     = uint8(3)
	PUBACK      = uint8(4)
	PUBREC      = uint8(5)
	PUBREL      = uint8(6)
	PUBCOMP     = uint8(7)
	SUBSCRIBE   = uint8(8)
	SUBACK      = uint8(9)
	UNSUBSCRIBE = uint8(10)
	UNSUBACK    = uint8(11)
	PINGREQ     = uint8(12)
	PINGRESP    = uint8(13)
	DISCONNECT  = uint8(14)
)

// StaticHeader holds all of the data that is found
// in the mqtt static header
type FixedHeader struct {
	MessageType uint8
	Dup         bool
	Qos         uint8
	Retain      bool
	Remaining   int
}

// VariableHeader holds all of the data that can be found
// in the mqtt variable header
// TODO maybe make this an interface since
// It can look different a lot of places
type VariableHeader interface {
	Type() string
	Bytes() []byte
}

type ConnectHeader struct {
	ProtoName string
	ProtoVersion uint8
	CleanSession bool
	Will bool
	WillQos uint8
	WillRetain bool
	Pass bool
	User bool
	KeepAlive uint8
}

type ConnackHeader struct {
	ReturnCode uint8
}



type PublishHeader struct {
	Topic string
}

// Payload is an interface that can be any kind of data
// you want to send as long as you can write
// it to bytes before we send it on the wire
type Payload interface {
	Bytes() []byte
}

// PayloadBuffer is an alias of []byte so we can attach
// functions to received payloads 
type PayloadBuffer []byte

// Message holds everything that a message can be
// message is the construct that the api will mainly
// deal with messages as the construct with which to
// attach functionality
type Message struct {
	FixedHeader    FixedHeader
	VariableHeader VariableHeader
	Payload        Payload
}

// Bytes writes all the data in a message to bytes
// this is to prepare the message for sending
func (m Message) Bytes() []byte {
	bytesToReturn := make([]byte, 0)
	bytesToReturn = append(bytesToReturn, m.FixedHeader.Bytes()...)
	bytesToReturn = append(bytesToReturn, m.VariableHeader.Bytes()...)
	bytesToReturn = append(bytesToReturn, m.Payload.Bytes()...)
	return bytesToReturn
}

// Bytes Returns the []byte value of the
// PayloadBuffer
func (p PayloadBuffer) Bytes() []byte {
	return []byte(p)
}

/********************ENCODING**************************/

// Bytes writes all the data in the fixed header
// as defined here: http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html#fixed-header
func (f FixedHeader) Bytes() []byte {
	bytesToReturn := make([]byte, 0)
	var byte1 byte

	// Set Message type
	byte1 |= f.MessageType

	// Set Dup Flag
	if f.Dup {
		byte1 |= (1 << 4)
	}

	// Set Qos
	byte1 |= (f.Qos << 5)

	// Set Retain
	if f.Retain {
		byte1 |= (1 << 7)
	}

	bytesToReturn = append(bytesToReturn, byte1)

	// Set Remaining length
	lengthBytes := EncodeRemainingLength(f.Remaining)

	bytesToReturn = append(bytesToReturn, lengthBytes...)
	return bytesToReturn
}

func (c ConnectHeader) Bytes() []byte {
	bytesToReturn := make([]byte, 0)
	ProtoNameBytes := []byte(c.ProtoName)

	ProtoVersionByte := byte(c.ProtoVersion)

	FlagByte := byte(0)

	if c.CleanSession {
		FlagByte |= (1 << 1)
	}

	if c.Will {
		FlagByte |= (1 << 2)
	}

	FlagByte |= (c.WillQos << 3)

	if c.WillRetain {
		FlagByte |= (1 << 5)
	}

	if c.Pass {
		FlagByte |= (1 << 6)
	}

	if c.User {
		FlagByte |= (1 << 7)
	}

	bytesToReturn = append(bytesToReturn, ProtoNameBytes...)
	bytesToReturn = append(bytesToReturn, ProtoVersionByte, FlagByte, c.KeepAlive)
	return bytesToReturn
}

func (c ConnectHeader) Type() string {
	return "CONNECT"
}

func (c ConnackHeader) Bytes() []byte {
	return []byte{c.ReturnCode}
}

func (c ConnackHeader) Type() string {
	return "CONNACK"
}

func (p PublishHeader) Bytes() []byte {
	return []byte(p.Topic)
}

func (p PublishHeader) Type() string {
	return "PUBLISH"
}


// EncodeRemainingLength encodes an int into the
// Remaining length encoding format as defined in the spec
func EncodeRemainingLength(length int) []byte {<commit_msg>added more documentation for packet.go<commit_after>package gott

const (
	CONNECT     = uint8(1)
	CONNACK     = uint8(2)
	PUBLISH     = uint8(3)
	PUBACK      = uint8(4)
	PUBREC      = uint8(5)
	PUBREL      = uint8(6)
	PUBCOMP     = uint8(7)
	SUBSCRIBE   = uint8(8)
	SUBACK      = uint8(9)
	UNSUBSCRIBE = uint8(10)
	UNSUBACK    = uint8(11)
	PINGREQ     = uint8(12)
	PINGRESP    = uint8(13)
	DISCONNECT  = uint8(14)
)

// StaticHeader holds all of the data that is found
// in the mqtt static header
type FixedHeader struct {
	MessageType uint8
	Dup         bool
	Qos         uint8
	Retain      bool
	Remaining   int
}

// VariableHeader is an interface that all of
// the different types of variable headers
type VariableHeader interface {
	Type() string
	Bytes() []byte
}

// ConnectHeader holds all of the data for the
// variable header for CONNECT messages
type ConnectHeader struct {
	ProtoName string
	ProtoVersion uint8
	CleanSession bool
	Will bool
	WillQos uint8
	WillRetain bool
	Pass bool
	User bool
	KeepAlive uint8
}

// ConnackHeader holds all of the data for the
// variable header for CONNACK messages
type ConnackHeader struct {
	ReturnCode uint8
}

// PublishHeader holds all of the data for the
// variable header for PUBLISH messages
type PublishHeader struct {
	Topic string
}

// Payload is an interface that can be any kind of data
// you want to send as long as you can write
// it to bytes before we send it on the wire
type Payload interface {
	Bytes() []byte
}

// PayloadBuffer is an alias of []byte so we can attach
// functions to received payloads 
type PayloadBuffer []byte

// Message holds everything that a message can be
// message is the construct that the api will mainly
// deal with messages as the construct with which to
// attach functionality
type Message struct {
	FixedHeader    FixedHeader
	VariableHeader VariableHeader
	Payload        Payload
}

// Bytes writes all the data in a message to bytes
// this is to prepare the message for sending
func (m Message) Bytes() []byte {
	bytesToReturn := make([]byte, 0)
	bytesToReturn = append(bytesToReturn, m.FixedHeader.Bytes()...)
	bytesToReturn = append(bytesToReturn, m.VariableHeader.Bytes()...)
	bytesToReturn = append(bytesToReturn, m.Payload.Bytes()...)
	return bytesToReturn
}

// Bytes Returns the []byte value of the
// PayloadBuffer
func (p PayloadBuffer) Bytes() []byte {
	return []byte(p)
}

/********************ENCODING**************************/

// Bytes writes all the data in the fixed header
// as defined here: http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html#fixed-header
func (f FixedHeader) Bytes() []byte {
	bytesToReturn := make([]byte, 0)
	var byte1 byte

	// Set Message type
	byte1 |= f.MessageType

	// Set Dup Flag
	if f.Dup {
		byte1 |= (1 << 4)
	}

	// Set Qos
	byte1 |= (f.Qos << 5)

	// Set Retain
	if f.Retain {
		byte1 |= (1 << 7)
	}

	bytesToReturn = append(bytesToReturn, byte1)

	// Set Remaining length
	lengthBytes := EncodeRemainingLength(f.Remaining)

	bytesToReturn = append(bytesToReturn, lengthBytes...)
	return bytesToReturn
}

// Bytes writes all the data in the variable header for
// CONNECT messages 
func (c ConnectHeader) Bytes() []byte {
	bytesToReturn := make([]byte, 0)
	ProtoNameBytes := []byte(c.ProtoName)

	ProtoVersionByte := byte(c.ProtoVersion)

	FlagByte := byte(0)

	if c.CleanSession {
		FlagByte |= (1 << 1)
	}

	if c.Will {
		FlagByte |= (1 << 2)
	}

	FlagByte |= (c.WillQos << 3)

	if c.WillRetain {
		FlagByte |= (1 << 5)
	}

	if c.Pass {
		FlagByte |= (1 << 6)
	}

	if c.User {
		FlagByte |= (1 << 7)
	}

	bytesToReturn = append(bytesToReturn, ProtoNameBytes...)
	bytesToReturn = append(bytesToReturn, ProtoVersionByte, FlagByte, c.KeepAlive)
	return bytesToReturn
}

// Type returns a string of the type of variable header
func (c ConnectHeader) Type() string {
	return "CONNECT"
}

// Bytes returns the bytes of the variable header
// for the CONNACK message
func (c ConnackHeader) Bytes() []byte {
	return []byte{c.ReturnCode}
}

// Tyoe returns a string of the type of variable header
func (c ConnackHeader) Type() string {
	return "CONNACK"
}

// Bytes returns the bytes of the variable header
// for the PUBLISH message
func (p PublishHeader) Bytes() []byte {
	return []byte(p.Topic)
}

// Type returns the string of the type of variable header
func (p PublishHeader) Type() string {
	return "PUBLISH"
}


// EncodeRemainingLength encodes an int into the
// Remaining length encoding format as defined in the spec
func EncodeRemainingLength(length int) []byte {
{'fraction_non_alphanumeric': 0.11338268123604747, 'fraction_numerical': 0.015744330865938197, 'mean_word_length': 3.465897166841553, 'pattern_counts': {'":': 0, '<': 39, '<?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': '3035735', 'n_tokens_mistral': 2923, 'n_tokens_neox': 2806, 'n_words': 966}