| 1 |
/* |
| 2 |
* Copyright (c) 2024 Contributors to the Eclipse Foundation |
| 3 |
* |
| 4 |
* All rights reserved. This program and the accompanying materials |
| 5 |
* are made available under the terms of the Eclipse Public License v2.0 |
| 6 |
* and Eclipse Distribution License v1.0 which accompany this distribution. |
| 7 |
* |
| 8 |
* The Eclipse Public License is available at |
| 9 |
* https://www.eclipse.org/legal/epl-2.0/ |
| 10 |
* and the Eclipse Distribution License is available at |
| 11 |
* http://www.eclipse.org/org/documents/edl-v10.php. |
| 12 |
* |
| 13 |
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
| 14 |
*/ |
| 15 |
|
| 16 |
package topicaliases |
| 17 |
|
| 18 |
import ( |
| 19 |
"sync" |
| 20 |
|
| 21 |
"github.com/eclipse/paho.golang/paho" |
| 22 |
) |
| 23 |
|
| 24 |
type TAHandler struct { |
| 25 |
sync.Mutex |
| 26 |
aliasMax uint16 |
| 27 |
aliases []string |
| 28 |
} |
| 29 |
|
| 30 |
func NewTAHandler(max uint16) *TAHandler { |
| 31 |
return &TAHandler{ |
| 32 |
aliasMax: max + 1, |
| 33 |
aliases: make([]string, max+1), |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
// GetTopic will return the topic for a given alias number |
| 38 |
func (t *TAHandler) GetTopic(a uint16) string { |
| 39 |
if a > t.aliasMax { |
| 40 |
return "" |
| 41 |
} |
| 42 |
t.Lock() |
| 43 |
defer t.Unlock() |
| 44 |
|
| 45 |
return t.aliases[a] |
| 46 |
} |
| 47 |
|
| 48 |
// GetAlias will return the alias for a given topic string |
| 49 |
func (t *TAHandler) GetAlias(topic string) uint16 { |
| 50 |
for i, s := range t.aliases { |
| 51 |
if s == topic { |
| 52 |
return uint16(i) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
return 0 |
| 57 |
} |
| 58 |
|
| 59 |
// SetAlias will request an alias number for a given string |
| 60 |
func (t *TAHandler) SetAlias(topic string) uint16 { |
| 61 |
t.Lock() |
| 62 |
defer t.Unlock() |
| 63 |
|
| 64 |
for i := uint16(1); i <= t.aliasMax; i++ { |
| 65 |
if t.aliases[i] == "" { |
| 66 |
t.aliases[i] = topic |
| 67 |
return i |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
return 0 |
| 72 |
} |
| 73 |
|
| 74 |
// ResetAlias reassigns a given alias number for a new topic |
| 75 |
func (t *TAHandler) ResetAlias(topic string, a uint16) { |
| 76 |
t.Lock() |
| 77 |
defer t.Unlock() |
| 78 |
|
| 79 |
t.aliases[a] = topic |
| 80 |
} |
| 81 |
|
| 82 |
// ResetAll resets all alias value. |
| 83 |
// Since topic aliases are not carried over upon reconnection, |
| 84 |
// ResetAll should be called during connection |
| 85 |
func (t *TAHandler) ResetAll() { |
| 86 |
t.Lock() |
| 87 |
defer t.Unlock() |
| 88 |
|
| 89 |
clear(t.aliases) |
| 90 |
} |
| 91 |
|
| 92 |
// PublishHook is designed to be given to an MQTT client and will be executed |
| 93 |
// before a publish is sent allowing it to modify the Properties of the packet. |
| 94 |
// In this case it allows the Topic Alias Handler to automatically replace topic |
| 95 |
// names with alias numbers |
| 96 |
func (t *TAHandler) PublishHook(p *paho.Publish) { |
| 97 |
// p.Topic is always not "" as the default publish checks before calling hooks |
| 98 |
if p.Properties != nil && p.Properties.TopicAlias != nil { |
| 99 |
// topic string is not empty and topic alias is set, reset the alias value. |
| 100 |
t.ResetAlias(p.Topic, *p.Properties.TopicAlias) |
| 101 |
return |
| 102 |
} |
| 103 |
|
| 104 |
// we already have an alias, set it and unset the topic |
| 105 |
if a := t.GetAlias(p.Topic); a != 0 { |
| 106 |
if p.Properties == nil { |
| 107 |
p.Properties = &paho.PublishProperties{} |
| 108 |
} |
| 109 |
p.Properties.TopicAlias = paho.Uint16(a) |
| 110 |
p.Topic = "" |
| 111 |
return |
| 112 |
} |
| 113 |
|
| 114 |
// we don't have an alias, try and get one |
| 115 |
if a := t.SetAlias(p.Topic); a != 0 { |
| 116 |
if p.Properties == nil { |
| 117 |
p.Properties = &paho.PublishProperties{} |
| 118 |
} |
| 119 |
p.Properties.TopicAlias = paho.Uint16(a) |
| 120 |
return |
| 121 |
} |
| 122 |
} |