| 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 broker |
| 18 |
|
| 19 |
import ( |
| 20 |
"encoding/json" |
| 21 |
"fmt" |
| 22 |
"time" |
| 23 |
|
| 24 |
"gitlab.com/ubports/development/core/lomiri-push-service/protocol" |
| 25 |
"gitlab.com/ubports/development/core/lomiri-push-service/server/store" |
| 26 |
) |
| 27 |
|
| 28 |
// Exchanges |
| 29 |
|
| 30 |
// Scratch area for exchanges, sessions should hold one of these. |
| 31 |
type ExchangesScratchArea struct { |
| 32 |
broadcastMsg protocol.BroadcastMsg |
| 33 |
notificationsMsg protocol.NotificationsMsg |
| 34 |
ackMsg protocol.AckMsg |
| 35 |
} |
| 36 |
|
| 37 |
type BaseExchange struct { |
| 38 |
Timestamp time.Time |
| 39 |
} |
| 40 |
|
| 41 |
// BroadcastExchange leads a session through delivering a BROADCAST. |
| 42 |
// For simplicity it is fully public. |
| 43 |
type BroadcastExchange struct { |
| 44 |
ChanId store.InternalChannelId |
| 45 |
TopLevel int64 |
| 46 |
Notifications []protocol.Notification |
| 47 |
Decoded []map[string]interface{} |
| 48 |
BaseExchange |
| 49 |
} |
| 50 |
|
| 51 |
// check interface already here |
| 52 |
var _ Exchange = (*BroadcastExchange)(nil) |
| 53 |
|
| 54 |
// Init ensures the BroadcastExchange is fully initialized for the sessions. |
| 55 |
func (sbe *BroadcastExchange) Init() { |
| 56 |
decoded := make([]map[string]interface{}, len(sbe.Notifications)) |
| 57 |
sbe.Decoded = decoded |
| 58 |
for i, notif := range sbe.Notifications { |
| 59 |
err := json.Unmarshal(notif.Payload, &decoded[i]) |
| 60 |
if err != nil { |
| 61 |
decoded[i] = nil |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func filterByLevel(clientLevel, topLevel int64, notifs []protocol.Notification) []protocol.Notification { |
| 67 |
c := int64(len(notifs)) |
| 68 |
if c == 0 { |
| 69 |
return nil |
| 70 |
} |
| 71 |
delta := topLevel - clientLevel |
| 72 |
if delta < 0 { // means too ahead, send the last pending |
| 73 |
delta = 1 |
| 74 |
} |
| 75 |
if delta < c { |
| 76 |
return notifs[c-delta:] |
| 77 |
} else { |
| 78 |
return notifs |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
func channelFilter(tag string, chanId store.InternalChannelId, notifs []protocol.Notification, decoded []map[string]interface{}) []json.RawMessage { |
| 83 |
if len(notifs) != 0 && chanId == store.SystemInternalChannelId { |
| 84 |
decoded := decoded[len(decoded)-len(notifs):] |
| 85 |
filtered := make([]json.RawMessage, 0) |
| 86 |
for i, decoded1 := range decoded { |
| 87 |
if _, ok := decoded1[tag]; ok { |
| 88 |
filtered = append(filtered, notifs[i].Payload) |
| 89 |
} |
| 90 |
} |
| 91 |
return filtered |
| 92 |
} |
| 93 |
return protocol.ExtractPayloads(notifs) |
| 94 |
} |
| 95 |
|
| 96 |
// Prepare session for a BROADCAST. |
| 97 |
func (sbe *BroadcastExchange) Prepare(sess BrokerSession) (outMessage protocol.SplittableMsg, inMessage interface{}, err error) { |
| 98 |
clientLevel := sess.Levels()[sbe.ChanId] |
| 99 |
notifs := filterByLevel(clientLevel, sbe.TopLevel, sbe.Notifications) |
| 100 |
tag := fmt.Sprintf("%s/%s", sess.DeviceImageChannel(), sess.DeviceImageModel()) |
| 101 |
payloads := channelFilter(tag, sbe.ChanId, notifs, sbe.Decoded) |
| 102 |
if len(payloads) == 0 && sbe.TopLevel >= clientLevel { |
| 103 |
// empty and don't need to force resync => do nothing |
| 104 |
return nil, nil, ErrNop |
| 105 |
} |
| 106 |
|
| 107 |
scratchArea := sess.ExchangeScratchArea() |
| 108 |
scratchArea.broadcastMsg.Reset() |
| 109 |
// xxx need an AppId as well, later |
| 110 |
scratchArea.broadcastMsg.ChanId = store.InternalChannelIdToHex(sbe.ChanId) |
| 111 |
scratchArea.broadcastMsg.TopLevel = sbe.TopLevel |
| 112 |
scratchArea.broadcastMsg.Payloads = payloads |
| 113 |
return &scratchArea.broadcastMsg, &scratchArea.ackMsg, nil |
| 114 |
} |
| 115 |
|
| 116 |
// Acked deals with an ACK for a BROADCAST. |
| 117 |
func (sbe *BroadcastExchange) Acked(sess BrokerSession, done bool) error { |
| 118 |
scratchArea := sess.ExchangeScratchArea() |
| 119 |
if scratchArea.ackMsg.Type != "ack" { |
| 120 |
return &ErrAbort{"expected ACK message"} |
| 121 |
} |
| 122 |
// update levels |
| 123 |
sess.Levels()[sbe.ChanId] = sbe.TopLevel |
| 124 |
return nil |
| 125 |
} |
| 126 |
|
| 127 |
// ConnMetaExchange allows to send a CONNBROKEN or CONNWARN message. |
| 128 |
type ConnMetaExchange struct { |
| 129 |
Msg protocol.OnewayMsg |
| 130 |
} |
| 131 |
|
| 132 |
// check interface already here |
| 133 |
var _ Exchange = (*ConnMetaExchange)(nil) |
| 134 |
|
| 135 |
// Prepare session for a CONNBROKEN/WARN. |
| 136 |
func (cbe *ConnMetaExchange) Prepare(sess BrokerSession) (outMessage protocol.SplittableMsg, inMessage interface{}, err error) { |
| 137 |
return cbe.Msg, nil, nil |
| 138 |
} |
| 139 |
|
| 140 |
// CONNBROKEN/WARN aren't acked. |
| 141 |
func (cbe *ConnMetaExchange) Acked(sess BrokerSession, done bool) error { |
| 142 |
panic("Acked should not get invoked on ConnMetaExchange") |
| 143 |
} |
| 144 |
|
| 145 |
// UnicastExchange leads a session through delivering a NOTIFICATIONS message. |
| 146 |
// For simplicity it is fully public. |
| 147 |
type UnicastExchange struct { |
| 148 |
ChanId store.InternalChannelId |
| 149 |
CachedOk bool |
| 150 |
BaseExchange |
| 151 |
} |
| 152 |
|
| 153 |
// check interface already here |
| 154 |
var _ Exchange = (*UnicastExchange)(nil) |
| 155 |
|
| 156 |
// Prepare session for a NOTIFICATIONS. |
| 157 |
func (sue *UnicastExchange) Prepare(sess BrokerSession) (outMessage protocol.SplittableMsg, inMessage interface{}, err error) { |
| 158 |
_, notifs, err := sess.Get(sue.ChanId, sue.CachedOk) |
| 159 |
if err != nil { |
| 160 |
return nil, nil, err |
| 161 |
} |
| 162 |
if len(notifs) == 0 { |
| 163 |
return nil, nil, ErrNop |
| 164 |
} |
| 165 |
scratchArea := sess.ExchangeScratchArea() |
| 166 |
scratchArea.notificationsMsg.Reset() |
| 167 |
scratchArea.notificationsMsg.Notifications = notifs |
| 168 |
return &scratchArea.notificationsMsg, &scratchArea.ackMsg, nil |
| 169 |
} |
| 170 |
|
| 171 |
// Acked deals with an ACK for a NOTIFICATIONS. |
| 172 |
func (sue *UnicastExchange) Acked(sess BrokerSession, done bool) error { |
| 173 |
scratchArea := sess.ExchangeScratchArea() |
| 174 |
if scratchArea.ackMsg.Type != "ack" { |
| 175 |
return &ErrAbort{"expected ACK message"} |
| 176 |
} |
| 177 |
err := sess.DropByMsgId(sue.ChanId, scratchArea.notificationsMsg.Notifications) |
| 178 |
if err != nil { |
| 179 |
return err |
| 180 |
} |
| 181 |
return nil |
| 182 |
} |
| 183 |
|
| 184 |
// FeedPending feeds exchanges covering pending notifications into the session. |
| 185 |
func FeedPending(sess BrokerSession) error { |
| 186 |
// find relevant channels, for now only system |
| 187 |
channels := []store.InternalChannelId{store.SystemInternalChannelId} |
| 188 |
for _, chanId := range channels { |
| 189 |
topLevel, notifications, err := sess.Get(chanId, true) |
| 190 |
if err != nil { |
| 191 |
// next broadcast will try again |
| 192 |
continue |
| 193 |
} |
| 194 |
clientLevel := sess.Levels()[chanId] |
| 195 |
if clientLevel != topLevel { |
| 196 |
broadcastExchg := &BroadcastExchange{ |
| 197 |
ChanId: chanId, |
| 198 |
TopLevel: topLevel, |
| 199 |
Notifications: notifications, |
| 200 |
} |
| 201 |
broadcastExchg.Init() |
| 202 |
sess.Feed(broadcastExchg) |
| 203 |
} |
| 204 |
} |
| 205 |
sess.Feed(&UnicastExchange{ChanId: sess.InternalChannelId(), CachedOk: true}) |
| 206 |
return nil |
| 207 |
} |