package frame import ( coder "git.hpds.cc/Component/mq_coder" ) // AuthenticationRespFrame is the response of Authentication. // AuthenticationRespFrame is a coder encoded bytes. type AuthenticationRespFrame struct { ok bool reason string } // OK returns if Authentication is success. func (f *AuthenticationRespFrame) OK() bool { return f.ok } // Reason returns the failed reason of Authentication. func (f *AuthenticationRespFrame) Reason() string { return f.reason } // NewAuthenticationRespFrame returns a AuthenticationRespFrame. func NewAuthenticationRespFrame(ok bool, reason string) *AuthenticationRespFrame { return &AuthenticationRespFrame{ ok: ok, reason: reason, } } // Type gets the type of the AuthenticationRespFrame. func (f *AuthenticationRespFrame) Type() Type { return TagOfAuthenticationAckFrame } // Encode encodes AuthenticationRespFrame to coder encoded bytes. func (f *AuthenticationRespFrame) Encode() []byte { // ok okBlock := coder.NewPrimitivePacketEncoder(byte(TagOfAuthenticationAckOk)) okBlock.SetBoolValue(f.ok) // reason reasonBlock := coder.NewPrimitivePacketEncoder(byte(TagOfAuthenticationAckReason)) reasonBlock.SetStringValue(f.reason) // frame ack := coder.NewNodePacketEncoder(byte(f.Type())) ack.AddPrimitivePacket(okBlock) ack.AddPrimitivePacket(reasonBlock) return ack.Encode() } // DecodeToAuthenticationRespFrame decodes coder encoded bytes to AuthenticationRespFrame. func DecodeToAuthenticationRespFrame(buf []byte) (*AuthenticationRespFrame, error) { node := coder.NodePacket{} _, err := coder.DecodeToNodePacket(buf, &node) if err != nil { return nil, err } f := &AuthenticationRespFrame{} // ok if okBlock, ok := node.PrimitivePackets[byte(TagOfAuthenticationAckOk)]; ok { ok, err := okBlock.ToBool() if err != nil { return nil, err } f.ok = ok } // reason if reasonBlock, ok := node.PrimitivePackets[byte(TagOfAuthenticationAckReason)]; ok { reason, err := reasonBlock.ToUTF8String() if err != nil { return nil, err } f.reason = reason } return f, nil }