"log"
"net"
"strings"
+ "time"
"github.com/grandcat/zeroconf"
)
func InitDiscovery() (err error) {
registerCh := make(chan error)
go registerDevice(nil, registerCh)
- err = <-registerCh
- if err != nil {
- log.Println("[Fail] " + err.Error())
- return
+
+ select {
+ case err = <-registerCh:
+ if err != nil {
+ log.Println("[Fail] " + err.Error())
+ return
+ }
}
ResetServiceName()
func registerDevice(AppNames []string, ret chan error) {
serviceName, err := getMacAddr()
-
if err != nil {
ret <- err
return
}
func getMacAddr() (macAddr string, err error) {
- ifas, err := net.Interfaces()
- if err != nil {
- return
+ var outboundIP string
+ ipChan := make(chan string, 1)
+
+ go pollGettingOutBoundIP(ipChan)
+
+ select {
+ case outboundIP = <-ipChan:
+ log.Println("[outboundIP]", outboundIP)
}
- outboundIP, err := common.GetOutboundIP()
+ ifas, err := net.Interfaces()
if err != nil {
return
}
}
}
- return "", errors.New("Can't get Unique ServiceName")
+ return "", errors.New("Can't get Mac Address")
+}
+
+func pollGettingOutBoundIP(outboundIP chan string) {
+ ticker := time.NewTicker(time.Duration(1) * time.Second)
+ defer func() { ticker.Stop() }()
+
+ for {
+ select {
+ case <-ticker.C:
+ log.Println("pollGettingOutBoundIP")
+ ip, _ := common.GetOutboundIP()
+ if len(ip) != 0 {
+ outboundIP <- ip
+ break
+ }
+ }
+ }
}