| 1 |
/* |
| 2 |
Copyright 2013-2014 Canonical Ltd. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify it |
| 5 |
under the terms of the GNU General Public License version 3, as published |
| 6 |
by the Free Software Foundation. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, but |
| 9 |
WITHOUT ANY WARRANTY; without even the implied warranties of |
| 10 |
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 11 |
PURPOSE. See the GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License along |
| 14 |
with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
// Package simple implements a simple broker for just one process. |
| 18 |
package simple |
| 19 |
|
| 20 |
import ( |
| 21 |
"sync" |
| 22 |
|
| 23 |
"gitlab.com/ubports/development/core/lomiri-push-service/logger" |
| 24 |
"gitlab.com/ubports/development/core/lomiri-push-service/protocol" |
| 25 |
"gitlab.com/ubports/development/core/lomiri-push-service/server/broker" |
| 26 |
"gitlab.com/ubports/development/core/lomiri-push-service/server/store" |
| 27 |
"gitlab.com/ubports/development/core/lomiri-push-service/server/statistics" |
| 28 |
) |
| 29 |
|
| 30 |
// SimpleBroker implements broker.Broker/BrokerSending for everything |
| 31 |
// in just one process. |
| 32 |
type SimpleBroker struct { |
| 33 |
sto store.PendingStore |
| 34 |
logger logger.Logger |
| 35 |
// running state |
| 36 |
runMutex sync.Mutex |
| 37 |
running bool |
| 38 |
stop chan bool |
| 39 |
stopped chan bool |
| 40 |
// sessions |
| 41 |
sessionCh chan *simpleBrokerSession |
| 42 |
registry map[string]*simpleBrokerSession |
| 43 |
sessionQueueSize uint |
| 44 |
// delivery |
| 45 |
deliveryCh chan *delivery |
| 46 |
currentStats *statistics.Statistics |
| 47 |
} |
| 48 |
|
| 49 |
// simpleBrokerSession represents a session in the broker. |
| 50 |
type simpleBrokerSession struct { |
| 51 |
broker *SimpleBroker |
| 52 |
registered bool |
| 53 |
deviceId string |
| 54 |
model string |
| 55 |
imageChannel string |
| 56 |
done chan bool |
| 57 |
exchanges chan broker.Exchange |
| 58 |
levels broker.LevelsMap |
| 59 |
// for exchanges |
| 60 |
exchgScratch broker.ExchangesScratchArea |
| 61 |
} |
| 62 |
|
| 63 |
type deliveryKind int |
| 64 |
|
| 65 |
const ( |
| 66 |
broadcastDelivery deliveryKind = iota |
| 67 |
unicastDelivery |
| 68 |
) |
| 69 |
|
| 70 |
// delivery holds all the information to request a delivery |
| 71 |
type delivery struct { |
| 72 |
kind deliveryKind |
| 73 |
chanId store.InternalChannelId |
| 74 |
} |
| 75 |
|
| 76 |
func (sess *simpleBrokerSession) SessionChannel() <-chan broker.Exchange { |
| 77 |
return sess.exchanges |
| 78 |
} |
| 79 |
|
| 80 |
func (sess *simpleBrokerSession) DeviceIdentifier() string { |
| 81 |
return sess.deviceId |
| 82 |
} |
| 83 |
|
| 84 |
func (sess *simpleBrokerSession) DeviceImageModel() string { |
| 85 |
return sess.model |
| 86 |
} |
| 87 |
|
| 88 |
func (sess *simpleBrokerSession) DeviceImageChannel() string { |
| 89 |
return sess.imageChannel |
| 90 |
} |
| 91 |
|
| 92 |
func (sess *simpleBrokerSession) Levels() broker.LevelsMap { |
| 93 |
return sess.levels |
| 94 |
} |
| 95 |
|
| 96 |
func (sess *simpleBrokerSession) ExchangeScratchArea() *broker.ExchangesScratchArea { |
| 97 |
return &sess.exchgScratch |
| 98 |
} |
| 99 |
|
| 100 |
func (sess *simpleBrokerSession) Get(chanId store.InternalChannelId, cachedOk bool) (int64, []protocol.Notification, error) { |
| 101 |
return sess.broker.get(chanId, cachedOk) |
| 102 |
} |
| 103 |
|
| 104 |
func (sess *simpleBrokerSession) DropByMsgId(chanId store.InternalChannelId, targets []protocol.Notification) error { |
| 105 |
return sess.broker.drop(chanId, targets) |
| 106 |
} |
| 107 |
|
| 108 |
func (sess *simpleBrokerSession) Feed(exchg broker.Exchange) { |
| 109 |
sess.exchanges <- exchg |
| 110 |
} |
| 111 |
|
| 112 |
func (sess *simpleBrokerSession) InternalChannelId() store.InternalChannelId { |
| 113 |
return store.UnicastInternalChannelId(sess.deviceId, sess.deviceId) |
| 114 |
} |
| 115 |
|
| 116 |
// NewSimpleBroker makes a new SimpleBroker. |
| 117 |
func NewSimpleBroker(sto store.PendingStore, cfg broker.BrokerConfig, logger logger.Logger, currentStats *statistics.Statistics) *SimpleBroker { |
| 118 |
sessionCh := make(chan *simpleBrokerSession, cfg.BrokerQueueSize()) |
| 119 |
deliveryCh := make(chan *delivery, cfg.BrokerQueueSize()) |
| 120 |
registry := make(map[string]*simpleBrokerSession) |
| 121 |
return &SimpleBroker{ |
| 122 |
logger: logger, |
| 123 |
sto: sto, |
| 124 |
stop: make(chan bool), |
| 125 |
stopped: make(chan bool), |
| 126 |
registry: registry, |
| 127 |
sessionCh: sessionCh, |
| 128 |
deliveryCh: deliveryCh, |
| 129 |
sessionQueueSize: cfg.SessionQueueSize(), |
| 130 |
currentStats: currentStats, |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
// Start starts the broker. |
| 135 |
func (b *SimpleBroker) Start() { |
| 136 |
b.runMutex.Lock() |
| 137 |
defer b.runMutex.Unlock() |
| 138 |
if b.running { |
| 139 |
return |
| 140 |
} |
| 141 |
b.running = true |
| 142 |
go b.run() |
| 143 |
} |
| 144 |
|
| 145 |
// Stop stops the broker. |
| 146 |
func (b *SimpleBroker) Stop() { |
| 147 |
b.runMutex.Lock() |
| 148 |
defer b.runMutex.Unlock() |
| 149 |
if !b.running { |
| 150 |
return |
| 151 |
} |
| 152 |
b.stop <- true |
| 153 |
<-b.stopped |
| 154 |
b.running = false |
| 155 |
} |
| 156 |
|
| 157 |
// Running returns whether ther broker is running. |
| 158 |
func (b *SimpleBroker) Running() bool { |
| 159 |
b.runMutex.Lock() |
| 160 |
defer b.runMutex.Unlock() |
| 161 |
return b.running |
| 162 |
} |
| 163 |
|
| 164 |
// Register registers a session with the broker. It feeds the session |
| 165 |
// pending notifications as well. |
| 166 |
func (b *SimpleBroker) Register(connect *protocol.ConnectMsg, track broker.SessionTracker) (broker.BrokerSession, error) { |
| 167 |
// xxx sanity check DeviceId |
| 168 |
model, err := broker.GetInfoString(connect, "device", "?") |
| 169 |
if err != nil { |
| 170 |
return nil, err |
| 171 |
} |
| 172 |
imageChannel, err := broker.GetInfoString(connect, "channel", "?") |
| 173 |
if err != nil { |
| 174 |
return nil, err |
| 175 |
} |
| 176 |
levels := map[store.InternalChannelId]int64{} |
| 177 |
for hexId, v := range connect.Levels { |
| 178 |
id, err := store.HexToInternalChannelId(hexId) |
| 179 |
if err != nil { |
| 180 |
return nil, &broker.ErrAbort{err.Error()} |
| 181 |
} |
| 182 |
levels[id] = v |
| 183 |
} |
| 184 |
sess := &simpleBrokerSession{ |
| 185 |
broker: b, |
| 186 |
deviceId: connect.DeviceId, |
| 187 |
model: model, |
| 188 |
imageChannel: imageChannel, |
| 189 |
done: make(chan bool), |
| 190 |
exchanges: make(chan broker.Exchange, b.sessionQueueSize), |
| 191 |
levels: levels, |
| 192 |
} |
| 193 |
b.sessionCh <- sess |
| 194 |
<-sess.done |
| 195 |
err = broker.FeedPending(sess) |
| 196 |
if err != nil { |
| 197 |
return nil, err |
| 198 |
} |
| 199 |
b.logger.Infof("Registered the following device info: %v %v", sess.model, sess.imageChannel) |
| 200 |
if b.currentStats != nil { |
| 201 |
b.currentStats.IncreaseDevices(sess.model, sess.imageChannel) |
| 202 |
} |
| 203 |
return sess, nil |
| 204 |
} |
| 205 |
|
| 206 |
// Unregister unregisters a session with the broker. Doesn't wait. |
| 207 |
func (b *SimpleBroker) Unregister(s broker.BrokerSession) { |
| 208 |
sess := s.(*simpleBrokerSession) |
| 209 |
b.sessionCh <- sess |
| 210 |
if b.currentStats != nil { |
| 211 |
b.currentStats.DecreaseDevices(sess.model, sess.imageChannel) |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
func (b *SimpleBroker) get(chanId store.InternalChannelId, cachedOk bool) (int64, []protocol.Notification, error) { |
| 216 |
topLevel, notifications, err := b.sto.GetChannelSnapshot(chanId) |
| 217 |
if err != nil { |
| 218 |
b.logger.Errorf("unsuccessful, get channel snapshot for %v (cachedOk=%v): %v", chanId, cachedOk, err) |
| 219 |
} |
| 220 |
return topLevel, notifications, err |
| 221 |
|
| 222 |
} |
| 223 |
|
| 224 |
func (b *SimpleBroker) drop(chanId store.InternalChannelId, targets []protocol.Notification) error { |
| 225 |
err := b.sto.DropByMsgId(chanId, targets) |
| 226 |
if err != nil { |
| 227 |
b.logger.Errorf("unsuccessful, drop from channel %v: %v", chanId, err) |
| 228 |
} |
| 229 |
return err |
| 230 |
|
| 231 |
} |
| 232 |
|
| 233 |
// run runs the agent logic of the broker. |
| 234 |
func (b *SimpleBroker) run() { |
| 235 |
Loop: |
| 236 |
for { |
| 237 |
select { |
| 238 |
case <-b.stop: |
| 239 |
b.stopped <- true |
| 240 |
break Loop |
| 241 |
case sess := <-b.sessionCh: |
| 242 |
if sess.registered { // unregister |
| 243 |
// unregister only current |
| 244 |
if b.registry[sess.deviceId] == sess { |
| 245 |
delete(b.registry, sess.deviceId) |
| 246 |
} |
| 247 |
} else { // register |
| 248 |
prev := b.registry[sess.deviceId] |
| 249 |
if prev != nil { // kick it |
| 250 |
prev.exchanges <- nil |
| 251 |
} |
| 252 |
b.registry[sess.deviceId] = sess |
| 253 |
sess.registered = true |
| 254 |
sess.done <- true |
| 255 |
} |
| 256 |
case delivery := <-b.deliveryCh: |
| 257 |
switch delivery.kind { |
| 258 |
case broadcastDelivery: |
| 259 |
topLevel, notifications, err := b.get(delivery.chanId, false) |
| 260 |
if err != nil { |
| 261 |
// next broadcast will try again |
| 262 |
continue Loop |
| 263 |
} |
| 264 |
broadcastExchg := &broker.BroadcastExchange{ |
| 265 |
ChanId: delivery.chanId, |
| 266 |
TopLevel: topLevel, |
| 267 |
Notifications: notifications, |
| 268 |
} |
| 269 |
broadcastExchg.Init() |
| 270 |
for _, sess := range b.registry { |
| 271 |
sess.exchanges <- broadcastExchg |
| 272 |
} |
| 273 |
if b.currentStats != nil { |
| 274 |
b.currentStats.IncreaseBroadcasts() |
| 275 |
} |
| 276 |
case unicastDelivery: |
| 277 |
chanId := delivery.chanId |
| 278 |
_, devId := chanId.UnicastUserAndDevice() |
| 279 |
sess := b.registry[devId] |
| 280 |
if sess != nil { |
| 281 |
sess.exchanges <- &broker.UnicastExchange{ChanId: chanId, CachedOk: false} |
| 282 |
} |
| 283 |
if b.currentStats != nil { |
| 284 |
b.currentStats.IncreaseUnicasts() |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
// Broadcast requests the broadcast for a channel. |
| 292 |
func (b *SimpleBroker) Broadcast(chanId store.InternalChannelId) { |
| 293 |
b.deliveryCh <- &delivery{ |
| 294 |
kind: broadcastDelivery, |
| 295 |
chanId: chanId, |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
// Unicast requests unicast for the channels. |
| 300 |
func (b *SimpleBroker) Unicast(chanIds ...store.InternalChannelId) { |
| 301 |
for _, chanId := range chanIds { |
| 302 |
b.deliveryCh <- &delivery{ |
| 303 |
kind: unicastDelivery, |
| 304 |
chanId: chanId, |
| 305 |
} |
| 306 |
} |
| 307 |
} |