Debian Codegraph @

server/broker/broker.go

raw
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 handles session registrations and delivery of messages
18 // through sessions.
19 package broker
20
21 import (
22 "errors"
23 "fmt"
24
25 "gitlab.com/ubports/development/core/lomiri-push-service/protocol"
26 "gitlab.com/ubports/development/core/lomiri-push-service/server/store"
27 )
28
29 type SessionTracker interface {
30 // SessionId
31 SessionId() string
32 }
33
34 // Broker is responsible for registring sessions and delivering messages
35 // through them.
36 type Broker interface {
37 // Register the session.
38 Register(connMsg *protocol.ConnectMsg, track SessionTracker) (BrokerSession, error)
39 // Unregister the session.
40 Unregister(BrokerSession)
41 }
42
43 // BrokerSending is the notification sending facet of the broker.
44 type BrokerSending interface {
45 // Broadcast channel.
46 Broadcast(chanId store.InternalChannelId)
47 // Unicast over channels.
48 Unicast(chanIds ...store.InternalChannelId)
49 }
50
51 // Exchange leads the session through performing an exchange, typically delivery.
52 type Exchange interface {
53 Prepare(sess BrokerSession) (outMessage protocol.SplittableMsg, inMessage interface{}, err error)
54 Acked(sess BrokerSession, done bool) error
55 }
56
57 // ErrNop returned by Prepare means nothing to do/send.
58 var ErrNop = errors.New("nothing to send")
59
60 // LevelsMap is the type for holding channel levels for session.
61 type LevelsMap map[store.InternalChannelId]int64
62
63 // GetInfoString helps retrieveng a string out of a protocol.ConnectMsg.Info.
64 func GetInfoString(msg *protocol.ConnectMsg, name, defaultVal string) (string, error) {
65 v, ok := msg.Info[name]
66 if !ok {
67 return defaultVal, nil
68 }
69 s, ok := v.(string)
70 if !ok {
71 return "", ErrUnexpectedValue
72 }
73 return s, nil
74 }
75
76 // GetInfoInt helps retrieving an integer out of a protocol.ConnectMsg.Info.
77 func GetInfoInt(msg *protocol.ConnectMsg, name string, defaultVal int) (int, error) {
78 v, ok := msg.Info[name]
79 if !ok {
80 return defaultVal, nil
81 }
82 n, ok := v.(float64)
83 if !ok {
84 return -1, ErrUnexpectedValue
85 }
86 return int(n), nil
87 }
88
89 // BrokerSession holds broker session state.
90 type BrokerSession interface {
91 // SessionChannel returns the session control channel
92 // on which the session gets exchanges to perform.
93 SessionChannel() <-chan Exchange
94 // DeviceIdentifier returns the device id string.
95 DeviceIdentifier() string
96 // DeviceImageModel returns the device model.
97 DeviceImageModel() string
98 // DeviceImageChannel returns the device system image channel.
99 DeviceImageChannel() string
100 // Levels returns the current channel levels for the session
101 Levels() LevelsMap
102 // ExchangeScratchArea returns the scratch area for exchanges.
103 ExchangeScratchArea() *ExchangesScratchArea
104 // Get gets the content of the channel with chanId.
105 Get(chanId store.InternalChannelId, cachedOk bool) (int64, []protocol.Notification, error)
106 // DropByMsgId drops notifications from the channel chanId by message id.
107 DropByMsgId(chanId store.InternalChannelId, targets []protocol.Notification) error
108 // Feed feeds exchange into the session.
109 Feed(Exchange)
110 // InternalChannelId() returns the channel id corresponding to the session.
111 InternalChannelId() store.InternalChannelId
112 }
113
114 // Session aborted error.
115 type ErrAbort struct {
116 Reason string
117 }
118
119 func (ea *ErrAbort) Error() string {
120 return fmt.Sprintf("session aborted (%s)", ea.Reason)
121 }
122
123 // Unexpect value in message
124 var ErrUnexpectedValue = &ErrAbort{"unexpected value in message"}
125
126 // BrokerConfig gives access to the typical broker configuration.
127 type BrokerConfig interface {
128 // SessionQueueSize gives the session queue size.
129 SessionQueueSize() uint
130 // BrokerQueueSize gives the internal broker queue size.
131 BrokerQueueSize() uint
132 }