discoverymgr : Add polling logic that get outbound ip 78/203478/2
authorjw_wonny.cha <jw_wonny.cha@samsung.com>
Mon, 15 Apr 2019 06:16:55 +0000 (15:16 +0900)
committerjw_wonny.cha <jw_wonny.cha@samsung.com>
Mon, 15 Apr 2019 07:05:04 +0000 (16:05 +0900)
Change-Id: I0b8f1d31ed666d4430c5b0d24929fa3110e755a7
Signed-off-by: jw_wonny.cha <jw_wonny.cha@samsung.com>
src/discoverymgr/discovery_init.go

index 4aa2677..5b03c74 100755 (executable)
@@ -22,6 +22,7 @@ import (
        "log"
        "net"
        "strings"
+       "time"
 
        "github.com/grandcat/zeroconf"
 )
@@ -32,10 +33,13 @@ var gServer *zeroconf.Server
 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()
@@ -44,7 +48,6 @@ func InitDiscovery() (err error) {
 
 func registerDevice(AppNames []string, ret chan error) {
        serviceName, err := getMacAddr()
-
        if err != nil {
                ret <- err
                return
@@ -71,12 +74,17 @@ func registerDevice(AppNames []string, ret chan error) {
 }
 
 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
        }
@@ -97,5 +105,22 @@ func getMacAddr() (macAddr string, err error) {
                }
        }
 
-       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
+                       }
+               }
+       }
 }