From 440b7f422c45423e9763b653bfede9033d26cc21 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Fri, 28 Apr 2023 15:00:15 -0600 Subject: [PATCH] move event handling for AcnStatus engine reboot from lcg into app --- app/app.go | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/app/app.go b/app/app.go index 634f245..29aef9a 100644 --- a/app/app.go +++ b/app/app.go @@ -31,6 +31,7 @@ type application struct { engines map[string]connections.Engine appBus event.Manager + eventQueue event.Queue appmutex sync.Mutex engineHooks connections.EngineHooks @@ -97,7 +98,7 @@ func LoadAppSettings(appDirectory string) *settings.GlobalSettingsFile { // NewApp creates a new app with some environment awareness and initializes a Tor Manager func NewApp(acn connectivity.ACN, appDirectory string, settings *settings.GlobalSettingsFile) Application { - app := &application{engines: make(map[string]connections.Engine), eventBuses: make(map[string]event.Manager), directory: appDirectory, appBus: event.NewEventManager(), settings: settings} + app := &application{engines: make(map[string]connections.Engine), eventBuses: make(map[string]event.Manager), directory: appDirectory, appBus: event.NewEventManager(), settings: settings, eventQueue: event.NewQueue()} app.peers = make(map[string]peer.CwtchPeer) app.engineHooks = connections.DefaultEngineHooks{} app.acn = acn @@ -107,6 +108,9 @@ func NewApp(acn connectivity.ACN, appDirectory string, settings *settings.Global prog, status := acn.GetBootstrapStatus() statusHandler(prog, status) + app.GetPrimaryBus().Subscribe(event.ACNStatus, app.eventQueue) + go app.eventHandler() + return app } @@ -474,6 +478,48 @@ func (app *application) QueryACNVersion() { app.appBus.Publish(event.NewEventList(event.ACNVersion, event.Data, version)) } +func (app *application) eventHandler() { + acnStatus := -1 + for { + e := app.eventQueue.Next() + switch e.EventType { + case event.ACNStatus: + newAcnStatus, err := strconv.Atoi(e.Data[event.Progress]) + if err != nil { + break + } + if newAcnStatus == 100 { + if acnStatus != 100 { + for _, onion := range app.ListProfiles() { + profile := app.GetPeer(onion) + if profile != nil { + autostart, exists := profile.GetScopedZonedAttribute(attr.LocalScope, attr.ProfileZone, constants.PeerAutostart) + if !exists || autostart == "true" { + app.ActivatePeerEngine(onion) + } + } + } + } + } else { + if acnStatus == 100 { + // just fell offline + for _, onion := range app.ListProfiles() { + app.DeactivatePeerEngine(onion) + } + } + } + acnStatus = newAcnStatus + + default: + // invalid event, signifies shutdown + if e.EventType == "" { + return + } + + } + } +} + // ShutdownPeer shuts down a peer and removes it from the app's management func (app *application) ShutdownPeer(onion string) { app.appmutex.Lock() @@ -519,6 +565,7 @@ func (app *application) Shutdown() { app.shutdownPeer(id) } log.Debugf("Shutting Down App") + app.eventQueue.Shutdown() app.appBus.Shutdown() log.Debugf("Shut Down Complete") }