Setup to actually listen on the relevant ports.
This commit is contained in:
parent
c4dc4c2de1
commit
9a5eb7ce4b
35
dospin.go
35
dospin.go
@ -23,14 +23,7 @@ func parseCmdOptions() cmdOptions {
|
|||||||
return o
|
return o
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func testServerManager(settings Settings) {
|
||||||
opts := parseCmdOptions()
|
|
||||||
log.Println("Loading config:", opts.config)
|
|
||||||
settings, err := loadSettings(opts.config)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
dh := NewDropletHandler(settings)
|
dh := NewDropletHandler(settings)
|
||||||
sm := NewServerManager("minecraft", dh, settings)
|
sm := NewServerManager("minecraft", dh, settings)
|
||||||
go sm.Serve()
|
go sm.Serve()
|
||||||
@ -41,3 +34,29 @@ func main() {
|
|||||||
sm.Stop()
|
sm.Stop()
|
||||||
sm.Done()
|
sm.Done()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
opts := parseCmdOptions()
|
||||||
|
log.Println("Loading config:", opts.config)
|
||||||
|
settings, err := loadSettings(opts.config)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, sv := range settings.Servers {
|
||||||
|
dh := NewDropletHandler(settings)
|
||||||
|
sm := NewServerManager(name, dh, settings)
|
||||||
|
|
||||||
|
// start the ServerManager
|
||||||
|
go sm.Serve()
|
||||||
|
|
||||||
|
// assign this ServerManager to all appropriate ports
|
||||||
|
for _, port := range sv.Ports {
|
||||||
|
log.Println("Setting up port", port)
|
||||||
|
setupService(sm, port)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
done := make(chan interface{})
|
||||||
|
<-done
|
||||||
|
}
|
||||||
|
@ -15,6 +15,12 @@ const (
|
|||||||
SERVERMANAGER_STOP
|
SERVERMANAGER_STOP
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ServerHandler interface {
|
||||||
|
// Takes snapshot name, and returns the IP to connect to.
|
||||||
|
Spinup(name string) (string, error)
|
||||||
|
Spindown(name string) error
|
||||||
|
}
|
||||||
|
|
||||||
type ServerManager struct {
|
type ServerManager struct {
|
||||||
name string
|
name string
|
||||||
ports []int
|
ports []int
|
||||||
|
@ -13,36 +13,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ServerHandler interface {
|
func setupService(serverManager *ServerManager, port int) {
|
||||||
// Takes snapshot name, and returns the IP to connect to.
|
|
||||||
Spinup(name string) (string, error)
|
|
||||||
Spindown(name string) error
|
|
||||||
}
|
|
||||||
|
|
||||||
type service struct {
|
|
||||||
name string
|
|
||||||
/*
|
|
||||||
This should start at 0 and should be incremented any time a cleanup check
|
|
||||||
shows no connections on this port. Once it reaches 5, the port forward
|
|
||||||
should be deleted along with that port in the map
|
|
||||||
*/
|
|
||||||
connectionStatus int
|
|
||||||
}
|
|
||||||
|
|
||||||
// Listens for clients on given ports to spin up machines for the ports
|
|
||||||
type ServiceManager struct {
|
|
||||||
machineManager ServerHandler
|
|
||||||
machineSvcCnt map[string]int
|
|
||||||
svcConnStatus map[int]service
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewServiceHandler(mm ServerHandler) *ServiceManager {
|
|
||||||
sh := new(ServiceManager)
|
|
||||||
sh.machineManager = mm
|
|
||||||
return sh
|
|
||||||
}
|
|
||||||
|
|
||||||
func (me *ServiceManager) setupService(serviceName, machineName string, port int) {
|
|
||||||
portStr := strconv.Itoa(port)
|
portStr := strconv.Itoa(port)
|
||||||
addr, err := net.ResolveTCPAddr("tcp", "0.0.0.0:"+portStr)
|
addr, err := net.ResolveTCPAddr("tcp", "0.0.0.0:"+portStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -64,16 +35,7 @@ func (me *ServiceManager) setupService(serviceName, machineName string, port int
|
|||||||
// connection accepted
|
// connection accepted
|
||||||
|
|
||||||
// spinup machine
|
// spinup machine
|
||||||
ip, err := me.machineManager.Spinup(machineName)
|
serverManager.Spinup()
|
||||||
|
|
||||||
// setup port forwarding
|
|
||||||
if err == nil {
|
|
||||||
setupPortForward(ip, portStr)
|
|
||||||
me.machineSvcCnt[machineName]++
|
|
||||||
me.svcConnStatus[port] = service{name: serviceName, connectionStatus: 0}
|
|
||||||
} else {
|
|
||||||
log.Print("Could not setup machine "+machineName+":", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// close existing connection, not doing anything with it
|
// close existing connection, not doing anything with it
|
||||||
conn.Close()
|
conn.Close()
|
||||||
@ -82,10 +44,3 @@ func (me *ServiceManager) setupService(serviceName, machineName string, port int
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
Periodically checks number of connections to each machine and deletes
|
|
||||||
them when they are no longer needed
|
|
||||||
*/
|
|
||||||
func (me *ServiceManager) cleanup() {
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user