Setup to actually listen on the relevant ports.

This commit is contained in:
Gary Talent 2016-02-29 06:06:31 +00:00
parent c4dc4c2de1
commit 9a5eb7ce4b
3 changed files with 35 additions and 55 deletions

View File

@ -23,14 +23,7 @@ func parseCmdOptions() cmdOptions {
return o
}
func main() {
opts := parseCmdOptions()
log.Println("Loading config:", opts.config)
settings, err := loadSettings(opts.config)
if err != nil {
log.Fatal(err)
}
func testServerManager(settings Settings) {
dh := NewDropletHandler(settings)
sm := NewServerManager("minecraft", dh, settings)
go sm.Serve()
@ -41,3 +34,29 @@ func main() {
sm.Stop()
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
}

View File

@ -15,6 +15,12 @@ const (
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 {
name string
ports []int

View File

@ -13,36 +13,7 @@ import (
"strconv"
)
type ServerHandler interface {
// 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) {
func setupService(serverManager *ServerManager, port int) {
portStr := strconv.Itoa(port)
addr, err := net.ResolveTCPAddr("tcp", "0.0.0.0:"+portStr)
if err != nil {
@ -64,16 +35,7 @@ func (me *ServiceManager) setupService(serviceName, machineName string, port int
// connection accepted
// spinup machine
ip, err := me.machineManager.Spinup(machineName)
// 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)
}
serverManager.Spinup()
// close existing connection, not doing anything with it
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() {
}