-
Notifications
You must be signed in to change notification settings - Fork 51
/
run.go
64 lines (55 loc) · 1.12 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
package routeros
import (
"fmt"
"gopkg.in/routeros.v2/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) {
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
}
for range a.reC {
}
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) {
c.nextTag++
a := &asyncReply{}
a.reC = make(chan *proto.Sentence)
a.tag = fmt.Sprintf("r%d", c.nextTag)
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
}