// Performs all operations required to close this app. // Closes the network resource, queues a new STATUS_DOWN, // removes from the registry, and logs the close. func closeApp(a *app.App) { // this really should be somewhere else in the code, but i can't figure out where // since most places would lead to circular references a.Connection.Close() if a.Manifest != nil { cmd.ChangeAppStatus(a, app.STATUS_DOWN, 0) registry.Remove(a) logging.Info("Closing application %s", a.Manifest.InstanceId) } else { logging.Info("Closing application") } }
// change the app's status and notify all those that depend on this app func (c *commandStrategy) statusChange() error { var newStatus int priority := -1 switch c.verb { case "APP_UP": newStatus = app.STATUS_UP case "APP_DOWN": newStatus = app.STATUS_DOWN case "APP_IN_USE": newStatus = app.STATUS_IN_USE default: return errors.New("Unrecognized status verb") } if c.app.Status != newStatus { c.app.RWMutex.Lock() c.app.Status = newStatus c.app.Priority = priority if c.app.Manifest != nil { logging.Info("Changing status of %s to '%s'\n", c.app.Manifest.InstanceId, c.app.StatusString()) sendDependencyNotice(c.app) } c.app.RWMutex.Unlock() } return nil }
// main parses command-line arguments and spawns a new server func main() { logging.SetLevel(logging.DEBUG) no_tls := flag.Bool("no-tls", false, "Whether to use TLS, default false") crt_path := flag.String("crt", "cert.crt", "Path to the TLS certificate, default `cert.crt`") key_path := flag.String("key", "key.key", "Path to the TLS key, default `key.key`") sname := flag.String("srv-name", "mycroft", "This server's name for SNI") flag.Parse() logging.Info("Starting Server ...") if *no_tls { logging.Warning("not using TLS") err := srv.StartListen(1847, false, "", "", "") if err != nil { logging.Fatal("Could not start server: ", err.Error()) } } else { err := srv.StartListen(1847, true, *crt_path, *key_path, *sname) if err != nil { logging.Fatal("Could not start server: ", err.Error()) } } }
// construct the app based on its manifest func (c *commandStrategy) appManifest() bool { c.app.RWMutex.Lock() defer c.app.RWMutex.Unlock() logging.Debug("Parsing application's manifest") man, err := decodeManifest(c.body) if err != nil { logging.Error("App's manifest is invalid: %s", err) sendManifestFail(c.app, err.Error()) return false } if _, exists := registry.GetInstance(man.InstanceId); exists { logging.Error("App's instance id is already taken: %s", man.InstanceId) sendManifestFail(c.app, fmt.Sprintf("instanceId %s is in use", man.InstanceId)) return false } c.app.Status = app.STATUS_DOWN c.app.Manifest = man registry.Register(c.app) sendManifestOkAndDependencies(c.app) logging.Info("App '%s' now connected with manifest parsed", c.app.Manifest.InstanceId) return true }