forked from CyCoreSystems/ari
-
Notifications
You must be signed in to change notification settings - Fork 1
/
application.go
95 lines (79 loc) · 3.05 KB
/
application.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
package ari
// Application represents a communication path interacting with an Asterisk
// server for application-level resources
type Application interface {
// List returns the list of applications in Asterisk, optionally using the key for filtering
List(*Key) ([]*Key, error)
// Get returns a handle to the application for further interaction
Get(key *Key) *ApplicationHandle
// Data returns the applications data
Data(key *Key) (*ApplicationData, error)
// Subscribe subscribes the given application to an event source
// event source may be one of:
// - channel:<channelId>
// - bridge:<bridgeId>
// - endpoint:<tech>/<resource> (e.g. SIP/102)
// - deviceState:<deviceName>
Subscribe(key *Key, eventSource string) error
// Unsubscribe unsubscribes (removes a subscription to) a given
// ARI application from the provided event source
// Equivalent to DELETE /applications/{applicationName}/subscription
Unsubscribe(key *Key, eventSource string) error
}
// ApplicationData describes the data for a Stasis (Ari) application
type ApplicationData struct {
// Key is the unique identifier for this application instance in the cluster
Key *Key `json:"key"`
BridgeIDs []string `json:"bridge_ids"` // Subscribed BridgeIds
ChannelIDs []string `json:"channel_ids"` // Subscribed ChannelIds
DeviceNames []string `json:"device_names"` // Subscribed Device names
EndpointIDs []string `json:"endpoint_ids"` // Subscribed Endpoints (tech/resource format)
Name string `json:"name"` // Name of the application
}
// ApplicationHandle provides a wrapper to an Application interface for
// operations on a specific application
type ApplicationHandle struct {
key *Key
a Application
}
// NewApplicationHandle creates a new handle to the application name
func NewApplicationHandle(key *Key, app Application) *ApplicationHandle {
return &ApplicationHandle{
key: key,
a: app,
}
}
// ID returns the identifier for the application
func (ah *ApplicationHandle) ID() string {
return ah.key.ID
}
// Key returns the key of the application
func (ah *ApplicationHandle) Key() *Key {
return ah.key
}
// Data retrives the data for the application
func (ah *ApplicationHandle) Data() (ad *ApplicationData, err error) {
ad, err = ah.a.Data(ah.key)
return
}
// Subscribe subscribes the application to an event source
// event source may be one of:
// - channel:<channelId>
// - bridge:<bridgeId>
// - endpoint:<tech>/<resource> (e.g. SIP/102)
// - deviceState:<deviceName>
func (ah *ApplicationHandle) Subscribe(eventSource string) (err error) {
err = ah.a.Subscribe(ah.key, eventSource)
return
}
// Unsubscribe unsubscribes (removes a subscription to) a given
// ARI application from the provided event source
// Equivalent to DELETE /applications/{applicationName}/subscription
func (ah *ApplicationHandle) Unsubscribe(eventSource string) (err error) {
err = ah.a.Unsubscribe(ah.key, eventSource)
return
}
// Match returns true fo the event matches the application
func (ah *ApplicationHandle) Match(e Event) bool {
return e.GetApplication() == ah.key.ID
}