-
Notifications
You must be signed in to change notification settings - Fork 1
/
configFuncs.go
63 lines (57 loc) · 1.25 KB
/
configFuncs.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
package shoset
import (
"fmt"
"time"
"github.com/ditrit/shoset/msg"
)
//TODO MOVE TO GANDALF
// GetConfig :
func GetConfig(c *ShosetConn) (msg.Message, error) {
var conf msg.Config
err := c.ReadMessage(&conf)
return conf, err
}
// HandleConfig :config
func HandleConfig(c *ShosetConn, message msg.Message) error {
conf := message.(msg.Config)
c.GetCh().Queue["cmd"].Push(conf, c.GetRemoteShosetType(), c.GetLocalAddress())
return nil
}
// SendConfig :
func SendConfig(c *Shoset, cmd msg.Message) {
fmt.Print("Sending Config.\n")
c.ConnsByName.IterateAll(
func(key string, conn *ShosetConn) {
conn.SendMessage(cmd)
},
)
}
// WaitConfig :
func WaitConfig(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 {
config := message.(msg.Config)
if config.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
}
}