-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
197 lines (175 loc) · 4.35 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
package vdsm
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"github.com/go-stomp/stomp"
"github.com/satori/go.uuid"
"io"
"io/ioutil"
"log"
"net"
"os"
"path/filepath"
"time"
)
type Congiuration struct {
TlsEnabled bool
CaCert string
VdsmCert string
VdsmKey string
Hostname string
Port string
IncomingHeartbeat int
OutgoingHeartbeat int
TLSConfig *tls.Config
}
type Client struct {
connection *stomp.Conn
configuration *Congiuration
subscriptions []*stomp.Subscription
}
type jsonRequest struct {
Method string `json:"method"`
Params interface{} `json:"params"`
Id string `json:"id"`
Version string `json:"jsonrpc"`
}
type jsonResponse struct {
Id string `json:"id"`
Result map[string]interface{} `json:"result"`
Error *VdsmError `json:"error"`
}
type VdsmError struct {
Code int
Message string
}
func (e *VdsmError) Error() string {
return fmt.Sprintf("Code %d, message %s", e.Code, e.Message)
}
func GetConfig(filename string, configuration interface{}) error {
if len(filename) == 0 {
return nil
}
path, _ := filepath.Abs(filename)
file, err := os.Open(path)
if err != nil {
return err
}
decoder := json.NewDecoder(file)
err = decoder.Decode(&configuration)
if err != nil {
return err
}
return nil
}
func GetId() string {
return fmt.Sprintf("%x", uuid.NewV4())
}
func loadCerts(config *Congiuration) {
ca := x509.NewCertPool()
ca_bytes, _ := ioutil.ReadFile(config.CaCert)
ok := ca.AppendCertsFromPEM(ca_bytes)
if !ok {
log.Fatal("Failed to load CA certificate")
}
certificate, err := tls.LoadX509KeyPair(config.VdsmCert, config.VdsmKey)
if err != nil {
log.Fatal(err)
}
config.TLSConfig = &tls.Config{
RootCAs: ca,
Certificates: []tls.Certificate{certificate},
InsecureSkipVerify: false, // TODO: check what is needed to enable it
}
}
func (client *Client) getSubscription(destination string) *stomp.Subscription {
for _, sub := range client.subscriptions {
if sub.Destination() == destination {
return sub
}
}
return nil
}
func (client *Client) Connect(config *Congiuration) error {
var connection io.ReadWriteCloser
var err error
if config.TlsEnabled {
loadCerts(config)
connection, err = tls.Dial("tcp", config.Hostname+":"+config.Port, config.TLSConfig)
} else {
connection, err = net.Dial("tcp", config.Hostname+":"+config.Port)
}
if err != nil {
log.Fatal("Failed to connect to ", config.Hostname, ":", config.Port)
}
conn, err := stomp.Connect(connection,
stomp.ConnOpt.AcceptVersion(stomp.V12),
stomp.ConnOpt.HeartBeat(time.Duration(
config.OutgoingHeartbeat)*time.Second,
time.Duration(config.IncomingHeartbeat)*time.Second),
)
if err != nil {
return err
}
client.connection = conn
client.configuration = config
return nil
}
func (client *Client) Disconnect() {
if client.connection != nil {
// none of our clients is graceful so we can ignore any network issues
client.connection.MustDisconnect()
client.connection = nil
}
}
func (client *Client) Subscribe(destination string) error {
subscription, err := client.connection.Subscribe(
destination, stomp.AckAuto,
stomp.SubscribeOpt.Header("id", GetId()))
if err != nil {
return err
}
client.subscriptions = append(client.subscriptions, subscription)
return nil
}
func (client *Client) Unsubscribe(destination string) {
subs := client.subscriptions[:0]
for _, sub := range client.subscriptions {
if sub.Destination() == destination {
sub.Unsubscribe()
} else {
subs = append(subs, sub)
}
}
client.subscriptions = subs
}
func (client *Client) Send(destination string, method string, params interface{}) (map[string]interface{}, error) {
req := new(jsonRequest)
req.Method = method
req.Id = GetId()
req.Params = params
req.Version = "2.0"
content, err := json.Marshal(req)
if err != nil {
return nil, err
}
err = client.connection.Send("jms.topic.vdsm_requests", "",
content,
stomp.SendOpt.Header("reply-to", destination))
if err != nil {
return nil, err
}
subscription := client.getSubscription(destination)
resp, err := subscription.Read()
var response jsonResponse
err = json.Unmarshal(resp.Body, &response)
if err != nil {
return nil, err
}
if response.Error != nil {
return nil, response.Error
}
return response.Result, nil
}