move event handling for AcnStatus engine reboot from lcg into app
This commit is contained in:
parent
12b89966de
commit
440b7f422c
49
app/app.go
49
app/app.go
|
@ -31,6 +31,7 @@ type application struct {
|
||||||
|
|
||||||
engines map[string]connections.Engine
|
engines map[string]connections.Engine
|
||||||
appBus event.Manager
|
appBus event.Manager
|
||||||
|
eventQueue event.Queue
|
||||||
appmutex sync.Mutex
|
appmutex sync.Mutex
|
||||||
engineHooks connections.EngineHooks
|
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
|
// 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 {
|
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.peers = make(map[string]peer.CwtchPeer)
|
||||||
app.engineHooks = connections.DefaultEngineHooks{}
|
app.engineHooks = connections.DefaultEngineHooks{}
|
||||||
app.acn = acn
|
app.acn = acn
|
||||||
|
@ -107,6 +108,9 @@ func NewApp(acn connectivity.ACN, appDirectory string, settings *settings.Global
|
||||||
prog, status := acn.GetBootstrapStatus()
|
prog, status := acn.GetBootstrapStatus()
|
||||||
statusHandler(prog, status)
|
statusHandler(prog, status)
|
||||||
|
|
||||||
|
app.GetPrimaryBus().Subscribe(event.ACNStatus, app.eventQueue)
|
||||||
|
go app.eventHandler()
|
||||||
|
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -474,6 +478,48 @@ func (app *application) QueryACNVersion() {
|
||||||
app.appBus.Publish(event.NewEventList(event.ACNVersion, event.Data, version))
|
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
|
// ShutdownPeer shuts down a peer and removes it from the app's management
|
||||||
func (app *application) ShutdownPeer(onion string) {
|
func (app *application) ShutdownPeer(onion string) {
|
||||||
app.appmutex.Lock()
|
app.appmutex.Lock()
|
||||||
|
@ -519,6 +565,7 @@ func (app *application) Shutdown() {
|
||||||
app.shutdownPeer(id)
|
app.shutdownPeer(id)
|
||||||
}
|
}
|
||||||
log.Debugf("Shutting Down App")
|
log.Debugf("Shutting Down App")
|
||||||
|
app.eventQueue.Shutdown()
|
||||||
app.appBus.Shutdown()
|
app.appBus.Shutdown()
|
||||||
log.Debugf("Shut Down Complete")
|
log.Debugf("Shut Down Complete")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue