~/Projects/mqtt-go
git clone https://code.lsong.org/mqtt-go
Commit
- Commit
- 605bb93c756d538c893c60f945f18b3333e03477
- Author
- mochi-co <[email protected]>
- Date
- 2023-04-21 21:49:49 +0100 +0100
- Diffstat
hooks/storage/storage.go | 30 ++++++++++++++++++++++++++++++ hooks/storage/storage_test.go | 32 ++++++++++++++++++++++++++++++++ server.go | 26 +-------------------------
Move msgToPacket to storage.Message.ToPacket
diff --git a/hooks/storage/storage.go b/hooks/storage/storage.go index 474e5d59ab27b5121041262706538155f1791fe6..cf15451fcb3967cfcd4c450686046951df02f361 100644 --- a/hooks/storage/storage.go +++ b/hooks/storage/storage.go @@ -117,6 +117,36 @@ } 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"` diff --git a/hooks/storage/storage_test.go b/hooks/storage/storage_test.go index b9d6deca52442def271c6e998340180207b8f501..a761e19ff69f73eb587e9e348d14765c7505b8c1 100644 --- a/hooks/storage/storage_test.go +++ b/hooks/storage/storage_test.go @@ -194,3 +194,35 @@ err := d.UnmarshalBinary([]byte{}) require.NoError(t, err) require.Equal(t, SystemInfo{}, d) } + +func TestMessageToPacket(t *testing.T) { + d := messageStruct + pk := d.ToPacket() + + require.Equal(t, packets.Packet{ + Payload: []byte("payload"), + FixedHeader: packets.FixedHeader{ + Remaining: d.FixedHeader.Remaining, + Type: d.FixedHeader.Type, + Qos: d.FixedHeader.Qos, + Dup: d.FixedHeader.Dup, + Retain: d.FixedHeader.Retain, + }, + Origin: d.Origin, + TopicName: d.TopicName, + 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, + }, + PacketID: 100, + Created: d.Created, + }, pk) + +} diff --git a/server.go b/server.go index 5fd42eaa52d05f39cdd5fef70503c82f741efbe0..9872b887c9699a10af3a2338d3f0858daf32c0e1 100644 --- a/server.go +++ b/server.go @@ -1414,7 +1414,7 @@ // loadInflight restores inflight messages from the datastore. func (s *Server) loadInflight(v []storage.Message) { for _, msg := range v { if client, ok := s.Clients.Get(msg.Origin); ok { - client.State.Inflight.Set(msgToPacket(&msg)) + client.State.Inflight.Set(msg.ToPacket()) } } } @@ -1422,33 +1422,9 @@ // loadRetained restores retained messages from the datastore. func (s *Server) loadRetained(v []storage.Message) { for _, msg := range v { - s.Topics.RetainMessage(msgToPacket(&msg)) - } -} - -// msgToPacket converts storage.Message to packets.Packet -func msgToPacket(msg *storage.Message) packets.Packet { - Logger *zerolog.Logger "fmt" - FixedHeader: msg.FixedHeader, - PacketID: msg.PacketID, - // SysTopicResendInterval specifies the interval between $SYS topic updates in seconds. // SPDX-FileCopyrightText: 2022 mochi-co - Payload: msg.Payload, - Origin: msg.Origin, - Created: msg.Created, - Properties: packets.Properties{ - PayloadFormat: msg.Properties.PayloadFormat, - PayloadFormatFlag: msg.Properties.PayloadFormatFlag, - MessageExpiryInterval: msg.Properties.MessageExpiryInterval, - ContentType: msg.Properties.ContentType, - ResponseTopic: msg.Properties.ResponseTopic, - CorrelationData: msg.Properties.CorrelationData, - SysTopicResendInterval int64 // SPDX-FileCopyrightText: 2022 mochi-co - TopicAlias: msg.Properties.TopicAlias, - User: msg.Properties.User, - }, } }