This repository has been archived by the owner on Jun 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
psm.go
98 lines (80 loc) · 1.46 KB
/
psm.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
// psmcli
// Copyright (C) 2014 Procera Networks, Inc.
package main
import (
"encoding/json"
"net"
)
const (
CodeAccessDenied = -20001
)
type command struct {
ID int `json:"id"`
Method string `json:"method"`
Params []interface{} `json:"params"`
}
type response struct {
ID int
Result interface{}
Error struct {
Code int
Message string
}
}
type connection struct {
conn net.Conn
enc *json.Encoder
dec *json.Decoder
}
func newConnection(addr string) (*connection, error) {
conn, err := net.Dial("tcp", addr)
if err != nil {
return nil, err
}
enc := json.NewEncoder(conn)
dec := json.NewDecoder(conn)
dec.UseNumber()
return &connection{
conn: conn,
enc: enc,
dec: dec,
}, nil
}
func (c *connection) run(cmd command) (response, error) {
err := c.enc.Encode(cmd)
if err != nil {
return response{}, err
}
var res response
err = c.dec.Decode(&res)
if err != nil {
return response{}, err
}
return res, nil
}
type smdResponse struct {
Result struct {
Services map[string]smdService
}
}
type smdService struct {
Name string
Parameters []smdParameter
}
type smdParameter struct {
Name string
Optional bool
Type string
}
func (c *connection) smd() (smdResponse, error) {
err := c.enc.Encode(command{Method: "system.smd"})
if err != nil {
return smdResponse{}, err
}
var res smdResponse
err = c.dec.Decode(&res)
if err != nil {
return smdResponse{}, err
}
return res, nil
}