discovery_execution.go : not used function remove
[platform/core/system/edge-orchestration.git] / src / discoverymgr / discovery_execution.go
1 /*******************************************************************************
2  * Copyright 2019 Samsung Electronics All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  *******************************************************************************/
17
18 /*
19 NOTE : systemctl stop edge-orchestration.service
20 */
21 package discoverymgr
22
23 import (
24         // "context"
25         "log"
26         // "net"
27         "time"
28
29         "grandcat/zeroconf"
30 )
31
32 //GetDeviceList return device list
33 func GetDeviceList() ([]DeviceReturnInfo, error) {
34
35         if err := getPollingOutBoundError(); err != nil {
36                 return nil, err
37         }
38
39         waitServer()
40         gServer.IsInterfaceChange()
41
42         zeroconf.MapMTX.Lock()
43         defer zeroconf.MapMTX.Unlock()
44
45         var ret []DeviceReturnInfo
46         for key, value := range zeroconf.DeviceMap {
47                 ret = append(ret, DeviceReturnInfo{
48                         DeviceIP:     key,
49                         ServiceNames: value})
50         }
51
52         return ret, nil
53 }
54
55 //GetDeviceListWithService retune device list
56 func GetDeviceListWithService(target string) ([]string, error) {
57
58         if err := getPollingOutBoundError(); err != nil {
59                 return nil, err
60         }
61
62         waitServer()
63
64         gServer.IsInterfaceChange()
65
66         zeroconf.MapMTX.Lock()
67         defer zeroconf.MapMTX.Unlock()
68
69         var ret []string
70         for key, value := range zeroconf.DeviceMap {
71                 for _, val := range value {
72                         if val == target {
73                                 ret = append(ret, key)
74                         }
75                 }
76         }
77
78         log.Println("GetDeviceListWithService", ret)
79         return ret, nil
80 }
81
82 func waitServer() {
83         //NOTE :
84         //it can be happend, pollingOutbound function is out,
85         //but gServer is not initialized because InitDiscovery go routine
86         for {
87                 if IsNilServer() == true {
88                         time.Sleep(0)
89                 } else {
90                         break
91                 }
92         }
93 }
94
95
96 // func discoveryBGR() {
97 //      discoveryPeriod := maxPollingTime
98 //      // discoveryPeriod := 3 * 1000
99 //      for {
100
101 //              data, err := discoverDevice(5000)
102 //              if err != nil {
103 //                      log.Println(logPrefix, err)
104 //                      continue
105 //              }
106
107 //              zeroconf.MapMTX.Lock()
108 //              //@Todo check the entity in map but not discovered
109 //              for k, v := range data {
110 //                      log.Println(logPrefix, "[discoveryBGR]", k, v)
111 //                      zeroconf.DeviceMap[k] = v
112 //              }
113 //              zeroconf.MapMTX.Unlock()
114 //              time.Sleep(time.Duration(discoveryPeriod) * time.Millisecond)
115
116 //              // if discoveryPeriod > maxPollingTime {
117 //              //      time.Sleep(maxPollingTime * time.Millisecond)
118 //              // } else {
119 //              //      discoveryPeriod *= 2
120 //              //      log.Println(logPrefix, "[discoveryBGR] will run after ", discoveryPeriod/1000, "sec")
121 //              // }
122
123 //      }
124 // }
125
126 // func discoverDevice(t int) (map[string][]string, error) {
127
128 //      domain := "local"
129
130 //      // Options for IPType.
131 //      const (
132 //              IPv4        = 0x01
133 //              IPv6        = 0x02
134 //              IPv4AndIPv6 = (IPv4 | IPv6) //< Default option.
135 //      )
136
137 //      // find Wifi Interface
138 //      var netIface []net.Interface
139 //      ifaces, err := net.Interfaces()
140 //      for _, iface := range ifaces {
141 //              if iface.Name[0:2] == "wl" {
142 //                      netIface = append(netIface, iface)
143 //              }
144 //      }
145
146 //      resolver, err := zeroconf.NewResolver(zeroconf.SelectIPTraffic(IPv4), zeroconf.SelectIfaces(netIface))
147 //      if err != nil {
148 //              return nil, err
149 //      }
150
151 //      var data = make(map[string][]string) //data[deviceIP][]ServiceNames
152 //      entries := make(chan *zeroconf.ServiceEntry)
153 //      go func(results <-chan *zeroconf.ServiceEntry) {
154 //              for entry := range results {
155 //                      deviceIP := entry.AddrIPv4[0].String()
156 //                      ServiceNames := make([]string, len(entry.Text))
157 //                      ServiceNames = entry.Text
158 //                      data[deviceIP] = ServiceNames
159 //                      log.Println(logPrefix, "Discoverd Device: "+deviceIP)
160 //              }
161 //      }(entries)
162
163 //      ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(t))
164 //      defer cancel()
165 //      err = resolver.Browse(ctx, serviceType, domain, entries)
166 //      if err != nil {
167 //              return nil, err
168 //      }
169
170 //      <-ctx.Done()
171
172 //      // ExitChan <- 1
173 //      // InitDiscovery()
174
175 //      return data, nil
176 // }