Skip to content

Commit 5220e7b

Browse files
committed
Add Connect.SetWillDelayInterval
1 parent 5d93b01 commit 5220e7b

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

changelog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ and this project adheres to http://semver.org/spec/v2.0.0.html.
77
## [0.28.1-dev]
88

99
- Fix missing payload of Connect.Will
10-
- Connect.Will returns WillDelayInterval delay
10+
- Connect.Will only sets the will, default delay interval is 0
11+
- Add Connect.SetWillDelayInterval
1112

1213
## [0.28.0] 2024-09-21
1314

connect.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,26 @@ type Connect struct {
6464
// Connect fields are exposed using methods to simplify the type
6565
// conversion.
6666

67-
// SetWill sets the will message and delay in seconds. The Server
68-
// delays publishing the Client’s Will Message until the Will Delay
69-
// Interval has passed or the Session ends, whichever happens first.
70-
func (p *Connect) SetWill(will *Publish, delayInterval uint32) {
67+
// SetWill sets the will message. The Server delays publishing the
68+
// Client’s Will Message until the Will Delay Interval has passed or
69+
// the Session ends, whichever happens first.
70+
func (p *Connect) SetWill(will *Publish) {
7171
p.will = will
7272
p.flags.toggle(WillFlag, true)
7373
p.flags.toggle(WillRetain, will.Retain())
74-
p.willDelayInterval = wuint32(delayInterval)
7574
p.willPayload = bindata(will.payload)
7675
p.setWillQoS(will.QoS())
7776
}
7877

79-
func (p *Connect) Will() (*Publish, uint32) {
80-
return p.will, uint32(p.willDelayInterval)
78+
func (p *Connect) SetWillDelayInterval(delayInterval uint32) {
79+
p.willDelayInterval = wuint32(delayInterval)
8180
}
81+
func (p *Connect) WillDelayInterval() uint32 {
82+
return uint32(p.willDelayInterval)
83+
}
84+
85+
// Will returns the will publish message.
86+
func (p *Connect) Will() *Publish { return p.will }
8287

8388
func (p *Connect) HasFlag(v byte) bool { return p.flags.Has(v) }
8489

connect_test.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ func ExampleConnect_String() {
3131
p.SetClientID("pink")
3232
p.SetUsername("gopher")
3333
p.SetPassword([]byte("cute"))
34-
p.SetWill(Pub(1, "client/gone", "pink"), 3)
34+
p.SetWill(Pub(1, "client/gone", "pink"))
35+
p.SetWillDelayInterval(3)
3536

3637
fmt.Println(p.String())
3738
fmt.Println(DocumentFlags(p))
@@ -72,12 +73,12 @@ func TestConnect(t *testing.T) {
7273
if got := c.String(); !strings.Contains(got, "CONNECT") {
7374
t.Error(got)
7475
}
75-
if v, _ := c.Will(); v != nil {
76+
if v := c.Will(); v != nil {
7677
t.Error("no will was set but got", v)
7778
}
7879
testControlPacket(t, c)
7980

80-
c.SetWill(Pub(1, "client/gone", "pink"), 3)
81+
c.SetWill(Pub(1, "client/gone", "pink"))
8182
testControlPacket(t, c)
8283

8384
// clears it
@@ -106,7 +107,7 @@ func TestDump_connect(t *testing.T) {
106107
c.SetKeepAlive(299)
107108
c.SetUsername("john.doe")
108109
c.SetPassword([]byte("secret"))
109-
c.SetWill(Pub(0, "client/gone", "macy"), 300)
110+
c.SetWill(Pub(0, "client/gone", "macy"))
110111
c.AddUserProp("color", "red")
111112

112113
var buf bytes.Buffer
@@ -189,9 +190,9 @@ func TestCompareConnect(t *testing.T) {
189190
p.SetQoS(2)
190191
p.AddUserProp("connected", "2022-01-01 14:44:32")
191192
//p.SetCorrelationData([]byte("11-22-33")) doesn't work in paho
192-
our.SetWill(p, 3)
193+
our.SetWill(p)
193194
}
194-
will, wExp := our.Will()
195+
will := our.Will()
195196
the.WillRetain = will.Retain()
196197
the.WillFlag = our.HasFlag(WillFlag)
197198
the.WillTopic = will.TopicName()
@@ -205,6 +206,7 @@ func TestCompareConnect(t *testing.T) {
205206
Key: "connected",
206207
Value: "2022-01-01 14:44:32",
207208
})
209+
wExp := our.WillDelayInterval()
208210
the.WillProperties.WillDelayInterval = &wExp
209211

210212
// possible bug in Properties.Pack
@@ -318,8 +320,9 @@ func BenchmarkConnectWill(b *testing.B) {
318320
w.AddUserProp("color", "red")
319321
w.SetContentType("text/plain")
320322
w.SetPayload([]byte("gopher"))
321-
p.SetWill(w, 5)
322323

324+
p.SetWill(w)
325+
p.SetWillDelayInterval(5)
323326
p.WriteTo(&buf)
324327
ReadPacket(&buf)
325328
}

example_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ func Example_packetReadWrite() {
1414
p.SetClientID("pink")
1515
p.SetUsername("gopher")
1616
p.SetPassword([]byte("cute"))
17-
p.SetWill(mq.Pub(1, "client/gone", "pink"), 7)
17+
p.SetWill(mq.Pub(1, "client/gone", "pink"))
18+
p.SetWillDelayInterval(7)
1819
p.WriteTo(&buf)
1920
}
2021
{ // read the packet

0 commit comments

Comments
 (0)