-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsimulator.go
97 lines (84 loc) · 2.18 KB
/
simulator.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
package main
type SimulatorFactory interface {
MakeSimulator() (sim APNSSimulator, err error)
}
type APNSSimulator interface {
Reply(notif *APNSNotificaton) (res *APNSResponse, err error)
}
type APNSDefinedBehaviorSimulator struct {
Statuses []uint8
idx int
}
type StatusSimulatorFactory struct {
Statuses []uint8
}
func NewStatusSimulatorFactory(status ...uint8) SimulatorFactory {
ret := new(StatusSimulatorFactory)
if len(status) > 0 {
ret.Statuses = make([]uint8, len(status))
for i, s := range status {
ret.Statuses[i] = s
}
}
return ret
}
func (self *StatusSimulatorFactory) MakeSimulator() (sim APNSSimulator, err error) {
sim = NewAPNSSimulatorWithStatuses(self.Statuses...)
return
}
type NormalSimulatorFactory struct {
MaxPayloadLen int
DeviceTokenLen int
}
func NewNormalSimulatorFactory(MaxPayloadLen, DeviceTokenLen int) SimulatorFactory {
ret := &NormalSimulatorFactory{MaxPayloadLen, DeviceTokenLen}
return ret
}
func (self *NormalSimulatorFactory) MakeSimulator() (sim APNSSimulator, err error) {
sim = &APNSNormalSimulator{self.MaxPayloadLen, self.DeviceTokenLen}
return
}
func NewAPNSSimulatorWithStatuses(status ...uint8) *APNSDefinedBehaviorSimulator {
ret := new(APNSDefinedBehaviorSimulator)
if len(status) > 0 {
ret.Statuses = make([]uint8, len(status))
for i, s := range status {
ret.Statuses[i] = s
}
}
return ret
}
func (self *APNSDefinedBehaviorSimulator) Reply(notif *APNSNotificaton) (res *APNSResponse, err error) {
res = new(APNSResponse)
res.id = notif.id
if len(self.Statuses) == 0 {
return
}
if self.idx >= len(self.Statuses) || self.idx < 0 {
self.idx = 0
}
res.status = self.Statuses[self.idx]
self.idx++
return
}
type APNSNormalSimulator struct {
MaxPayloadLen int
DeviceTokenLen int
}
func (self *APNSNormalSimulator) Reply(notif *APNSNotificaton) (res *APNSResponse, err error) {
res = new(APNSResponse)
res.id = notif.id
if self.MaxPayloadLen <= 0 {
self.MaxPayloadLen = 256
}
if self.DeviceTokenLen <= 0 {
self.DeviceTokenLen = 32
}
if len(notif.payload) > self.MaxPayloadLen {
res.status = 7
} else if len(notif.devToken) != self.DeviceTokenLen {
res.status = 5
}
// TODO detect more errors
return
}