forked from mguzelevich/goatt
/
client_nats.go
98 lines (85 loc) · 2.15 KB
/
client_nats.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
package main
import (
"encoding/json"
"fmt"
"os"
"time"
"github.com/apcera/nats"
)
var timeout = 500 * time.Millisecond
type NatsClient struct {
natsConn *nats.Conn
natsEnConn *nats.EncodedConn
}
func (nc *NatsClient) Init(server, service string) {
var err error
nc.natsConn, err = nats.Connect(server)
if err != nil {
panic(err)
}
nc.natsEnConn, err = nats.NewEncodedConn(nc.natsConn, nats.JSON_ENCODER)
if err != nil {
panic(err)
}
//defer ec.Close()
}
func (nc *NatsClient) Request(ctx *Context, step ScenarioStep, dryRun bool) {
var err error
var req JsonMap
body, err := ctx.Evaluate(string(step.Msg))
msg := []byte(body)
if err := json.Unmarshal(msg, &req); err != nil {
fmt.Fprintf(os.Stderr, "handle message [%s]", err)
return
}
fmt.Fprintf(os.Stderr, "[%s]\n", body)
if dryRun {
return
}
response, err := nc.natsConn.Request(step.Subject, msg, timeout)
if err != nil {
fmt.Fprintf(os.Stderr, "response message error [%s]", err)
return
}
var resp interface{}
if err := json.Unmarshal(response.Data, &resp); err != nil {
fmt.Fprintf(os.Stderr, "unmarshal handle response message [%s]", err)
return
}
ctx.Result = resp
if err != nil {
fmt.Printf("rsp ERROR: [%s]\n", err)
} else {
fmt.Printf("rsp: [%s]\n[%s]\n", string(response.Subject), string(response.Data))
}
}
func (nc *NatsClient) Publish(ctx *Context, step ScenarioStep, dryRun bool) {
var err error
var req JsonMap
body, err := ctx.Evaluate(string(step.Msg))
msg := []byte(body)
if err := json.Unmarshal(msg, &req); err != nil {
fmt.Fprintf(os.Stderr, "handle message [%s]", err)
return
}
fmt.Fprintf(os.Stderr, "[%s]\n", body)
if dryRun {
return
}
if err = nc.natsEnConn.Publish(step.Subject, msg); err != nil {
fmt.Printf("rsp ERROR: [%s]\n", err)
}
}
func (nc *NatsClient) Subscribe(ctx *Context, step ScenarioStep, dryRun bool) {
fmt.Fprintf(os.Stderr, "\n")
if step.Msg != "" {
nc.natsEnConn.QueueSubscribe(step.Subject, step.Msg, nc.handler)
} else {
nc.natsEnConn.Subscribe(step.Subject, nc.handler)
}
c := make(chan bool, 0)
<-c
}
func (nc *NatsClient) handler(msg *nats.Msg) {
fmt.Fprintf(os.Stderr, "handle message [%s]", msg)
}