void Worker() { try { FanArtTV_CrawlArtists(); if (m_Exit) { return; } MusicBrainz_CrawlAlbums(); if (m_Exit) { return; } Cleanup(); mdb.Save(); } finally { progressFanArtTV = 0; progressMusicBrainz = 0; progressCleanup = 0; } }
/// <summary>Selects the next file.</summary> /// <param name="streamID">The stream identifier.</param> /// <returns>Returns true on success, false otherwise</returns> public MDBFileSelection SelectNextFile(long streamID) { var config = mdb.GetStreamSettings(streamID); MDBSubset subset = mdb.Subsets.TryGetStruct(config.SubsetID); if (subset.ID == 0) { subset.Name = "Undefined"; } int seqNumber = mdb.Subsets.SequenceNumber ^ mdb.SubsetFilters.SequenceNumber ^ mdb.AudioFiles.SequenceNumber; if (audioFileIDs == null || seqNumber != sequenceNumber) { audioFileIDs = mdb.GetSubsetAudioFileIDs(subset.ID, config.MinimumLength, config.MaximumLength); if (subset.TitleCount != audioFileIDs.Count) { subset.TitleCount = audioFileIDs.Count; if (subset.ID > 0) { mdb.Subsets.Update(subset); } } sequenceNumber = seqNumber; this.LogInfo("Reloaded subset {0} at player", subset); } if (subset.ID > 0 && subset.TitleCount != audioFileIDs.Count) { subset.TitleCount = audioFileIDs.Count; try { mdb.Subsets.Update(subset); this.LogInfo("Subset {0} title count updated!", subset); } catch { } } if (audioFileIDs.Count == 0) { this.LogDebug("No subset defined or subset result empty for stream <red>{0}<default> selecting random titles.", streamID); audioFileIDs = mdb.AudioFiles.IDs; } if (audioFileIDs.Count == 0) { this.LogDebug("No audio files found!"); Selection = null; return(null); } var listSearch = Search.FieldEquals(nameof(MDBPlayListItem.StreamID), streamID); while (true) { Func <long> getCount = () => mdb.PlayListItems.Count(listSearch); //fill playlist for (int n = 0; getCount() < config.MinimumTitleCount; n++) { //max 4 tries per slot if (n > config.MinimumTitleCount * 4) { break; } int i = (int)((rnd.Next() * (long)rnd.Next()) % audioFileIDs.Count); long nextID = audioFileIDs[i]; //audiofile valid ? MDBAudioFile audioFile; if (!mdb.AudioFiles.TryGetStruct(nextID, out audioFile)) { continue; } //file does not exist if (!File.Exists(mdb.Files.TryGetStruct(audioFile.FileID).GetFullPath(mdb))) { mdb.AudioFiles.TryDelete(audioFile.FileID); mdb.Files.TryDelete(audioFile.FileID); this.LogError("AudioFile <red>{0}<default> removed (inaccessible).", audioFile); continue; } //yes, playlist contains id already ? if (mdb.PlayListItems.Exist(listSearch & Search.FieldEquals(nameof(MDBPlayListItem.AudioFileID), nextID))) { continue; } //no add mdb.PlayListItems.Insert(new MDBPlayListItem() { AudioFileID = audioFile.FileID, StreamID = streamID, SubsetID = subset.ID, Added = DateTime.UtcNow.AddTicks(DefaultRNG.Int8), }); this.LogInfo("Added audio file {0} from subset {1} to {2} playlist.", audioFile, subset, streamID); } mdb.Save(); //get current entry try { var items = mdb.PlayListItems.GetStructs( listSearch & Search.FieldGreater(nameof(MDBPlayListItem.OwnerID), 0), ResultOption.SortAscending(nameof(MDBPlayListItem.Added)) + ResultOption.Limit(1)); if (items.Count == 0) { items = mdb.PlayListItems.GetStructs( listSearch & Search.FieldEquals(nameof(MDBPlayListItem.OwnerID), 0), ResultOption.SortAscending(nameof(MDBPlayListItem.Added)) + ResultOption.Limit(1)); } var item = items.FirstOrDefault(); if (item.ID == 0) { continue; } try { var result = Selection = MDBFileSelection.Load(mdb, item); return(result); } finally { //always remove playlistitem (even on errors) mdb.PlayListItems.TryDelete(item.ID); } } catch (Exception ex) { this.LogError(ex, "Cannot start stream {0}!", streamID, ex.Message); Selection = null; return(null); } } }
protected override void Worker() { if (LogConsole != null) { LogConsole.ExceptionMode = LogExceptionMode.Full; LogConsole.Flags |= LogConsoleFlags.DisplayTimeStamp; LogConsole.Mode = LogReceiverMode.Opportune; } Logger.DebugReceiver?.Close(); //init the async mysql connection class we want to use. //new MySql.Data.MySqlClient.MySqlConnection().Dispose(); WebServer webServer = null; FtpServer ftpServer = null; MusicDataBase mdb = null; MDBBroadcaster broadcaster = null; DesktopPlayer player = null; try { var rootFolder = FileSystem.ProgramDirectory; webServer = new WebServer(); mdb = new MusicDataBase(rootFolder); if (LogSystem != null) { //allow user to override loglevel by commandline if (LogSystem.Level == LogLevel.Information) { LogSystem.Level = mdb.Config.ReadEnum <LogLevel>("MusicDataBase", "LogLevel", LogLevel.Information); } } this.LogInfo("Loading Database..."); mdb.Load(); MemoryStorage.Default.LogVerboseMessages = LogSystem?.Level == LogLevel.Verbose; if (null != mdb.Database) { mdb.Database.Storage.LogVerboseMessages = MemoryStorage.Default.LogVerboseMessages; } if (mdb.Config.ReadBool("MusicDataBase", "ClearDatabase", false)) { foreach (ITable t in mdb.Tables) { t.Clear(); } } mdb.Save(); this.LogInfo("Loading auth tables..."); if (mdb.Database != null) { webServer.AuthTables.Connect(TableConnectorMode.Direct, mdb.Database); } else { try { webServer.AuthTables.Load(mdb.DataBasePath); } catch (Exception ex) { this.LogWarning(ex, "Load auth tables failed. Recreating..."); } } authTables = webServer.AuthTables; var authInterface = new AuthInterface <MDBUserLevel>(webServer); { //auth interface User admin = webServer.AuthTables.CheckAdminPresent(); if (mdb.Config.ReadBool("MusicDataBase", "LocalhostIsAdmin", false)) { authInterface.DefaultLocalhostUser = admin; } } this.LogInfo("Initializing FtpServer..."); ftpServer = new FtpServer(); ftpServer.CheckLogin += this.FtpServerCheckLogin; //add music dirs to ftp server SetFtpMusicFolders(mdb, ftpServer); int ftpPort = mdb.Config.ReadInt32("FtpServer", "Port", 8021); ftpServer.Listen(ftpPort); player = new DesktopPlayer(mdb, (long)MDBStreamType.JukeBob); player.Start(); this.LogInfo("Initializing WebServer..."); webServer.SessionMode = WebServerSessionMode.Cookie; webServer.SessionTimeout = TimeSpan.FromDays(1); webServer.PerformanceChecks = mdb.Config.ReadBool("WebServer", "PerformanceChecks", false); webServer.EnableExplain = mdb.Config.ReadBool("WebServer", "Explain", false); webServer.TransmitLayout = false; webServer.EnableTemplates = true; webServer.CheckAccess += WebServerCheckAccess; webServer.StaticFilesPath = mdb.WebFolder; //prepare rpc var avatarFolder = mdb.GetFolderConfig("MusicDataBase", "AvatarFolder", "avatar"); webServer.Register(new AvatarInterface(avatarFolder)); webServer.Register(authInterface); var webInterface = new WebInterface(mdb, webServer.AuthTables, player); webInterface.LogCollector.Level = LogLevel.Debug; webInterface.LogCollector.MaximumItemCount = 1000; webInterface.LogCollector.ExceptionMode = LogExceptionMode.Full; webServer.Register(webInterface); webServer.Register(new HostInterface(this)); //start server int webPort = mdb.Config.ReadInt32("WebServer", "Port", 8080); webServer.Listen(webPort); mdb.SetHostConfiguration(Environment.UserName + "." + Environment.MachineName, webPort, ftpPort, mdb.GetLocalAddresses()); //using (Streamer streamer = new Streamer(mdb, MDBStreamType.JukeBox)) broadcaster = new MDBBroadcaster(mdb); broadcaster.Start(); if (mdb.Config.ReadBool("Crawler", "RunAtStartup", false)) { webInterface.FileCrawler.Start(); } //main loop this.LogNotice("Entering main loop..."); while (!ServiceParameters.Shutdown) { ServiceParameters.WaitForShutdown(1000); //SetConsoleTitle(); } } finally { //cleanup broadcaster?.Dispose(); ftpServer?.Close(); player?.Stop(); player?.Dispose(); authTables?.Save(); authTables = null; mdb?.Save(); webServer?.Close(); } this.LogInfo("Shutdown completed."); Logger.Flush(); }