Debian Codegraph @

protocol/messages.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 protocol
18
19 // Structs representing messages.
20
21 import (
22 "encoding/json"
23 "fmt"
24 )
25
26 // System channel id using a shortened hex-encoded form for the NIL UUID.
27 const SystemChannelId = "0"
28
29 // CONNECT message
30 type ConnectMsg struct {
31 Type string `json:"T"`
32 ClientVer string
33 DeviceId string
34 Authorization string
35 Cookie string
36 Info map[string]interface{} `json:",omitempty"` // platform etc...
37 // maps channel ids (hex encoded UUIDs) to known client channel levels
38 Levels map[string]int64
39 }
40
41 // CONNACK message
42 type ConnAckMsg struct {
43 Type string `json:"T"`
44 Params ConnAckParams
45 }
46
47 // ConnAckParams carries the connection parameters from the server on
48 // connection acknowledgement.
49 type ConnAckParams struct {
50 // ping interval formatted time.Duration
51 PingInterval string
52 }
53
54 // SplittableMsg are messages that may require and are capable of splitting.
55 type SplittableMsg interface {
56 Split() (done bool)
57 }
58
59 // OnewayMsg are messages that are not to be followed by a response,
60 // after sending them the session either aborts or continues.
61 type OnewayMsg interface {
62 SplittableMsg
63 // continue session after the message?
64 OnewayContinue() bool
65 }
66
67 // CONNBROKEN message, server side is breaking the connection for reason.
68 type ConnBrokenMsg struct {
69 Type string `json:"T"`
70 // reason
71 Reason string
72 }
73
74 func (m *ConnBrokenMsg) Split() bool {
75 return true
76 }
77
78 func (m *ConnBrokenMsg) OnewayContinue() bool {
79 return false
80 }
81
82 // CONNBROKEN reasons
83 const (
84 BrokenHostMismatch = "host-mismatch"
85 )
86
87 // CONNWARN message, server side is warning about partial functionality
88 // because reason.
89 type ConnWarnMsg struct {
90 Type string `json:"T"`
91 // reason
92 Reason string
93 }
94
95 func (m *ConnWarnMsg) Split() bool {
96 return true
97 }
98 func (m *ConnWarnMsg) OnewayContinue() bool {
99 return true
100 }
101
102 // CONNWARN reasons
103 const (
104 WarnUnauthorized = "unauthorized"
105 )
106
107 // SETPARAMS message
108 type SetParamsMsg struct {
109 Type string `json:"T"`
110 SetCookie string
111 }
112
113 func (m *SetParamsMsg) Split() bool {
114 return true
115 }
116 func (m *SetParamsMsg) OnewayContinue() bool {
117 return true
118 }
119
120 // PING/PONG messages
121 type PingPongMsg struct {
122 Type string `json:"T"`
123 }
124
125 const maxPayloadSize = 62 * 1024
126
127 // BROADCAST messages
128 type BroadcastMsg struct {
129 Type string `json:"T"`
130 AppId string `json:",omitempty"`
131 ChanId string
132 TopLevel int64
133 Payloads []json.RawMessage
134 splitting int
135 }
136
137 func (m *BroadcastMsg) Split() bool {
138 var prevTop int64
139 if m.splitting == 0 {
140 prevTop = m.TopLevel - int64(len(m.Payloads))
141 } else {
142 prevTop = m.TopLevel
143 m.Payloads = m.Payloads[len(m.Payloads):m.splitting]
144 m.TopLevel = prevTop + int64(len(m.Payloads))
145 }
146 payloads := m.Payloads
147 var size int
148 for i := range payloads {
149 size += len(payloads[i])
150 if size > maxPayloadSize {
151 m.TopLevel = prevTop + int64(i)
152 m.splitting = len(payloads)
153 m.Payloads = payloads[:i]
154 return false
155 }
156 }
157 return true
158 }
159
160 // Reset resets the splitting state if the message storage is to be
161 // reused and sets the proper Type.
162 func (b *BroadcastMsg) Reset() {
163 b.Type = "broadcast"
164 b.splitting = 0
165 }
166
167 // NOTIFICATIONS message
168 type NotificationsMsg struct {
169 Type string `json:"T"`
170 Notifications []Notification
171 splitting int
172 }
173
174 // Reset resets the splitting state if the message storage is to be
175 // reused and sets the proper Type.
176 func (m *NotificationsMsg) Reset() {
177 m.Type = "notifications"
178 m.splitting = 0
179 }
180
181 func (m *NotificationsMsg) Split() bool {
182 if m.splitting != 0 {
183 m.Notifications = m.Notifications[len(m.Notifications):m.splitting]
184 }
185 notifs := m.Notifications
186 var size int
187 for i, notif := range notifs {
188 size += len(notif.Payload) + len(notif.AppId) + len(notif.MsgId) + notificationOverhead
189 if size > maxPayloadSize {
190 m.splitting = len(notifs)
191 m.Notifications = notifs[:i]
192 return false
193 }
194 }
195 return true
196 }
197
198 var notificationOverhead int
199
200 func init() {
201 buf, err := json.Marshal(Notification{})
202 if err != nil {
203 panic(fmt.Errorf("failed to compute Notification marshal overhead: %v", err))
204 }
205 notificationOverhead = len(buf) - 4 // - 4 for the null from P(ayload)
206 }
207
208 // A single unicast notification
209 type Notification struct {
210 AppId string `json:"A"`
211 MsgId string `json:"M"`
212 // payload
213 Payload json.RawMessage `json:"P"`
214 }
215
216 // ExtractPayloads gets only the payloads out of a slice of notications.
217 func ExtractPayloads(notifications []Notification) []json.RawMessage {
218 n := len(notifications)
219 if n == 0 {
220 return nil
221 }
222 payloads := make([]json.RawMessage, n)
223 for i := 0; i < n; i++ {
224 payloads[i] = notifications[i].Payload
225 }
226 return payloads
227 }
228
229 // ACKnowledgement message
230 type AckMsg struct {
231 Type string `json:"T"`
232 }
233
234 // xxx ... query levels messages