package frame import ( coder "git.hpds.cc/Component/mq_coder" ) // HandshakeFrame is a coder encoded. type HandshakeFrame struct { // Name is client name Name string // ClientId represents client id ClientId string // ClientType represents client type (Protocol Gateway | Stream Function) ClientType byte // ObserveDataTags are the client data tag list. ObserveDataTags []byte // auth authName string authPayload string } // NewHandshakeFrame creates a new HandshakeFrame. func NewHandshakeFrame(name string, clientId string, clientType byte, observeDataTags []byte, authName string, authPayload string) *HandshakeFrame { return &HandshakeFrame{ Name: name, ClientId: clientId, ClientType: clientType, ObserveDataTags: observeDataTags, authName: authName, authPayload: authPayload, } } // Type gets the type of Frame. func (h *HandshakeFrame) Type() Type { return TagOfHandshakeFrame } // Encode to coder encoding. func (h *HandshakeFrame) Encode() []byte { // name nameBlock := coder.NewPrimitivePacketEncoder(TagOfHandshakeName) nameBlock.SetStringValue(h.Name) // client ID idBlock := coder.NewPrimitivePacketEncoder(TagOfHandshakeId) idBlock.SetStringValue(h.ClientId) // client type typeBlock := coder.NewPrimitivePacketEncoder(TagOfHandshakeType) typeBlock.SetBytesValue([]byte{h.ClientType}) // observe data tags observeDataTagsBlock := coder.NewPrimitivePacketEncoder(TagOfHandshakeObserveDataTags) observeDataTagsBlock.SetBytesValue(h.ObserveDataTags) // auth authNameBlock := coder.NewPrimitivePacketEncoder(TagOfHandshakeAuthName) authNameBlock.SetStringValue(h.authName) authPayloadBlock := coder.NewPrimitivePacketEncoder(TagOfHandshakeAuthPayload) authPayloadBlock.SetStringValue(h.authPayload) // handshake frame handshake := coder.NewNodePacketEncoder(h.Type()) handshake.AddPrimitivePacket(nameBlock) handshake.AddPrimitivePacket(idBlock) handshake.AddPrimitivePacket(typeBlock) handshake.AddPrimitivePacket(observeDataTagsBlock) handshake.AddPrimitivePacket(authNameBlock) handshake.AddPrimitivePacket(authPayloadBlock) return handshake.Encode() } // DecodeToHandshakeFrame decodes coder encoded bytes to HandshakeFrame. func DecodeToHandshakeFrame(buf []byte) (*HandshakeFrame, error) { node := coder.NodePacket{} _, err := coder.DecodeToNodePacket(buf, &node) if err != nil { return nil, err } handshake := &HandshakeFrame{} // name if nameBlock, ok := node.PrimitivePackets[TagOfHandshakeName]; ok { name, err := nameBlock.ToUTF8String() if err != nil { return nil, err } handshake.Name = name } // client id if idBlock, ok := node.PrimitivePackets[TagOfHandshakeId]; ok { id, err := idBlock.ToUTF8String() if err != nil { return nil, err } handshake.ClientId = id } // client type if typeBlock, ok := node.PrimitivePackets[TagOfHandshakeType]; ok { clientType := typeBlock.ToBytes() handshake.ClientType = clientType[0] } // observe data tag list if observeDataTagsBlock, ok := node.PrimitivePackets[TagOfHandshakeObserveDataTags]; ok { handshake.ObserveDataTags = observeDataTagsBlock.ToBytes() } // auth if authNameBlock, ok := node.PrimitivePackets[TagOfHandshakeAuthName]; ok { authName, err := authNameBlock.ToUTF8String() if err != nil { return nil, err } handshake.authName = authName } if authPayloadBlock, ok := node.PrimitivePackets[TagOfHandshakeAuthPayload]; ok { authPayload, err := authPayloadBlock.ToUTF8String() if err != nil { return nil, err } handshake.authPayload = authPayload } return handshake, nil } // AuthPayload authentication payload func (h *HandshakeFrame) AuthPayload() string { return h.authPayload } // AuthName authentication name func (h *HandshakeFrame) AuthName() string { return h.authName }