-
Notifications
You must be signed in to change notification settings - Fork 2
/
event_listener.go
128 lines (108 loc) · 3.3 KB
/
event_listener.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* This file is part of AMS SDK
* Copyright 2021 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the Lesser GNU General Public License version 3, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranties of MERCHANTABILITY, SATISFACTORY
* QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser GNU General Public
* License for more details.
*
* You should have received a copy of the Lesser GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package client
import (
"fmt"
"sync"
)
// The EventListener struct is used to interact with an event stream
type EventListener struct {
c *client
chActive chan bool
disconnected bool
err error
targets []*EventTarget
targetsLock sync.Mutex
}
// The EventTarget struct is returned to the caller of AddHandler and used in RemoveHandler
type EventTarget struct {
function func(interface{})
types []string
}
// AddHandler adds a function to be called whenever an event is received
func (e *EventListener) AddHandler(types []string, function func(interface{})) (*EventTarget, error) {
if function == nil {
return nil, fmt.Errorf("A valid function must be provided")
}
// Handle locking
e.targetsLock.Lock()
defer e.targetsLock.Unlock()
// Create a new target
target := EventTarget{
function: function,
types: types,
}
// And add it to the targets
e.targets = append(e.targets, &target)
return &target, nil
}
// RemoveHandler removes a function to be called whenever an event is received
func (e *EventListener) RemoveHandler(target *EventTarget) error {
if target == nil {
return fmt.Errorf("A valid event target must be provided")
}
// Handle locking
e.targetsLock.Lock()
defer e.targetsLock.Unlock()
// Locate and remove the function from the list
for i, entry := range e.targets {
if entry == target {
copy(e.targets[i:], e.targets[i+1:])
e.targets[len(e.targets)-1] = nil
e.targets = e.targets[:len(e.targets)-1]
return nil
}
}
return fmt.Errorf("Couldn't find this function and event types combination")
}
// Disconnect must be used once done listening for events
func (e *EventListener) Disconnect() {
if e.disconnected {
return
}
// Handle locking
e.c.eventListenersLock.Lock()
defer e.c.eventListenersLock.Unlock()
// Locate and remove it from the global list
for i, listener := range e.c.eventListeners {
if listener == e {
copy(e.c.eventListeners[i:], e.c.eventListeners[i+1:])
e.c.eventListeners[len(e.c.eventListeners)-1] = nil
e.c.eventListeners = e.c.eventListeners[:len(e.c.eventListeners)-1]
break
}
}
// Turn off the handler
e.err = nil
e.disconnected = true
close(e.chActive)
}
// Wait hangs until the server disconnects the connection or Disconnect() is called
func (e *EventListener) Wait() error {
<-e.chActive
return e.err
}
// IsActive returns true if this listener is still connected, false otherwise.
func (e *EventListener) IsActive() bool {
select {
case <-e.chActive:
return false // If the chActive channel is closed we got disconnected
default:
return true
}
}