-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.go
93 lines (82 loc) · 1.69 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
package routeros
import (
"strconv"
"strings"
"time"
"github.com/swoga/go-routeros/proto"
)
type asyncReply struct {
chanReply
Reply
}
// Run simply calls RunArgs().
func (c *Client) Run(sentence ...string) (*Reply, error) {
return c.RunArgs(sentence)
}
// RunArgs sends a sentence to the RouterOS device and waits for the reply.
func (c *Client) RunArgs(sentence []string) (*Reply, error) {
for _, word := range sentence {
// check if word is empty or only contains spaces
if len(strings.Trim(word, " ")) == 0 {
return nil, errEmptyWord
}
}
c.w.BeginSentence()
for _, word := range sentence {
c.w.WriteWord(word)
}
if !c.async {
return c.endCommandSync()
}
a, err := c.endCommandAsync()
if err != nil {
return nil, err
}
readAllSentences:
for {
timeout, timer := newTimeoutTimer(c.timeout)
select {
case _, open := <-a.reC:
if timer != nil {
timer.Stop()
}
if !open {
break readAllSentences
}
case <-timeout:
return nil, errAsyncTimeout
}
}
return &a.Reply, a.err
}
func (c *Client) endCommandSync() (*Reply, error) {
err := c.w.EndSentence()
if err != nil {
return nil, err
}
return c.readReply()
}
func (c *Client) endCommandAsync() (*asyncReply, error) {
a := &asyncReply{}
a.reC = make(chan *proto.Sentence)
a.tag = "r" + strconv.FormatUint(c.nextTag(), 10)
c.w.WriteWord(".tag=" + a.tag)
c.mu.Lock()
defer c.mu.Unlock()
err := c.w.EndSentence()
if err != nil {
return nil, err
}
if c.tags == nil {
return nil, errAsyncLoopEnded
}
c.tags[a.tag] = a
return a, nil
}
func newTimeoutTimer(d time.Duration) (timeout <-chan time.Time, timer *time.Timer) {
if d > 0 {
timer = time.NewTimer(d)
timeout = timer.C
}
return
}