-
Notifications
You must be signed in to change notification settings - Fork 1
/
listen.go
62 lines (46 loc) · 994 Bytes
/
listen.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 listen
import (
"encoding/json"
"fmt"
"net"
"github.com/asteris-llc/mantl-bootstrap/common"
"github.com/asteris-llc/mantl-bootstrap/structs"
"github.com/spf13/cobra"
)
func Init(root *cobra.Command) {
listenCmd := &cobra.Command{
Use: "listen",
Short: "Listen for bootstrap information",
Long: "Listen for bootstrap information",
RunE: func(cmd *cobra.Command, args []string) error {
return Listen(args)
},
}
root.AddCommand(listenCmd)
}
func Listen(args []string) error {
c := common.DefaultConfig()
l, err := net.Listen(c.Type, fmt.Sprintf(":%d", c.Port))
if err != nil {
return err
}
var bs structs.Bootstrap
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
continue
}
d := json.NewDecoder(conn)
if err := d.Decode(&bs); err != nil {
fmt.Println(err)
conn.Close()
continue
}
common.SendResponse(common.MSG_SUCCESS, "Success", conn)
conn.Close()
break
}
fmt.Printf("%+v\n", bs)
return nil
}