-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
service.go
145 lines (127 loc) · 3.81 KB
/
service.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package service
import (
"sync"
"time"
coapgwService "github.com/plgd-dev/hub/v2/coap-gateway/service"
coapgwTestService "github.com/plgd-dev/hub/v2/test/coap-gateway/service"
coapgwTest "github.com/plgd-dev/hub/v2/test/coap-gateway/test"
)
const (
SignUpKey = "SignUp" // register
SignOffKey = "SignOff" // deregister
SignInKey = "SignIn" // log in
SignOutKey = "SignOut" // log out
PublishKey = "Publish"
UnpublishKey = "Unpublish"
RefreshTokenKey = "RefreshToken"
)
type CoapHandlerWithCounter struct {
*coapgwTest.DefaultObserverHandler
CallCounter struct {
Data map[string]int
Lock sync.Mutex
}
signedInChan chan struct{}
signedOffChan chan struct{}
refreshChan chan struct{}
}
func NewCoapHandlerWithCounter(atLifetime int64) *CoapHandlerWithCounter {
dh := coapgwTest.MakeDefaultObserverHandler(atLifetime)
return &CoapHandlerWithCounter{
DefaultObserverHandler: &dh,
CallCounter: struct {
Data map[string]int
Lock sync.Mutex
}{
Data: make(map[string]int),
},
signedInChan: make(chan struct{}),
signedOffChan: make(chan struct{}),
refreshChan: make(chan struct{}),
}
}
func (ch *CoapHandlerWithCounter) SignUp(req coapgwService.CoapSignUpRequest) (coapgwService.CoapSignUpResponse, error) {
resp, err := ch.DefaultObserverHandler.SignUp(req)
ch.CallCounter.Lock.Lock()
ch.CallCounter.Data[SignUpKey]++
ch.CallCounter.Lock.Unlock()
return resp, err
}
func (ch *CoapHandlerWithCounter) SignOff() error {
err := ch.DefaultObserverHandler.SignOff()
ch.CallCounter.Lock.Lock()
ch.CallCounter.Data[SignOffKey]++
signOffCount, ok := ch.CallCounter.Data[SignOffKey]
ch.CallCounter.Lock.Unlock()
if ok && signOffCount == 1 {
close(ch.signedOffChan)
}
return err
}
func (ch *CoapHandlerWithCounter) WaitForFirstSignOff(timeout time.Duration) bool {
select {
case <-ch.signedOffChan:
return true
case <-time.After(timeout):
return false
}
}
func (ch *CoapHandlerWithCounter) SignIn(req coapgwService.CoapSignInReq) (coapgwService.CoapSignInResp, error) {
resp, err := ch.DefaultObserverHandler.SignIn(req)
ch.CallCounter.Lock.Lock()
ch.CallCounter.Data[SignInKey]++
signInCount, ok := ch.CallCounter.Data[SignInKey]
ch.CallCounter.Lock.Unlock()
if ok && signInCount == 1 {
close(ch.signedInChan)
}
return resp, err
}
func (ch *CoapHandlerWithCounter) WaitForFirstSignIn(timeout time.Duration) bool {
select {
case <-ch.signedInChan:
return true
case <-time.After(timeout):
return false
}
}
func (ch *CoapHandlerWithCounter) SignOut(req coapgwService.CoapSignInReq) error {
err := ch.DefaultObserverHandler.SignOut(req)
ch.CallCounter.Lock.Lock()
ch.CallCounter.Data[SignOutKey]++
ch.CallCounter.Lock.Unlock()
return err
}
func (ch *CoapHandlerWithCounter) PublishResources(req coapgwTestService.PublishRequest) error {
err := ch.DefaultObserverHandler.PublishResources(req)
ch.CallCounter.Lock.Lock()
ch.CallCounter.Data[PublishKey]++
ch.CallCounter.Lock.Unlock()
return err
}
func (ch *CoapHandlerWithCounter) UnpublishResources(req coapgwTestService.UnpublishRequest) error {
err := ch.DefaultObserverHandler.UnpublishResources(req)
ch.CallCounter.Lock.Lock()
ch.CallCounter.Data[UnpublishKey]++
ch.CallCounter.Lock.Unlock()
return err
}
func (ch *CoapHandlerWithCounter) RefreshToken(req coapgwService.CoapRefreshTokenReq) (coapgwService.CoapRefreshTokenResp, error) {
resp, err := ch.DefaultObserverHandler.RefreshToken(req)
ch.CallCounter.Lock.Lock()
ch.CallCounter.Data[RefreshTokenKey]++
refreshCount, ok := ch.CallCounter.Data[RefreshTokenKey]
ch.CallCounter.Lock.Unlock()
if ok && refreshCount == 1 {
close(ch.refreshChan)
}
return resp, err
}
func (ch *CoapHandlerWithCounter) WaitForFirstRefreshToken(timeout time.Duration) bool {
select {
case <-ch.refreshChan:
return true
case <-time.After(timeout):
return false
}
}