network/frame/rejected_frame.go

57 lines
1.3 KiB
Go

package frame
import (
coder "git.hpds.cc/Component/mq_coder"
)
// RejectedFrame is a coder encoded bytes, Tag is a fixed value TYPE_ID_REJECTED_FRAME
type RejectedFrame struct {
message string
}
// NewRejectedFrame creates a new RejectedFrame with a given TagId of user's data
func NewRejectedFrame(msg string) *RejectedFrame {
return &RejectedFrame{message: msg}
}
// Type gets the type of Frame.
func (f *RejectedFrame) Type() Type {
return TagOfRejectedFrame
}
// Encode to coder encoded bytes
func (f *RejectedFrame) Encode() []byte {
rejected := coder.NewNodePacketEncoder(byte(f.Type()))
// message
msgBlock := coder.NewPrimitivePacketEncoder(byte(TagOfRejectedMessage))
msgBlock.SetStringValue(f.message)
rejected.AddPrimitivePacket(msgBlock)
return rejected.Encode()
}
// Message rejected message
func (f *RejectedFrame) Message() string {
return f.message
}
// DecodeToRejectedFrame decodes coder encoded bytes to RejectedFrame
func DecodeToRejectedFrame(buf []byte) (*RejectedFrame, error) {
node := coder.NodePacket{}
_, err := coder.DecodeToNodePacket(buf, &node)
if err != nil {
return nil, err
}
rejected := &RejectedFrame{}
// message
if msgBlock, ok := node.PrimitivePackets[byte(TagOfRejectedMessage)]; ok {
msg, e := msgBlock.ToUTF8String()
if e != nil {
return nil, e
}
rejected.message = msg
}
return rejected, nil
}