2022-10-11 17:36:09 +08:00
|
|
|
package frame
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
coder "git.hpds.cc/Component/mq_coder"
|
|
|
|
gonanoid "github.com/matoous/go-nanoid/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MetaFrame is a coder encoded bytes, SeqId is a fixed value of TYPE_ID_TRANSACTION.
|
|
|
|
// used for describes metadata for a DataFrame.
|
|
|
|
type MetaFrame struct {
|
2023-04-05 16:15:59 +08:00
|
|
|
tid string
|
|
|
|
metadata []byte
|
|
|
|
sourceId string
|
|
|
|
broadcast bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// randString genetates a random string.
|
|
|
|
func randString() string {
|
|
|
|
tid, err := gonanoid.New()
|
|
|
|
if err != nil {
|
|
|
|
tid = strconv.FormatInt(time.Now().UnixMicro(), 10)
|
|
|
|
}
|
|
|
|
return tid
|
2022-10-11 17:36:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMetaFrame creates a new MetaFrame instance.
|
|
|
|
func NewMetaFrame() *MetaFrame {
|
|
|
|
tid, err := gonanoid.New()
|
|
|
|
if err != nil {
|
|
|
|
tid = strconv.FormatInt(time.Now().UnixMicro(), 10)
|
|
|
|
}
|
|
|
|
return &MetaFrame{tid: tid}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTransactionId set the transaction id.
|
|
|
|
func (m *MetaFrame) SetTransactionId(transactionId string) {
|
|
|
|
m.tid = transactionId
|
|
|
|
}
|
|
|
|
|
|
|
|
// TransactionId returns transactionId
|
|
|
|
func (m *MetaFrame) TransactionId() string {
|
|
|
|
return m.tid
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMetadata set the extra info of the application
|
|
|
|
func (m *MetaFrame) SetMetadata(metadata []byte) {
|
|
|
|
m.metadata = metadata
|
|
|
|
}
|
|
|
|
|
|
|
|
// Metadata returns the extra info of the application
|
|
|
|
func (m *MetaFrame) Metadata() []byte {
|
|
|
|
return m.metadata
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSourceId set the source ID.
|
|
|
|
func (m *MetaFrame) SetSourceId(sourceId string) {
|
|
|
|
m.sourceId = sourceId
|
|
|
|
}
|
|
|
|
|
|
|
|
// SourceId returns source ID
|
|
|
|
func (m *MetaFrame) SourceId() string {
|
|
|
|
return m.sourceId
|
|
|
|
}
|
|
|
|
|
2023-04-05 16:15:59 +08:00
|
|
|
// SetBroadcast set broadcast mode
|
|
|
|
func (m *MetaFrame) SetBroadcast(enabled bool) {
|
|
|
|
m.broadcast = enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsBroadcast returns the broadcast mode is enabled
|
|
|
|
func (m *MetaFrame) IsBroadcast() bool {
|
|
|
|
return m.broadcast
|
|
|
|
}
|
|
|
|
|
2022-10-11 17:36:09 +08:00
|
|
|
// Encode implements Frame.Encode method.
|
|
|
|
func (m *MetaFrame) Encode() []byte {
|
2023-03-26 23:18:55 +08:00
|
|
|
meta := coder.NewNodePacketEncoder(byte(TagOfMetaFrame))
|
2023-04-05 16:15:59 +08:00
|
|
|
// transaction Id
|
2023-03-26 23:18:55 +08:00
|
|
|
transactionId := coder.NewPrimitivePacketEncoder(byte(TagOfTransactionId))
|
2022-10-11 17:36:09 +08:00
|
|
|
transactionId.SetStringValue(m.tid)
|
|
|
|
meta.AddPrimitivePacket(transactionId)
|
|
|
|
|
2023-04-05 16:15:59 +08:00
|
|
|
// source Id
|
2023-03-26 23:18:55 +08:00
|
|
|
sourceId := coder.NewPrimitivePacketEncoder(byte(TagOfSourceId))
|
2022-10-11 17:36:09 +08:00
|
|
|
sourceId.SetStringValue(m.sourceId)
|
|
|
|
meta.AddPrimitivePacket(sourceId)
|
|
|
|
|
|
|
|
// metadata
|
|
|
|
if m.metadata != nil {
|
2023-03-26 23:18:55 +08:00
|
|
|
metadata := coder.NewPrimitivePacketEncoder(byte(TagOfMetadata))
|
2022-10-11 17:36:09 +08:00
|
|
|
metadata.SetBytesValue(m.metadata)
|
|
|
|
meta.AddPrimitivePacket(metadata)
|
|
|
|
}
|
2023-04-05 16:15:59 +08:00
|
|
|
// broadcast mode
|
|
|
|
broadcast := coder.NewPrimitivePacketEncoder(byte(TagOfBroadcast))
|
|
|
|
broadcast.SetBoolValue(m.broadcast)
|
|
|
|
meta.AddPrimitivePacket(broadcast)
|
2022-10-11 17:36:09 +08:00
|
|
|
|
|
|
|
return meta.Encode()
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeToMetaFrame decode a MetaFrame instance from given buffer.
|
2023-04-05 16:15:59 +08:00
|
|
|
func DecodeToMetaFrame(buf []byte, meta *MetaFrame) error {
|
2022-10-11 17:36:09 +08:00
|
|
|
nodeBlock := coder.NodePacket{}
|
|
|
|
_, err := coder.DecodeToNodePacket(buf, &nodeBlock)
|
|
|
|
if err != nil {
|
2023-04-05 16:15:59 +08:00
|
|
|
return err
|
2022-10-11 17:36:09 +08:00
|
|
|
}
|
|
|
|
|
2023-04-05 16:15:59 +08:00
|
|
|
//meta := &MetaFrame{}
|
2022-10-11 17:36:09 +08:00
|
|
|
for k, v := range nodeBlock.PrimitivePackets {
|
|
|
|
switch k {
|
2023-03-26 23:18:55 +08:00
|
|
|
case byte(TagOfTransactionId):
|
2022-10-11 17:36:09 +08:00
|
|
|
val, err := v.ToUTF8String()
|
|
|
|
if err != nil {
|
2023-04-05 16:15:59 +08:00
|
|
|
return err
|
2022-10-11 17:36:09 +08:00
|
|
|
}
|
|
|
|
meta.tid = val
|
2023-03-26 23:18:55 +08:00
|
|
|
case byte(TagOfMetadata):
|
2022-10-11 17:36:09 +08:00
|
|
|
meta.metadata = v.ToBytes()
|
2023-03-26 23:18:55 +08:00
|
|
|
case byte(TagOfSourceId):
|
2022-10-11 17:36:09 +08:00
|
|
|
sourceId, err := v.ToUTF8String()
|
|
|
|
if err != nil {
|
2023-04-05 16:15:59 +08:00
|
|
|
return err
|
2022-10-11 17:36:09 +08:00
|
|
|
}
|
|
|
|
meta.sourceId = sourceId
|
2023-04-05 16:15:59 +08:00
|
|
|
case byte(TagOfBroadcast):
|
|
|
|
broadcast, err := v.ToBool()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
meta.broadcast = broadcast
|
2022-10-11 17:36:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-05 16:15:59 +08:00
|
|
|
return nil
|
2022-10-11 17:36:09 +08:00
|
|
|
}
|