-
Notifications
You must be signed in to change notification settings - Fork 2
/
agent.go
71 lines (61 loc) · 1.66 KB
/
agent.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
package systeminfo
import (
"github.com/allada/gdd/protocol/shared"
"sync"
"encoding/json"
"fmt"
)
type GetInfoCommandFn struct {
mux sync.RWMutex
fn func(GetInfoCommand)
}
func (a *GetInfoCommandFn) Load() func(GetInfoCommand) {
a.mux.RLock()
defer a.mux.RUnlock()
return a.fn
}
func (a *GetInfoCommandFn) Store(fn func(GetInfoCommand)) {
a.mux.Lock()
defer a.mux.Unlock()
a.fn = fn
}
type SystemInfoAgent struct {
conn *shared.Connection
commandHandlers struct {
GetInfo GetInfoCommandFn
}
}
func NewAgent(conn *shared.Connection) *SystemInfoAgent {
agent := &SystemInfoAgent{
conn: conn,
}
conn.RegisterAgent(agent)
return agent
}
func (agent *SystemInfoAgent) Name() string {
return "SystemInfo"
}
func (agent *SystemInfoAgent) ProcessCommand(id int64, targetId string, funcName string, data *json.RawMessage) {
defer shared.TryRecoverFromPanic(agent.conn)
switch (funcName) {
case "getInfo":
var out GetInfoCommand
agent.conn.SetupCommand(id, targetId, data, &out)
fn := agent.commandHandlers.GetInfo.Load()
if fn == nil {
out.RespondWithError(shared.ErrorCodeMethodNotFound, "")
break
}
fn(out)
default:
fmt.Printf("Command %s unknown\n", funcName)
agent.conn.SendErrorResult(id, targetId, shared.ErrorCodeMethodNotFound, "")
}
}
// Dispatchable Events
// Commands Sent From Frontend
func (agent *SystemInfoAgent) SetGetInfoHandler(handler func(GetInfoCommand)) {
agent.commandHandlers.GetInfo.Store(handler)
}
func init() {
}