diff --git a/dospin.go b/dospin.go index 13a872f..22069b6 100644 --- a/dospin.go +++ b/dospin.go @@ -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 +} diff --git a/servermanager.go b/servermanager.go index 9fbfe2b..9bca746 100644 --- a/servermanager.go +++ b/servermanager.go @@ -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 diff --git a/servicemanager.go b/servicemanager.go index ef8a5b7..ff86545 100644 --- a/servicemanager.go +++ b/servicemanager.go @@ -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() { -}