-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommandFuncs.go
62 lines (56 loc) · 1.23 KB
/
commandFuncs.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
package shoset
import (
"fmt"
"time"
"github.com/ditrit/shoset/msg"
)
// GetCommand :
func GetCommand(c *ShosetConn) (msg.Message, error) {
var cmd msg.Command
err := c.ReadMessage(&cmd)
return cmd, err
}
// HandleCommand :
func HandleCommand(c *ShosetConn, message msg.Message) error {
cmd := message.(msg.Command)
c.GetCh().Queue["cmd"].Push(cmd, c.GetRemoteShosetType(), c.GetLocalAddress())
return nil
}
// SendCommand :
func SendCommand(c *Shoset, cmd msg.Message) {
fmt.Print("Sending Command.\n")
c.ConnsByName.IterateAll(
func(key string, conn *ShosetConn) {
conn.SendMessage(cmd)
},
)
}
// WaitCommand :
func WaitCommand(c *Shoset, replies *msg.Iterator, args map[string]string, timeout int) *msg.Message {
commandName, ok := args["name"]
if !ok {
return nil
}
term := make(chan *msg.Message, 1)
cont := true
go func() {
for cont {
message := replies.Get().GetMessage()
if message != nil {
command := message.(msg.Command)
if command.GetCommand() == commandName {
term <- &message
}
} else {
time.Sleep(time.Duration(10) * time.Millisecond)
}
}
}()
select {
case res := <-term:
cont = false
return res
case <-time.After(time.Duration(timeout) * time.Second):
return nil
}
}