func (mc *masterClock) tickClocks() { ticker := time.NewTicker(durationPerTick) for range ticker.C { statemanager.Lock() mc.ticker() statemanager.Unlock() } }
// Start initalizes all scoreboard subsystems and starts up a webserver on port func Start(port uint16) { l := openLog() if l != nil { defer l.Close() } mux := http.NewServeMux() var savers []*statemanager.Saver // Initialize statemanager and load Settings.* statemanager.Initialize() savers = append(savers, initSettings("config/settings")) // Initialize leagues and load Leagues.* leagues.Initialize() savers = append(savers, statemanager.NewSaver("config/leagues", "Leagues", time.Duration(5)*time.Second, true, true)) // Initialize scoreboard and load Scoreboard.* statemanager.Lock() scoreboard.New() statemanager.Unlock() savers = append(savers, statemanager.NewSaver("config/scoreboard", "Scoreboard", time.Duration(5)*time.Second, true, true)) // Initialize websocket interface websocket.Initialize(mux) addFileWatcher("TeamLogos", "html", "/images/teamlogo") addFileWatcher("Sponsors", "html", "/images/sponsor_banner") addFileWatcher("Image", "html", "/images/fullscreen") addFileWatcher("Video", "html", "/videos") addFileWatcher("CustomHtml", "html", "/customhtml") printStartup(port) mux.Handle("/", http.FileServer(http.Dir(filepath.Join(statemanager.BaseFilePath(), "html")))) c := make(chan os.Signal, 1) go func() { err := http.ListenAndServe(fmt.Sprintf(":%d", port), setDefaultHeaders(mux)) if err != nil { log.Print(err) } c <- os.Kill }() mux.Handle("/version", http.HandlerFunc(versionHandler)) mux.Handle("/urls", http.HandlerFunc(urlsHandler)) signal.Notify(c, os.Interrupt, os.Kill) s := <-c log.Printf("Server received signal: %v. Shutting down", s) for _, saver := range savers { saver.Close() } }
// setup default settings func initSettings(saveFile string) *statemanager.Saver { defaults := []struct{ name, value string }{ {"BackgroundStyle", "bg_blacktowhite"}, {"BoxStyle", "box_flat"}, {"CurrentView", "scoreboard"}, {"HideJamTotals", "false"}, {"SwapTeams", "false"}, {"Image", "/images/fullscreen/American Flag.jpg"}, {"Video", "/videos/American Flag.webm"}, {"CustomHtml", "/customhtml/example"}, } views := []string{"View", "Preview"} statemanager.Lock() for _, v := range views { for _, d := range defaults { statemanager.StateUpdateString(fmt.Sprintf("Settings.%v.%v", v, d.name), d.value) } } statemanager.RegisterPatternUpdaterString("Settings", 0, setSettings) statemanager.Unlock() return statemanager.NewSaver(saveFile, "Settings", time.Duration(5)*time.Second, true, true) }
func (c *connection) Run() { defer c.Close() for { var cmd command err := c.conn.ReadJSON(&cmd) if err != nil { log.Print("Cannot read command: ", err) return } switch cmd.Action { case "Register": c.listener.RegisterPaths(cmd.Data) case "NewObject": u := uuid.NewV4().String() fields := make(map[string]string) for f, v := range cmd.FieldData { k := fmt.Sprintf("%v(%v).%v", cmd.Field, u, f) fields[k] = v } statemanager.Lock() statemanager.StateSetGroup(fields) statemanager.Unlock() default: // Try to send a command through the statemanager err := statemanager.Command(cmd.Action, cmd.Data) if err != nil { log.Print("Error processing command: ", err) } log.Printf("cmd: %+v returned error: %v", cmd, err) } } }