Skip to content

Commit 8cb27c6

Browse files
committed
Wait for 1.5 seconds
Former-commit-id: 67948548ecf10b45c331f71402e3d12abbe9cf3a [formerly 45b8c676d429954934c5821f2d5c9b0fff82b46c] Former-commit-id: d78de357383ae537a91acfc2f9306a94e4612ba6
1 parent 6899974 commit 8cb27c6

18 files changed

+3471
-9
lines changed

bufferflow_grbl.go

+457
Large diffs are not rendered by default.

bufferflow_grbl.go.REMOVED.git-id

-1
This file was deleted.

discovery.go

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
//
2+
// discovery.go
3+
//
4+
// Created by Martino Facchin
5+
// Copyright (c) 2015 Arduino LLC
6+
//
7+
// Permission is hereby granted, free of charge, to any person
8+
// obtaining a copy of this software and associated documentation
9+
// files (the "Software"), to deal in the Software without
10+
// restriction, including without limitation the rights to use,
11+
// copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
// copies of the Software, and to permit persons to whom the
13+
// Software is furnished to do so, subject to the following
14+
// conditions:
15+
//
16+
// The above copyright notice and this permission notice shall be
17+
// included in all copies or substantial portions of the Software.
18+
//
19+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21+
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26+
// OTHER DEALINGS IN THE SOFTWARE.
27+
//
28+
29+
package main
30+
31+
import (
32+
"github.com/oleksandr/bonjour"
33+
"log"
34+
"net"
35+
"strings"
36+
"time"
37+
)
38+
39+
const timeoutConst = 2
40+
41+
// SavedNetworkPorts contains the ports which we know are already connected
42+
var SavedNetworkPorts []OsSerialPort
43+
44+
// GetNetworkList returns a list of Network Ports
45+
// The research of network ports is articulated in two phases. First we add new ports coming from
46+
// the bonjour module, then we prune the boards who don't respond to a ping
47+
func GetNetworkList() ([]OsSerialPort, error) {
48+
newPorts, err := getPorts()
49+
if err != nil {
50+
return nil, err
51+
}
52+
53+
SavedNetworkPorts = Filter(SavedNetworkPorts, func(port OsSerialPort) bool {
54+
any := true
55+
for _, p := range newPorts {
56+
if p.Name == port.Name && p.FriendlyName == port.FriendlyName {
57+
any = false
58+
return any
59+
}
60+
}
61+
return any
62+
})
63+
64+
SavedNetworkPorts, err = pruneUnreachablePorts(SavedNetworkPorts)
65+
if err != nil {
66+
return nil, err
67+
}
68+
69+
SavedNetworkPorts = append(SavedNetworkPorts, newPorts...)
70+
71+
return SavedNetworkPorts, nil
72+
}
73+
74+
func checkAvailability(ip string) bool {
75+
timeout := time.Duration(1500 * time.Millisecond)
76+
// Check if the port 80 is open
77+
conn, err := net.DialTimeout("tcp", ip+":80", timeout)
78+
if err != nil {
79+
log.Println(err)
80+
// Check if the port 22 is open
81+
conn, err = net.DialTimeout("tcp", ip+":22", timeout)
82+
if err != nil {
83+
log.Println(err)
84+
return false
85+
}
86+
conn.Close()
87+
return true
88+
}
89+
conn.Close()
90+
return true
91+
}
92+
93+
func pruneUnreachablePorts(ports []OsSerialPort) ([]OsSerialPort, error) {
94+
times := 2
95+
96+
ports = Filter(ports, func(port OsSerialPort) bool {
97+
any := false
98+
for i := 0; i < times; i++ {
99+
if checkAvailability(port.Name) {
100+
any = true
101+
}
102+
}
103+
return any
104+
})
105+
106+
return ports, nil
107+
}
108+
109+
func getPorts() ([]OsSerialPort, error) {
110+
resolver, err := bonjour.NewResolver(nil)
111+
if err != nil {
112+
log.Println("Failed to initialize resolver:", err.Error())
113+
return nil, err
114+
}
115+
116+
results := make(chan *bonjour.ServiceEntry)
117+
118+
timeout := make(chan bool, 1)
119+
go func(exitCh chan<- bool) {
120+
time.Sleep(timeoutConst * time.Second)
121+
exitCh <- true
122+
close(results)
123+
}(resolver.Exit)
124+
125+
arrPorts := []OsSerialPort{}
126+
go func(results chan *bonjour.ServiceEntry, exitCh chan<- bool) {
127+
for e := range results {
128+
var boardInfosSlice []string
129+
for _, element := range e.Text {
130+
if strings.Contains(element, "board=yun") {
131+
boardInfosSlice = append(boardInfosSlice, "arduino:avr:yun")
132+
}
133+
}
134+
arrPorts = append(arrPorts, OsSerialPort{Name: e.AddrIPv4.String(), FriendlyName: e.Instance, NetworkPort: true, RelatedNames: boardInfosSlice})
135+
}
136+
timeout <- true
137+
}(results, resolver.Exit)
138+
139+
err = resolver.Browse("_arduino._tcp", "", results)
140+
if err != nil {
141+
log.Println("Failed to browse:", err.Error())
142+
return nil, err
143+
}
144+
// wait for some kind of timeout and return arrPorts
145+
select {
146+
case <-timeout:
147+
return arrPorts, nil
148+
}
149+
}

discovery.go.REMOVED.git-id

-1
This file was deleted.

0 commit comments

Comments
 (0)