Moved port listening into ServerManager.
This commit is contained in:
parent
ff49b1be3f
commit
0209582028
@ -58,10 +58,10 @@ func runServer(opts cmdOptions) {
|
||||
// start the ServerManager
|
||||
go sm.Serve()
|
||||
|
||||
// assign this ServerManager to all appropriate ports
|
||||
// assign this ServerManager all appropriate ports
|
||||
for _, port := range sv.Ports {
|
||||
log.Println("Setting up port", port)
|
||||
setupService(sm, port)
|
||||
sm.setupListener(port)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,7 +7,11 @@
|
||||
*/
|
||||
package main
|
||||
|
||||
import "log"
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const (
|
||||
SERVERMANAGER_SPINUP = iota
|
||||
@ -92,6 +96,38 @@ func (me *ServerManager) addPortForwards(ip string) {
|
||||
func (me *ServerManager) rmPortForwards() {
|
||||
}
|
||||
|
||||
func (me *ServerManager) setupListener(port int) {
|
||||
portStr := strconv.Itoa(port)
|
||||
addr, err := net.ResolveTCPAddr("tcp", "0.0.0.0:"+portStr)
|
||||
if err != nil {
|
||||
log.Print("Could not resolve port and listen address:", err)
|
||||
return
|
||||
}
|
||||
|
||||
// listen on port
|
||||
go func() {
|
||||
l, err := net.ListenTCP("tcp", addr)
|
||||
if err != nil {
|
||||
log.Print("Could not listen for TCP connection:", err)
|
||||
} else {
|
||||
for {
|
||||
conn, err := l.AcceptTCP()
|
||||
if err != nil {
|
||||
log.Print("Could not accept TCP connection:", err)
|
||||
} else {
|
||||
// connection accepted
|
||||
|
||||
// spinup machine
|
||||
me.Spinup()
|
||||
|
||||
// close existing connection, not doing anything with it
|
||||
conn.Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (me *ServerManager) serveAction(action int) bool {
|
||||
running := true
|
||||
switch action {
|
||||
|
@ -1,46 +0,0 @@
|
||||
/*
|
||||
Copyright 2016 gtalent2@gmail.com
|
||||
|
||||
This Source Code Form is subject to the terms of the Mozilla Public
|
||||
License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func setupService(serverManager *ServerManager, port int) {
|
||||
portStr := strconv.Itoa(port)
|
||||
addr, err := net.ResolveTCPAddr("tcp", "0.0.0.0:"+portStr)
|
||||
if err != nil {
|
||||
log.Print("Could not resolve port and listen address:", err)
|
||||
return
|
||||
}
|
||||
|
||||
// listen on port
|
||||
go func() {
|
||||
l, err := net.ListenTCP("tcp", addr)
|
||||
if err != nil {
|
||||
log.Print("Could not listen for TCP connection:", err)
|
||||
} else {
|
||||
for {
|
||||
conn, err := l.AcceptTCP()
|
||||
if err != nil {
|
||||
log.Print("Could not accept TCP connection:", err)
|
||||
} else {
|
||||
// connection accepted
|
||||
|
||||
// spinup machine
|
||||
serverManager.Spinup()
|
||||
|
||||
// close existing connection, not doing anything with it
|
||||
conn.Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
Loading…
Reference in New Issue
Block a user