Skip to content

Commit 0003df7

Browse files
author
Alice Pintus
committed
Merge pull request arduino#21 from arduino/port_discovery
Port discovery
2 parents 53f6f8b + 280c768 commit 0003df7

File tree

6 files changed

+53
-19
lines changed

6 files changed

+53
-19
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Please use the current latest version:
2323

2424
## Submitting an issue
2525

26-
Please attach the output of the commands running at http://localhost:8989 if useful.
26+
Please attach the output of the commands running at the debug console if useful.
2727

2828
## Submitting a pull request
2929

config.ini

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
addr = :8989 # http service address
21
configUpdateInterval = 0 # Update interval for re-reading config file set via -config flag. Zero disables config file re-reading.
32
gc = std # Type of garbage collection. std = Normal garbage collection allowing system to decide (this has been known to cause a stop the world in the middle of a CNC job which can cause lost responses from the CNC controller and thus stalled jobs. use max instead to solve.), off = let memory grow unbounded (you have to send in the gc command manually to garbage collect or you will run out of RAM eventually), max = Force garbage collection on each recv or send on a serial port (this minimizes stop the world events and thus lost serial responses, but increases CPU usage)
43
hostname = unknown-hostname # Override the hostname we get from the OS

hub.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ func restart(path string) {
321321
hiberString = "-hibernate"
322322
}
323323

324-
cmd = exec.Command(exePath, "-ls", "-addr", *addr, "-regex", *regExpFilter, "-gc", *gcType, hiberString)
324+
cmd = exec.Command(exePath, "-ls", "-regex", *regExpFilter, "-gc", *gcType, hiberString)
325325

326326
fmt.Println(cmd)
327327

info.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
)
6+
7+
func infoHandler(c *gin.Context) {
8+
c.JSON(200, gin.H{
9+
"http": "http://localhost" + port,
10+
"https": "https://localhost" + port,
11+
"ws": "ws://localhost" + port,
12+
"wss": "wss://localhost" + portSSL,
13+
})
14+
}

main.go

+36-15
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"os/user"
1616
"path/filepath"
1717
"runtime/debug"
18+
"strconv"
1819
"text/template"
1920
"time"
2021
//"github.com/sanbornm/go-selfupdate/selfupdate" #included in update.go to change heavily
@@ -26,8 +27,6 @@ var (
2627
embedded_autoupdate = true
2728
embedded_autoextract = false
2829
hibernate = flag.Bool("hibernate", false, "start hibernated")
29-
addr = flag.String("addr", ":8989", "http service address")
30-
addrSSL = flag.String("addrSSL", ":8990", "https service address")
3130
verbose = flag.Bool("v", true, "show debug logging")
3231
//verbose = flag.Bool("v", false, "show debug logging")
3332
isLaunchSelf = flag.Bool("ls", false, "launch self 5 seconds later")
@@ -41,6 +40,8 @@ var (
4140
appName = flag.String("appName", "", "")
4241
globalToolsMap = make(map[string]string)
4342
tempToolsPath = createToolsDir()
43+
port string
44+
portSSL string
4445
)
4546

4647
type NullWriter int
@@ -137,7 +138,6 @@ func main() {
137138
}
138139
}
139140

140-
f := flag.Lookup("addr")
141141
log.Println("Version:" + version)
142142

143143
// hostname
@@ -159,11 +159,6 @@ func main() {
159159
debug.SetGCPercent(-1)
160160
}
161161

162-
ip := "0.0.0.0"
163-
log.Print("Starting server and websocket on " + ip + "" + f.Value.String())
164-
165-
log.Println("The Arduino Create Agent is now running")
166-
167162
// see if they provided a regex filter
168163
if len(*regExpFilter) > 0 {
169164
log.Printf("You specified a serial port regular expression filter: %v\n", *regExpFilter)
@@ -214,17 +209,43 @@ func main() {
214209
r.POST("/socket.io/", socketHandler)
215210
r.Handle("WS", "/socket.io/", socketHandler)
216211
r.Handle("WSS", "/socket.io/", socketHandler)
212+
r.GET("/info", infoHandler)
217213
go func() {
218-
if err := r.RunTLS(*addrSSL, filepath.Join(dest, "cert.pem"), filepath.Join(dest, "key.pem")); err != nil {
219-
log.Printf("Error trying to bind to port: %v, so exiting...", err)
220-
log.Fatal("Error ListenAndServe:", err)
214+
start := 49152
215+
end := 65535
216+
i := start
217+
for i < end {
218+
i = i + 1
219+
portSSL = ":" + strconv.Itoa(i)
220+
if err := r.RunTLS(portSSL, filepath.Join(dest, "cert.pem"), filepath.Join(dest, "key.pem")); err != nil {
221+
log.Printf("Error trying to bind to port: %v, so exiting...", err)
222+
continue
223+
} else {
224+
ip := "0.0.0.0"
225+
log.Print("Starting server and websocket (SSL) on " + ip + "" + port)
226+
break
227+
}
228+
}
229+
}()
230+
231+
go func() {
232+
start := 49152
233+
end := 65535
234+
i := start
235+
for i < end {
236+
i = i + 1
237+
port = ":" + strconv.Itoa(i)
238+
if err := r.Run(port); err != nil {
239+
log.Printf("Error trying to bind to port: %v, so exiting...", err)
240+
continue
241+
} else {
242+
ip := "0.0.0.0"
243+
log.Print("Starting server and websocket on " + ip + "" + port)
244+
break
245+
}
221246
}
222247
}()
223248

224-
if err := r.Run(*addr); err != nil {
225-
log.Printf("Error trying to bind to port: %v, so exiting...", err)
226-
log.Fatal("Error ListenAndServe:", err)
227-
}
228249
}()
229250
}
230251
setupSysTray()

trayicon.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func setupSysTrayReal() {
9191
for {
9292
<-mDebug.ClickedCh
9393
logAction("log on")
94-
open.Start("http://localhost:8989")
94+
open.Start("http://localhost" + port)
9595
}
9696
}()
9797

0 commit comments

Comments
 (0)