-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutilities.go
127 lines (105 loc) · 2.9 KB
/
utilities.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"encoding/json"
"errors"
"log"
"github.com/alexjlockwood/gcm"
)
func sendMessageToGCM(tokens []string, payloadAsString string) (bool, error) {
// At any exit, decrement pending
defer func() {
go decrementPending()
}()
if len(tokens) == 0 {
errText := "No tokens were supplied, exiting"
log.Println(errText)
return false, errors.New(errText)
}
if payloadAsString == "" {
errText := "Payload was empty, exiting"
log.Println(errText)
return false, errors.New(errText)
}
// Unpack the JSON payload
var payload map[string]interface{}
err := json.Unmarshal([]byte(payloadAsString), &payload)
if err != nil {
log.Println("Can't unmarshal the json: " + err.Error())
log.Println("Original: " + payloadAsString)
return false, err
}
// All is well, make & send the message
go appendAttempts(len(tokens))
msg := gcm.NewMessage(payload, tokens...)
sender := &gcm.Sender{ApiKey: settings.GCMAPIKey}
response, err := sender.Send(msg, 2)
if err != nil {
log.Println("Failed to send message:")
log.Println(err.Error())
go appendFailures(1)
return false, err
}
numCan := 0
numErr := 0
if response != nil {
for i, result := range response.Results {
// Canonicals
if result.RegistrationID != "" {
numCan++
canonicalReplacements = append(canonicalReplacements, canonicalReplacement{tokens[i], result.RegistrationID})
}
if result.Error != "" {
numErr++
log.Printf("Error sending: %s", result.Error)
if result.Error == "NotRegistered" {
handleNotRegisteredError(tokens[i])
go appendNotRegistered(1)
}
}
}
go appendCanonicals(numCan)
go appendFailures(numErr)
}
log.Printf("Message sent. Attempts: %d, Errors: %d, Successful: %d (Canonicals: %d)", len(tokens), numErr, len(tokens)-numErr, numCan)
return true, nil
}
func handleCanonicalsInResult(original string, results []gcm.Result) {
for _, r := range results {
canonicalReplacements = append(canonicalReplacements, canonicalReplacement{original, r.RegistrationID})
}
}
func handleNotRegisteredError(original string) {
notRegisteredMutex.Lock()
notRegisteredKeys = append(notRegisteredKeys, original)
notRegisteredMutex.Unlock()
}
func appendAttempts(numToAppend int) {
runReportMutex.Lock()
defer runReportMutex.Unlock()
runReport.Attempts += numToAppend
}
func appendFailures(numToAppend int) {
runReportMutex.Lock()
defer runReportMutex.Unlock()
runReport.Failures += numToAppend
}
func appendCanonicals(numToAppend int) {
runReportMutex.Lock()
defer runReportMutex.Unlock()
runReport.Canonicals += numToAppend
}
func appendNotRegistered(numToAppend int) {
runReportMutex.Lock()
defer runReportMutex.Unlock()
runReport.NotRegistered += numToAppend
}
func incrementPending() {
runReportMutex.Lock()
defer runReportMutex.Unlock()
runReport.Pending++
}
func decrementPending() {
runReportMutex.Lock()
defer runReportMutex.Unlock()
runReport.Pending--
}