This repository has been archived by the owner on Jul 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
209 lines (176 loc) · 4.52 KB
/
client.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package sdk
import (
"net/rpc"
"github.com/hashicorp/go-plugin"
)
// PluginManifest is used to describe the plugin's id, name, author, version, etc.
type PluginManifest struct {
Id, Name, Author, Version string
}
// ExtensionType specifies the type of web extension.
type ExtensionType int
const (
CSS = iota + 1
JavaScript
)
// WebExtension represents an addon to the web page.
type WebExtension struct {
Type ExtensionType
PathMatchRegex string
Source string
}
const (
StringValue = iota
NumberValue
OptionValue // drop down list
BooleanValue
)
// PluginConfig represents a plugin's configuration key-value item constraints
// including title and description.
type PluginConfig struct {
Title string
Description string
Key string // a unique key
Type int
Default string // Not for OptionValues
Placeholder string
Values []string // for option lists, first value is default
IsUserSpecific bool
}
// ConfigKV represents a set key-value configuration, used in communicating to
// the plugin the current configuration.
type ConfigKV struct {
Key string
Value string
UserID string
}
// AvailableDevice represents an available device the plugin may be able to
// register.
type AvailableDevice struct {
UniqueID string
ManufacturerName string
ModelName string
Type int64
}
// Iglu is the interface that we're exposing as a plugin.
type Iglu interface {
OnLoad() error
GetManifest() PluginManifest
OnDeviceToggle(uniqueID string, status bool) error
GetDeviceStatus(uniqueID string) bool
GetWebExtensions() []WebExtension
GetPluginConfiguration() []PluginConfig
OnConfigurationUpdate(conf []ConfigKV)
GetAvailableDevices() []AvailableDevice
}
// IgluRPC is what the server is using to communicate to the plugin over RPC
type IgluRPC struct {
client *rpc.Client
}
type OnLoadReply struct {
Err error
}
func (i *IgluRPC) OnLoad() error {
rep := OnLoadReply{}
err := i.client.Call("Plugin.OnLoad", new(interface{}), &rep)
if err != nil {
panic(err)
}
return rep.Err
}
type GetManifestReply struct {
Manifest PluginManifest
}
func (i *IgluRPC) GetManifest() PluginManifest {
rep := GetManifestReply{}
err := i.client.Call("Plugin.GetManifest", new(interface{}), &rep)
if err != nil {
panic(err)
}
return rep.Manifest
}
type OnDeviceToggleArgs struct {
Id string
Status bool
}
type OnDeviceToggleReply struct {
Err error
}
func (i *IgluRPC) OnDeviceToggle(id string, status bool) error {
args := &OnDeviceToggleArgs{Id: id, Status: status}
rep := OnDeviceToggleReply{}
err := i.client.Call("Plugin.OnDeviceToggle", args, &rep)
if err != nil {
panic(err)
}
return rep.Err
}
type GetWebExtensionsReply struct {
Extensions []WebExtension
}
func (i *IgluRPC) GetWebExtensions() []WebExtension {
rep := &GetWebExtensionsReply{}
err := i.client.Call("Plugin.GetWebExtensions", new(interface{}), &rep)
if err != nil {
panic(err)
}
return rep.Extensions
}
type GetPluginConfigurationReply struct {
Configuration []PluginConfig
}
func (i *IgluRPC) GetPluginConfiguration() []PluginConfig {
rep := &GetPluginConfigurationReply{}
err := i.client.Call("Plugin.GetPluginConfiguration", new(interface{}), &rep)
if err != nil {
panic(err)
}
return rep.Configuration
}
type OnConfigurationUpdateArgs struct {
Configuration []ConfigKV
}
func (i *IgluRPC) OnConfigurationUpdate(config []ConfigKV) {
args := &OnConfigurationUpdateArgs{Configuration: config}
err := i.client.Call("Plugin.GetPluginConfiguration", args, 0)
if err != nil {
panic(err)
}
return
}
type GetAvailableDevicesReply struct {
Devices []AvailableDevice
}
func (i *IgluRPC) GetAvailableDevices() []AvailableDevice {
rep := &GetAvailableDevicesReply{}
err := i.client.Call("Plugin.GetAvailableDevices", new(interface{}), &rep)
if err != nil {
panic(err)
}
return rep.Devices
}
type GetDeviceStatusArgs struct {
UniqueID string
}
type GetDeviceStatusReply struct {
Status bool
}
func (i *IgluRPC) GetDeviceStatus(uniqueID string) bool {
args := &GetDeviceStatusArgs{UniqueID: uniqueID}
rep := &GetDeviceStatusReply{}
err := i.client.Call("Plugin.GetDeviceStatus", args, &rep)
if err != nil {
panic(err)
}
return rep.Status
}
// This is the implementation of plugin.Plugin.
type IgluPlugin struct {
Impl Iglu
}
func (p *IgluPlugin) Server(*plugin.MuxBroker) (interface{}, error) {
return &IgluRPCServer{Impl: p.Impl}, nil
}
func (IgluPlugin) Client(b *plugin.MuxBroker, c *rpc.Client) (interface{}, error) {
return &IgluRPC{client: c}, nil
}