1 /*******************************************************************************
2 * Copyright 2019 Samsung Electronics All Rights Reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 *******************************************************************************/
19 NOTE : systemctl stop edge-orchestration.service
32 //GetDeviceList return device list
33 func GetDeviceList() ([]DeviceReturnInfo, error) {
35 if err := getPollingOutBoundError(); err != nil {
40 gServer.IsInterfaceChange()
42 zeroconf.MapMTX.Lock()
43 defer zeroconf.MapMTX.Unlock()
45 var ret []DeviceReturnInfo
46 for key, value := range zeroconf.DeviceMap {
47 ret = append(ret, DeviceReturnInfo{
55 //GetDeviceListWithService retune device list
56 func GetDeviceListWithService(target string) ([]string, error) {
58 if err := getPollingOutBoundError(); err != nil {
64 gServer.IsInterfaceChange()
66 zeroconf.MapMTX.Lock()
67 defer zeroconf.MapMTX.Unlock()
70 for key, value := range zeroconf.DeviceMap {
71 for _, val := range value {
73 ret = append(ret, key)
78 log.Println("GetDeviceListWithService", ret)
84 //it can be happend, pollingOutbound function is out,
85 //but gServer is not initialized because InitDiscovery go routine
87 if IsNilServer() == true {
96 // func discoveryBGR() {
97 // discoveryPeriod := maxPollingTime
98 // // discoveryPeriod := 3 * 1000
101 // data, err := discoverDevice(5000)
103 // log.Println(logPrefix, err)
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
113 // zeroconf.MapMTX.Unlock()
114 // time.Sleep(time.Duration(discoveryPeriod) * time.Millisecond)
116 // // if discoveryPeriod > maxPollingTime {
117 // // time.Sleep(maxPollingTime * time.Millisecond)
119 // // discoveryPeriod *= 2
120 // // log.Println(logPrefix, "[discoveryBGR] will run after ", discoveryPeriod/1000, "sec")
126 // func discoverDevice(t int) (map[string][]string, error) {
130 // // Options for IPType.
134 // IPv4AndIPv6 = (IPv4 | IPv6) //< Default option.
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)
146 // resolver, err := zeroconf.NewResolver(zeroconf.SelectIPTraffic(IPv4), zeroconf.SelectIfaces(netIface))
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)
163 // ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(t))
165 // err = resolver.Browse(ctx, serviceType, domain, entries)
173 // // InitDiscovery()