| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package main |
| 17 |
|
| 18 |
import ( |
| 19 |
"bytes" |
| 20 |
"context" |
| 21 |
"encoding/binary" |
| 22 |
"fmt" |
| 23 |
"net/url" |
| 24 |
"time" |
| 25 |
|
| 26 |
"github.com/eclipse/paho.golang/autopaho" |
| 27 |
"github.com/eclipse/paho.golang/paho" |
| 28 |
) |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
func subscribe(ctx context.Context, serverURL *url.URL, msgCount uint64, ready chan struct{}) { |
| 37 |
var msgRcvCount uint64 |
| 38 |
msgReceived := make(map[uint64]bool, msgCount) |
| 39 |
allReceived := make(chan struct{}) |
| 40 |
|
| 41 |
ping := make(chan uint64) |
| 42 |
|
| 43 |
cliCfg := autopaho.ClientConfig{ |
| 44 |
ServerUrls: []*url.URL{serverURL}, |
| 45 |
KeepAlive: 20, |
| 46 |
CleanStartOnInitialConnection: true, |
| 47 |
SessionExpiryInterval: 60, |
| 48 |
OnConnectionUp: func(cm *autopaho.ConnectionManager, connAck *paho.Connack) { |
| 49 |
fmt.Println("mqtt connection up") |
| 50 |
if _, err := cm.Subscribe(ctx, &paho.Subscribe{ |
| 51 |
Subscriptions: []paho.SubscribeOptions{ |
| 52 |
{Topic: testTopic, QoS: QOS}, |
| 53 |
}, |
| 54 |
}); err != nil { |
| 55 |
fmt.Printf("subscribe: failed to subscribe (%s). Probably due to connection drop so will retry\n", err) |
| 56 |
return |
| 57 |
} |
| 58 |
fmt.Println("subscribe: mqtt subscription made") |
| 59 |
if ready != nil { |
| 60 |
close(ready) |
| 61 |
ready = nil |
| 62 |
} |
| 63 |
}, |
| 64 |
OnConnectError: func(err error) { fmt.Printf("subscribe: error whilst attempting connection: %s\n", err) }, |
| 65 |
Errors: logger{prefix: "subscribe"}, |
| 66 |
|
| 67 |
|
| 68 |
ClientConfig: paho.ClientConfig{ |
| 69 |
ClientID: "TestSub", |
| 70 |
OnPublishReceived: []func(paho.PublishReceived) (bool, error){ |
| 71 |
func(pr paho.PublishReceived) (bool, error) { |
| 72 |
msgNo, err := binary.ReadUvarint(bytes.NewReader(pr.Packet.Payload)) |
| 73 |
if err != nil { |
| 74 |
panic(err) |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
newMessage := false |
| 79 |
|
| 80 |
if QOS == 1 { |
| 81 |
|
| 82 |
if !msgReceived[msgNo] { |
| 83 |
newMessage = true |
| 84 |
} |
| 85 |
} else { |
| 86 |
|
| 87 |
if msgReceived[msgNo] { |
| 88 |
panic(fmt.Sprintf("subscribe: message # %d already received", msgNo)) |
| 89 |
} |
| 90 |
newMessage = true |
| 91 |
} |
| 92 |
if newMessage { |
| 93 |
msgReceived[msgNo] = true |
| 94 |
msgRcvCount++ |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
if msgNo > 1 && !msgReceived[msgNo-1] { |
| 99 |
panic(fmt.Sprintf("subscribe: message # %d received but we don't have previous QOS2 msg", msgNo)) |
| 100 |
} |
| 101 |
if msgReceived[msgNo+1] { |
| 102 |
panic(fmt.Sprintf("subscribe: message # %d received but we have already received next QOS2 msg ", msgNo)) |
| 103 |
} |
| 104 |
|
| 105 |
ping <- msgRcvCount |
| 106 |
if msgRcvCount%NotifyEvery == 0 { |
| 107 |
fmt.Printf("subscribe: received %d messages\n", msgRcvCount) |
| 108 |
} |
| 109 |
if msgRcvCount == msgCount { |
| 110 |
close(allReceived) |
| 111 |
} |
| 112 |
} |
| 113 |
return true, nil |
| 114 |
}}, |
| 115 |
OnClientError: func(err error) { fmt.Printf("subscribe: client error: %s\n", err) }, |
| 116 |
OnServerDisconnect: func(d *paho.Disconnect) { |
| 117 |
if d.Properties != nil { |
| 118 |
fmt.Printf("subscribe: server requested disconnect: %s\n", d.Properties.ReasonString) |
| 119 |
} else { |
| 120 |
fmt.Printf("subscribe:server requested disconnect; reason code: %d\n", d.ReasonCode) |
| 121 |
} |
| 122 |
}, |
| 123 |
}, |
| 124 |
} |
| 125 |
|
| 126 |
c, err := autopaho.NewConnection(ctx, cliCfg) |
| 127 |
if err != nil { |
| 128 |
panic(err) |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
lastMsgTime := time.Now() |
| 133 |
tick := time.NewTicker(timeoutSecs * time.Second) |
| 134 |
defer tick.Stop() |
| 135 |
|
| 136 |
waitLoop: |
| 137 |
for { |
| 138 |
select { |
| 139 |
case <-ctx.Done(): |
| 140 |
err = ctx.Err() |
| 141 |
break waitLoop |
| 142 |
case <-allReceived: |
| 143 |
break waitLoop |
| 144 |
case <-ping: |
| 145 |
lastMsgTime = time.Now() |
| 146 |
continue |
| 147 |
case <-tick.C: |
| 148 |
if time.Since(lastMsgTime) > timeoutSecs*time.Second { |
| 149 |
var missing []uint64 |
| 150 |
for i := uint64(1); i <= msgCount; i++ { |
| 151 |
if !msgReceived[i] { |
| 152 |
missing = append(missing, i) |
| 153 |
} |
| 154 |
} |
| 155 |
fmt.Printf("subscribe: Aborting because no messages received (received %d unique messages - missing %v)\n", msgRcvCount, missing) |
| 156 |
break waitLoop |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
if err != nil { |
| 162 |
fmt.Println("subscribe: Aborting due to: ", err) |
| 163 |
return |
| 164 |
} |
| 165 |
|
| 166 |
fmt.Println("subscribe: All received, disconnecting") |
| 167 |
if err = c.Disconnect(context.Background()); err != nil { |
| 168 |
panic(err) |
| 169 |
} |
| 170 |
<-c.Done() |
| 171 |
fmt.Println("subscribe: Done") |
| 172 |
} |