/
listener.go
107 lines (88 loc) · 3.69 KB
/
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
package device
import (
"github.com/xmidt-org/wrp-go/v3"
)
// EventType is the type of device-related event
type EventType uint8
const (
// Connect indicates a successful device connection. After receipt of this event, the given
// Device is able to receive requests.
Connect EventType = iota
// Disconnect indicates a device disconnection. After receipt of this event, the given
// Device can no longer receive requests.
Disconnect
// MessageSent indicates that a message was successfully dispatched to a device.
MessageSent
// MessageReceived indicates that a message has been successfully received and
// dispatched to any goroutine waiting on it, as would be the case for a response.
MessageReceived
// MessageFailed indicates that a message could not be sent to a device, either because
// of a communications error or due to the device disconnecting. For each enqueued message
// at the time of a device's disconnection, there will be (1) MessageFailed event.
MessageFailed
// TransactionComplete indicates that a response to a transaction has been received, and the
// transaction completed successfully (at least as far as the routing infrastructure can tell).
TransactionComplete
// TransactionBroken indicates receipt of a message that had a transaction key for which there
// was no waiting transaction
TransactionBroken
InvalidEventString string = "!!INVALID DEVICE EVENT TYPE!!"
)
var (
// blankEvent is an Event in its initial state. Useful for quick state reset.
blankEvent Event
)
func (et EventType) String() string {
switch et {
case Connect:
return "Connect"
case Disconnect:
return "Disconnect"
case MessageSent:
return "MessageSent"
case MessageReceived:
return "MessageReceived"
case MessageFailed:
return "MessageFailed"
case TransactionComplete:
return "TransactionComplete"
case TransactionBroken:
return "TransactionBroken"
default:
return InvalidEventString
}
}
// Event represents a single occurrence of interest for device-related applications.
// Instances of Event should be considered immutable by application code. Also, Event
// instances should not be stored across calls to a listener, as the infrastructure is
// free to reuse Event instances.
type Event struct {
// Type describes the kind of this event. This field is always set.
Type EventType
// Device refers to the device, possibly disconnected, for which this event is being set.
// This field is always set.
Device Interface
// Message is the WRP message relevant to this event.
//
// Never assume that it is safe to use this Message outside the listener invocation. Make
// a copy if this Message is needed by other goroutines or if it needs to be part of a long-lived
// data structure.
Message wrp.Typed
// Format is the encoding format of the Contents field
Format wrp.Format
// Contents is the encoded representation of the Message field. It is always set if and only if
// the Message field is set.
//
// Never assume that it is safe to use this byte slice outside the listener invocation. Make
// a copy if this byte slice is needed by other goroutines or if it needs to be part of a long-lived
// data structure.
Contents []byte
// Error is the error which occurred during an attempt to send a message. This field is only populated
// for MessageFailed events when there was an actual error. For MessageFailed events that indicate a
// device was disconnected with enqueued messages, this field will be nil.
Error error
}
// Listener is an event sink. Listeners should never modify events and should never
// store events for later use. If data from an event is needed for another goroutine
// or for long-term storage, a copy should be made.
type Listener func(*Event)