Fixup Server Settings + IntentRunning event

This commit is contained in:
Sarah Jamie Lewis 2023-03-01 12:09:26 -08:00
parent a48b217d2c
commit da1658d36a
3 changed files with 27 additions and 7 deletions

View File

@ -44,14 +44,12 @@ type ServerInfo struct {
StorageType string StorageType string
} }
type PublishFn func(event.Event)
var lock sync.Mutex var lock sync.Mutex
var appServers server.Servers var appServers server.Servers
var killStatsUpdate chan bool = make(chan bool, 1) var killStatsUpdate chan bool = make(chan bool, 1)
var enabled bool = false var enabled bool = false
func Init(acn connectivity.ACN, appdir string, ) *ServersFunctionality { func Init(acn connectivity.ACN, appdir string) *ServersFunctionality {
lock.Lock() lock.Lock()
defer lock.Unlock() defer lock.Unlock()
if appServers == nil { if appServers == nil {
@ -73,12 +71,21 @@ var acnStatus = -1
func (sf *ServersFunctionality) OnACNStatusEvent(appl app.Application, e *event.Event) { func (sf *ServersFunctionality) OnACNStatusEvent(appl app.Application, e *event.Event) {
newAcnStatus, err := strconv.Atoi(e.Data[event.Progress]) newAcnStatus, err := strconv.Atoi(e.Data[event.Progress])
if err != nil { if !appl.IsFeatureEnabled(serversExperiment) || err != nil {
return return
} }
if newAcnStatus == 100 { if newAcnStatus == 100 {
if acnStatus != 100 { if acnStatus != 100 {
// just came online // just came online
for _, onion := range sf.ListServers() {
autostart := false
if s := sf.GetServer(onion); s != nil {
autostart = s.GetAttribute(server.AttrAutostart) == "true"
}
if autostart {
sf.LaunchServer(appl, onion)
}
}
} }
} else { } else {
if acnStatus == 100 { if acnStatus == 100 {
@ -114,11 +121,11 @@ type ServersFunctionality struct {
func (sh *ServersFunctionality) UpdateSettings(appl app.Application, acn connectivity.ACN) { func (sh *ServersFunctionality) UpdateSettings(appl app.Application, acn connectivity.ACN) {
if appl.IsFeatureEnabled(serversExperiment) { if appl.IsFeatureEnabled(serversExperiment) {
sh.Disable()
} else {
sh.LoadServers(appl, acn, app.DefactoPasswordForUnencryptedProfiles) sh.LoadServers(appl, acn, app.DefactoPasswordForUnencryptedProfiles)
if !sh.Enabled() { if !sh.Enabled() {
sh.Enable(appl, acn) sh.Enable(appl, acn)
// republish servers
serversList := sh.ListServers() serversList := sh.ListServers()
for _, server := range serversList { for _, server := range serversList {
serverInfo := sh.GetServerInfo(server) serverInfo := sh.GetServerInfo(server)
@ -128,6 +135,8 @@ func (sh *ServersFunctionality) UpdateSettings(appl app.Application, acn connect
} }
sh.LaunchServers(appl, acn) sh.LaunchServers(appl, acn)
} }
} else {
sh.Disable()
} }
} }
@ -164,7 +173,7 @@ func (sh *ServersFunctionality) LaunchServers(appl app.Application, acn connecti
} }
} }
func (sf *ServersFunctionality) Enable(application app.Application, acn connectivity.ACN, ) { func (sf *ServersFunctionality) Enable(application app.Application, acn connectivity.ACN) {
lock.Lock() lock.Lock()
defer lock.Unlock() defer lock.Unlock()
if appServers != nil && !enabled { if appServers != nil && !enabled {
@ -262,12 +271,14 @@ func (sf *ServersFunctionality) DeleteServer(appl app.Application, onion string,
} }
func (sf *ServersFunctionality) LaunchServer(appl app.Application, onion string) { func (sf *ServersFunctionality) LaunchServer(appl app.Application, onion string) {
log.Infof("Launching Server...")
if appl.IsFeatureEnabled(serversExperiment) { if appl.IsFeatureEnabled(serversExperiment) {
appServers.LaunchServer(onion) appServers.LaunchServer(onion)
server := appServers.GetServer(onion) server := appServers.GetServer(onion)
if server != nil { if server != nil {
newStats := server.GetStatistics() newStats := server.GetStatistics()
appl.GetPrimaryBus().Publish(event.NewEventList(ServerStatsUpdate, event.Identity, onion, TotalMessages, strconv.Itoa(newStats.TotalMessages), Connections, strconv.Itoa(newStats.TotalConnections))) appl.GetPrimaryBus().Publish(event.NewEventList(ServerStatsUpdate, event.Identity, onion, TotalMessages, strconv.Itoa(newStats.TotalMessages), Connections, strconv.Itoa(newStats.TotalConnections)))
appl.GetPrimaryBus().Publish(event.NewEventList(ServerIntentUpdate, event.Identity, onion, Intent, IntentRunning))
} }
} }
} }

View File

@ -20,6 +20,8 @@ func main() {
generatedBindingsPrefix := `` generatedBindingsPrefix := ``
generatedBindings := `` generatedBindings := ``
experimentRegistry := `` experimentRegistry := ``
experimentUpdateSettings := ``
file, err := os.Open("spec") file, err := os.Open("spec")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -70,6 +72,11 @@ func main() {
eventHandler.AddModule(%s) eventHandler.AddModule(%s)
%s.Enable(application, &globalACN) %s.Enable(application, &globalACN)
`, parts[2], parts[4], parts[2], parts[2]) `, parts[2], parts[4], parts[2], parts[2])
experimentUpdateSettings += fmt.Sprintf(`
%s.UpdateSettings(application, &globalACN)
`, parts[2])
continue continue
} }
@ -112,6 +119,7 @@ func main() {
templateString := string(template) templateString := string(template)
templateString = strings.ReplaceAll(templateString, "{{BINDINGS}}", generatedBindings) templateString = strings.ReplaceAll(templateString, "{{BINDINGS}}", generatedBindings)
templateString = strings.ReplaceAll(templateString, "{{EXPERIMENT_REGISTER}}", experimentRegistry) templateString = strings.ReplaceAll(templateString, "{{EXPERIMENT_REGISTER}}", experimentRegistry)
templateString = strings.ReplaceAll(templateString, "{{EXPERIMENT_UPDATESETTINGS}}", experimentUpdateSettings)
templateString = strings.ReplaceAll(templateString, "{{IMPORTS}}", generatedBindingsPrefix) templateString = strings.ReplaceAll(templateString, "{{IMPORTS}}", generatedBindingsPrefix)
os.WriteFile("lib.go", []byte(templateString), 0644) os.WriteFile("lib.go", []byte(templateString), 0644)
} }

View File

@ -488,6 +488,7 @@ func UpdateSettings(settingsJson string) {
var newSettings app.GlobalSettings var newSettings app.GlobalSettings
json.Unmarshal([]byte(settingsJson), &newSettings) json.Unmarshal([]byte(settingsJson), &newSettings)
application.UpdateSettings(newSettings) application.UpdateSettings(newSettings)
{{EXPERIMENT_UPDATESETTINGS}}
} }