代码整理
This commit is contained in:
213
hooks/storage/storage.go
Normal file
213
hooks/storage/storage.go
Normal file
@@ -0,0 +1,213 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// SPDX-FileCopyrightText: 2022 mochi-mqtt, mochi-co
|
||||
// SPDX-FileContributor: mochi-co
|
||||
|
||||
package storage
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"testmqtt/packets"
|
||||
"testmqtt/system"
|
||||
)
|
||||
|
||||
const (
|
||||
// SubscriptionKey 唯一标识订阅信息。在存储系统中,这个键用于检索和管理客户端的订阅信息
|
||||
// SubscriptionKey unique key to denote Subscriptions in a store
|
||||
SubscriptionKey = "SUB"
|
||||
// SysInfoKey 唯一标识服务器系统信息。在存储系统中,这个键用于存储和检索与服务器状态或系统配置相关的信息。
|
||||
// SysInfoKey unique key to denote server system information in a store
|
||||
SysInfoKey = "SYS"
|
||||
// RetainedKey 唯一标识保留的消息。保留消息是在订阅时立即发送的消息,即使订阅者在消息发布时不在线。这个键用于存储这些保留消息。
|
||||
// RetainedKey unique key to denote retained messages in a store
|
||||
RetainedKey = "RET"
|
||||
// InflightKey 唯一标识飞行中的消息。飞行中的消息指的是已经发布但尚未确认的消息,这个键用于跟踪这些消息的状态。
|
||||
// InflightKey unique key to denote inflight messages in a store
|
||||
InflightKey = "IFM"
|
||||
// ClientKey 唯一标识客户端信息。在存储系统中,这个键用于检索和管理有关客户端的信息,如客户端状态、连接信息等。
|
||||
// ClientKey unique key to denote clients in a store
|
||||
ClientKey = "CL"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrDBFileNotOpen 数据库文件没有打开
|
||||
// ErrDBFileNotOpen indicates that the file database (e.g. bolt/badger) wasn't open for reading.
|
||||
ErrDBFileNotOpen = errors.New("数据库文件没有打开")
|
||||
)
|
||||
|
||||
// Serializable is an interface for objects that can be serialized and deserialized.
|
||||
type Serializable interface {
|
||||
UnmarshalBinary([]byte) error
|
||||
MarshalBinary() (data []byte, err error)
|
||||
}
|
||||
|
||||
// Client is a storable representation of an MQTT client.
|
||||
type Client struct {
|
||||
Will ClientWill `json:"will"` // will topic and payload data if applicable
|
||||
Properties ClientProperties `json:"properties"` // the connect properties for the client
|
||||
Username []byte `json:"username"` // the username of the client
|
||||
ID string `json:"id" storm:"id"` // the client id / storage key
|
||||
T string `json:"t"` // the data type (client)
|
||||
Remote string `json:"remote"` // the remote address of the client
|
||||
Listener string `json:"listener"` // the listener the client connected on
|
||||
ProtocolVersion byte `json:"protocolVersion"` // mqtt protocol version of the client
|
||||
Clean bool `json:"clean"` // if the client requested a clean start/session
|
||||
}
|
||||
|
||||
// ClientProperties contains a limited set of the mqtt v5 properties specific to a client connection.
|
||||
type ClientProperties struct {
|
||||
AuthenticationData []byte `json:"authenticationData,omitempty"`
|
||||
User []packets.UserProperty `json:"user,omitempty"`
|
||||
AuthenticationMethod string `json:"authenticationMethod,omitempty"`
|
||||
SessionExpiryInterval uint32 `json:"sessionExpiryInterval,omitempty"`
|
||||
MaximumPacketSize uint32 `json:"maximumPacketSize,omitempty"`
|
||||
ReceiveMaximum uint16 `json:"receiveMaximum,omitempty"`
|
||||
TopicAliasMaximum uint16 `json:"topicAliasMaximum,omitempty"`
|
||||
SessionExpiryIntervalFlag bool `json:"sessionExpiryIntervalFlag,omitempty"`
|
||||
RequestProblemInfo byte `json:"requestProblemInfo,omitempty"`
|
||||
RequestProblemInfoFlag bool `json:"requestProblemInfoFlag,omitempty"`
|
||||
RequestResponseInfo byte `json:"requestResponseInfo,omitempty"`
|
||||
}
|
||||
|
||||
// ClientWill contains a will message for a client, and limited mqtt v5 properties.
|
||||
type ClientWill struct {
|
||||
Payload []byte `json:"payload,omitempty"`
|
||||
User []packets.UserProperty `json:"user,omitempty"`
|
||||
TopicName string `json:"topicName,omitempty"`
|
||||
Flag uint32 `json:"flag,omitempty"`
|
||||
WillDelayInterval uint32 `json:"willDelayInterval,omitempty"`
|
||||
Qos byte `json:"qos,omitempty"`
|
||||
Retain bool `json:"retain,omitempty"`
|
||||
}
|
||||
|
||||
// MarshalBinary encodes the values into a json string.
|
||||
func (d Client) MarshalBinary() (data []byte, err error) {
|
||||
return json.Marshal(d)
|
||||
}
|
||||
|
||||
// UnmarshalBinary decodes a json string into a struct.
|
||||
func (d *Client) UnmarshalBinary(data []byte) error {
|
||||
if len(data) == 0 {
|
||||
return nil
|
||||
}
|
||||
return json.Unmarshal(data, d)
|
||||
}
|
||||
|
||||
// Message is a storable representation of an MQTT message (specifically publish).
|
||||
type Message struct {
|
||||
Properties MessageProperties `json:"properties"` // -
|
||||
Payload []byte `json:"payload"` // the message payload (if retained)
|
||||
T string `json:"t,omitempty"` // the data type
|
||||
ID string `json:"id,omitempty" storm:"id"` // the storage key
|
||||
Origin string `json:"origin,omitempty"` // the id of the client who sent the message
|
||||
TopicName string `json:"topic_name,omitempty"` // the topic the message was sent to (if retained)
|
||||
FixedHeader packets.FixedHeader `json:"fixedheader"` // the header properties of the message
|
||||
Created int64 `json:"created,omitempty"` // the time the message was created in unixtime
|
||||
Sent int64 `json:"sent,omitempty"` // the last time the message was sent (for retries) in unixtime (if inflight)
|
||||
PacketID uint16 `json:"packet_id,omitempty"` // the unique id of the packet (if inflight)
|
||||
}
|
||||
|
||||
// MessageProperties contains a limited subset of mqtt v5 properties specific to publish messages.
|
||||
type MessageProperties struct {
|
||||
CorrelationData []byte `json:"correlationData,omitempty"`
|
||||
SubscriptionIdentifier []int `json:"subscriptionIdentifier,omitempty"`
|
||||
User []packets.UserProperty `json:"user,omitempty"`
|
||||
ContentType string `json:"contentType,omitempty"`
|
||||
ResponseTopic string `json:"responseTopic,omitempty"`
|
||||
MessageExpiryInterval uint32 `json:"messageExpiry,omitempty"`
|
||||
TopicAlias uint16 `json:"topicAlias,omitempty"`
|
||||
PayloadFormat byte `json:"payloadFormat,omitempty"`
|
||||
PayloadFormatFlag bool `json:"payloadFormatFlag,omitempty"`
|
||||
}
|
||||
|
||||
// MarshalBinary encodes the values into a json string.
|
||||
func (d Message) MarshalBinary() (data []byte, err error) {
|
||||
return json.Marshal(d)
|
||||
}
|
||||
|
||||
// UnmarshalBinary decodes a json string into a struct.
|
||||
func (d *Message) UnmarshalBinary(data []byte) error {
|
||||
if len(data) == 0 {
|
||||
return nil
|
||||
}
|
||||
return json.Unmarshal(data, d)
|
||||
}
|
||||
|
||||
// ToPacket converts a storage.Message to a standard packet.
|
||||
func (d *Message) ToPacket() packets.Packet {
|
||||
pk := packets.Packet{
|
||||
FixedHeader: d.FixedHeader,
|
||||
PacketID: d.PacketID,
|
||||
TopicName: d.TopicName,
|
||||
Payload: d.Payload,
|
||||
Origin: d.Origin,
|
||||
Created: d.Created,
|
||||
Properties: packets.Properties{
|
||||
PayloadFormat: d.Properties.PayloadFormat,
|
||||
PayloadFormatFlag: d.Properties.PayloadFormatFlag,
|
||||
MessageExpiryInterval: d.Properties.MessageExpiryInterval,
|
||||
ContentType: d.Properties.ContentType,
|
||||
ResponseTopic: d.Properties.ResponseTopic,
|
||||
CorrelationData: d.Properties.CorrelationData,
|
||||
SubscriptionIdentifier: d.Properties.SubscriptionIdentifier,
|
||||
TopicAlias: d.Properties.TopicAlias,
|
||||
User: d.Properties.User,
|
||||
},
|
||||
}
|
||||
|
||||
// Return a deep copy of the packet data otherwise the slices will
|
||||
// continue pointing at the values from the storage packet.
|
||||
pk = pk.Copy(true)
|
||||
pk.FixedHeader.Dup = d.FixedHeader.Dup
|
||||
|
||||
return pk
|
||||
}
|
||||
|
||||
// Subscription is a storable representation of an MQTT subscription.
|
||||
type Subscription struct {
|
||||
T string `json:"t,omitempty"`
|
||||
ID string `json:"id,omitempty" storm:"id"`
|
||||
Client string `json:"client,omitempty"`
|
||||
Filter string `json:"filter"`
|
||||
Identifier int `json:"identifier,omitempty"`
|
||||
RetainHandling byte `json:"retain_handling,omitempty"`
|
||||
Qos byte `json:"qos"`
|
||||
RetainAsPublished bool `json:"retain_as_pub,omitempty"`
|
||||
NoLocal bool `json:"no_local,omitempty"`
|
||||
}
|
||||
|
||||
// MarshalBinary encodes the values into a json string.
|
||||
func (d Subscription) MarshalBinary() (data []byte, err error) {
|
||||
return json.Marshal(d)
|
||||
}
|
||||
|
||||
// UnmarshalBinary decodes a json string into a struct.
|
||||
func (d *Subscription) UnmarshalBinary(data []byte) error {
|
||||
if len(data) == 0 {
|
||||
return nil
|
||||
}
|
||||
return json.Unmarshal(data, d)
|
||||
}
|
||||
|
||||
// SystemInfo is a storable representation of the system information values.
|
||||
type SystemInfo struct {
|
||||
system.Info // embed the system info struct
|
||||
T string `json:"t"` // the data type
|
||||
ID string `json:"id" storm:"id"` // the storage key
|
||||
}
|
||||
|
||||
// MarshalBinary 将值编码为json字符串
|
||||
// MarshalBinary encodes the values into a json string.
|
||||
func (d SystemInfo) MarshalBinary() (data []byte, err error) {
|
||||
return json.Marshal(d)
|
||||
}
|
||||
|
||||
// UnmarshalBinary 将json字符串解码为结构体
|
||||
// UnmarshalBinary decodes a json string into a struct.
|
||||
func (d *SystemInfo) UnmarshalBinary(data []byte) error {
|
||||
if len(data) == 0 {
|
||||
return nil
|
||||
}
|
||||
return json.Unmarshal(data, d)
|
||||
}
|
||||
Reference in New Issue
Block a user