-
Notifications
You must be signed in to change notification settings - Fork 53
/
run.go
113 lines (91 loc) · 2.41 KB
/
run.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
package routeros
import (
"context"
"fmt"
"log/slog"
"github.com/go-routeros/routeros/v3/proto"
)
type asyncReply struct {
chanReply
Reply
}
// Run simply calls RunArgs().
func (c *Client) Run(sentences ...string) (*Reply, error) {
return c.RunArgs(sentences)
}
// RunContext simply calls RunArgsContext().
func (c *Client) RunContext(ctx context.Context, sentences ...string) (*Reply, error) {
return c.RunArgsContext(ctx, sentences)
}
// RunArgs sends a sentence to the RouterOS device and waits for the reply.
func (c *Client) RunArgs(sentences []string) (*Reply, error) {
return c.RunArgsContext(context.Background(), sentences)
}
// RunArgsContext sends a sentence to the RouterOS device and waits for the reply.
func (c *Client) RunArgsContext(ctx context.Context, sentences []string) (*Reply, error) {
c.logger().Debug("RunArgsContext", slog.Any("sentences", sentences))
c.w.BeginSentence()
for _, sentence := range sentences {
c.w.WriteWord(sentence)
}
if !c.IsAsync() {
return c.runArgsContextSync()
}
// async mode, assign new tag to request
tag := c.incrementTag()
a := &asyncReply{}
a.reC = make(chan *proto.Sentence)
a.tag = fmt.Sprintf("r%d", tag)
c.w.WriteWord(".tag=" + a.tag)
c.logger().Debug("set tag", slog.String("tag", a.tag))
if err := c.w.EndSentence(); err != nil {
return nil, err
}
c.mu.Lock()
if c.tags == nil {
c.mu.Unlock()
return nil, errAsyncLoopEnded
}
c.tags[a.tag] = a
c.mu.Unlock()
// wait for asyncLoop to close channel or context done
for {
select {
case <-ctx.Done():
c.r.Cancel()
return nil, ctx.Err()
case _, ok := <-a.reC:
if !ok { // channel closed
return &a.Reply, a.err
}
}
}
}
// runArgsContextSync - read command reply in sync mode and return
func (c *Client) runArgsContextSync() (*Reply, error) {
var err error
if err = c.w.EndSentence(); err != nil {
return nil, err
}
out := new(Reply)
var lastErr error
for {
var sen *proto.Sentence
// read next sentence
if sen, err = c.r.ReadSentence(); err != nil {
return nil, err
}
var done bool
switch done, err = out.processSentence(sen); {
case err != nil && done:
// processed error sentence and it was fatal
return nil, err
case err != nil:
// processed error sentence, but it was not fatal, read next, store last error
lastErr = err
case done:
// processed sentence is Done, return result and last error
return out, lastErr
}
}
}