~pedronis/ubuntu-push/automatic-land-to-vivid

« back to all changes in this revision

Viewing changes to client/service/mbox.go

  • Committer: CI bot
  • Author(s): Richard Huddie, CI bot, Roberto Alsina, Guillermo Gonzalez, John R. Lenton, Samuele Pedroni (Canonical Services Ltd.)
  • Date: 2014-07-21 14:17:24 UTC
  • mfrom: (91.1.177 trunk)
  • Revision ID: ps-jenkins@lists.canonical.com-20140721141724-zf0n470ryo5k4dmd
  [Samuele Pedroni]
  * Check in the api whether an app has pushed too many notifications.
  * Return payload of most recent notification in too many pending
    notifications API error.
  * Introduce clear_pending flag to clean everything pending for an app.
  * Refactor and cleanup.
  * Introduce replace_tag support in store and api, with acceptance test.
  * Teach a couple of trick to cmd/acceptanceclient: exit on run timeout,
    wait for event matching given regexp pattern.
  * Limit unicast data payload to 2K.
  * Payload should be json (fixes message needing to be base64-encoded in
    helper reply)
  * Implement limited mboxes
  * Refactor and cleanup of things done in haste by Chipaca.

  [Richard Huddie]
  * autopilot test framework and basic coverage of broadcast notifications.

  [Guillermo Gonzalez]
  * Add scripts to simplify setup/run of the autopilot tests in the
    device/emulator and include basic unicast tests.
  * Add autopilot test for notification using the emblem counter.
  * Adds scenarios to the autopilot tests for legacy and click (without
    version) applications.
  * Broadcast via the helpers route.
  * Basic support for actions (only default action) in the persistent
    notifications.
  * Change PostBroadcast to send the broadcast message to the software
    updates helper.

  [John R. Lenton]
  * Detangle client and postal.
  * Introduce PostalService interface, and change the client tests to use
    that as much as reasonable.
  * Async invocation of helpers.
  * Give click.Click knowledge of helpers.
  * Write ual-based helper launcher.
  * Switch to the ual-based helper launcher unless the environment
    variable UBUNTU_PUSH_USE_TRIVIAL_HELPER is set.
  * Threw together an implementation of helpers for legacy applications.
  * Hacked up an initial software updates helper, to be handed off to the
    appropriate team shortly.

  [Roberto Alsina]
  * Wrap the (dbus) WindowStack API and add endpoint to the Postal service
    to support inhibition of notifications for focused apps.
  * Inhibit notifications for focused apps
 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 Copyright 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 service
 
18
 
 
19
import (
 
20
        "encoding/json"
 
21
)
 
22
 
 
23
var mBoxMaxMessagesSize = 128 * 1024
 
24
 
 
25
// mBox can hold a size-limited amount of notification messages for one application.
 
26
type mBox struct {
 
27
        evicted  int
 
28
        curSize  int
 
29
        messages []string
 
30
        nids     []string
 
31
}
 
32
 
 
33
func (box *mBox) evictFor(sz int) {
 
34
        evictedSize := 0
 
35
        i := box.evicted
 
36
        n := len(box.messages)
 
37
        for evictedSize < sz && i < n {
 
38
                evictedSize += len(box.messages[i])
 
39
                box.messages[i] = ""
 
40
                box.nids[i] = ""
 
41
                box.evicted++
 
42
                i++
 
43
        }
 
44
        box.curSize -= evictedSize
 
45
}
 
46
 
 
47
// Append appends a message with notification id to the mbox.
 
48
func (box *mBox) Append(message json.RawMessage, nid string) {
 
49
        sz := len(message)
 
50
        if box.curSize+sz > mBoxMaxMessagesSize {
 
51
                // make space
 
52
                box.evictFor(sz)
 
53
        }
 
54
        n := len(box.messages)
 
55
        evicted := box.evicted
 
56
        if evicted > 0 {
 
57
                if evicted == n {
 
58
                        // all evicted, just start from scratch
 
59
                        box.messages = box.messages[0:0]
 
60
                        box.nids = box.nids[0:0]
 
61
                        box.evicted = 0
 
62
                } else if evicted >= cap(box.messages)/2 {
 
63
                        // amortize: do a copy only each cap/2 evicted
 
64
                        copy(box.messages, box.messages[box.evicted:])
 
65
                        kept := n - box.evicted
 
66
                        box.messages = box.messages[0:kept]
 
67
                        copy(box.nids, box.nids[box.evicted:])
 
68
                        box.nids = box.nids[0:kept]
 
69
                        box.evicted = 0
 
70
                }
 
71
        }
 
72
        box.messages = append(box.messages, string(message))
 
73
        box.nids = append(box.nids, nid)
 
74
        box.curSize += sz
 
75
}
 
76
 
 
77
// AllMessages gets all messages from the mbox.
 
78
func (box *mBox) AllMessages() []string {
 
79
        return box.messages[box.evicted:]
 
80
}