Program.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using MediaBrowser.Common.Constants;
  2. using MediaBrowser.Common.Implementations.Logging;
  3. using MediaBrowser.Common.Implementations.Updates;
  4. using MediaBrowser.Model.Logging;
  5. using MediaBrowser.Server.Implementations;
  6. using MediaBrowser.ServerApplication;
  7. using MediaBrowser.ServerApplication.Native;
  8. using Microsoft.Win32;
  9. using System;
  10. using System.Diagnostics;
  11. using System.IO;
  12. using System.Threading;
  13. using System.Windows;
  14. using System.Net;
  15. using System.Net.Security;
  16. using System.Security.Cryptography.X509Certificates;
  17. using System.Threading.Tasks;
  18. using System.Reflection;
  19. namespace MediaBrowser.Server.Mono
  20. {
  21. public class MainClass
  22. {
  23. private static ApplicationHost _appHost;
  24. private static ILogger _logger;
  25. public static void Main (string[] args)
  26. {
  27. var applicationPath = Assembly.GetEntryAssembly ().Location;
  28. var appPaths = CreateApplicationPaths(applicationPath);
  29. var logManager = new NlogManager(appPaths.LogDirectoryPath, "server");
  30. logManager.ReloadLogger(LogSeverity.Info);
  31. logManager.AddConsoleOutput();
  32. var logger = _logger = logManager.GetLogger("Main");
  33. BeginLog(logger);
  34. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  35. if (PerformUpdateIfNeeded(appPaths, logger))
  36. {
  37. logger.Info("Exiting to perform application update.");
  38. return;
  39. }
  40. try
  41. {
  42. RunApplication(appPaths, logManager);
  43. }
  44. finally
  45. {
  46. logger.Info("Shutting down");
  47. _appHost.Dispose();
  48. }
  49. }
  50. private static ServerApplicationPaths CreateApplicationPaths(string applicationPath)
  51. {
  52. return new ServerApplicationPaths(applicationPath);
  53. }
  54. /// <summary>
  55. /// Determines whether this instance [can self restart].
  56. /// </summary>
  57. /// <returns><c>true</c> if this instance [can self restart]; otherwise, <c>false</c>.</returns>
  58. public static bool CanSelfRestart
  59. {
  60. get
  61. {
  62. return false;
  63. }
  64. }
  65. /// <summary>
  66. /// Gets a value indicating whether this instance can self update.
  67. /// </summary>
  68. /// <value><c>true</c> if this instance can self update; otherwise, <c>false</c>.</value>
  69. public static bool CanSelfUpdate
  70. {
  71. get
  72. {
  73. return false;
  74. }
  75. }
  76. private static RemoteCertificateValidationCallback _ignoreCertificates = new RemoteCertificateValidationCallback(delegate { return true; });
  77. private static TaskCompletionSource<bool> _applicationTaskCompletionSource = new TaskCompletionSource<bool>();
  78. private static void RunApplication(ServerApplicationPaths appPaths, ILogManager logManager)
  79. {
  80. SystemEvents.SessionEnding += SystemEvents_SessionEnding;
  81. // Allow all https requests
  82. ServicePointManager.ServerCertificateValidationCallback = _ignoreCertificates;
  83. _appHost = new ApplicationHost(appPaths, logManager);
  84. Console.WriteLine ("appHost.Init");
  85. var initProgress = new Progress<double>();
  86. var task = _appHost.Init(initProgress);
  87. Task.WaitAll (task);
  88. Console.WriteLine ("Running startup tasks");
  89. task = _appHost.RunStartupTasks();
  90. Task.WaitAll (task);
  91. task = _applicationTaskCompletionSource.Task;
  92. Task.WaitAll (task);
  93. }
  94. /// <summary>
  95. /// Handles the SessionEnding event of the SystemEvents control.
  96. /// </summary>
  97. /// <param name="sender">The source of the event.</param>
  98. /// <param name="e">The <see cref="SessionEndingEventArgs"/> instance containing the event data.</param>
  99. static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
  100. {
  101. if (e.Reason == SessionEndReasons.SystemShutdown)
  102. {
  103. Shutdown();
  104. }
  105. }
  106. /// <summary>
  107. /// Begins the log.
  108. /// </summary>
  109. /// <param name="logger">The logger.</param>
  110. private static void BeginLog(ILogger logger)
  111. {
  112. logger.Info("Media Browser Server started");
  113. logger.Info("Command line: {0}", string.Join(" ", Environment.GetCommandLineArgs()));
  114. logger.Info("Server: {0}", Environment.MachineName);
  115. logger.Info("Operating system: {0}", Environment.OSVersion.ToString());
  116. }
  117. /// <summary>
  118. /// Handles the UnhandledException event of the CurrentDomain control.
  119. /// </summary>
  120. /// <param name="sender">The source of the event.</param>
  121. /// <param name="e">The <see cref="UnhandledExceptionEventArgs"/> instance containing the event data.</param>
  122. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  123. {
  124. var exception = (Exception)e.ExceptionObject;
  125. LogUnhandledException(exception);
  126. if (!Debugger.IsAttached)
  127. {
  128. Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(exception));
  129. }
  130. }
  131. private static void LogUnhandledException(Exception ex)
  132. {
  133. _logger.ErrorException("UnhandledException", ex);
  134. _appHost.LogManager.Flush ();
  135. var path = Path.Combine(_appHost.ServerConfigurationManager.ApplicationPaths.LogDirectoryPath, "crash_" + Guid.NewGuid() + ".txt");
  136. var builder = LogHelper.GetLogMessage(ex);
  137. Console.WriteLine ("UnhandledException");
  138. Console.WriteLine (builder.ToString());
  139. File.WriteAllText(path, builder.ToString());
  140. }
  141. /// <summary>
  142. /// Performs the update if needed.
  143. /// </summary>
  144. /// <param name="appPaths">The app paths.</param>
  145. /// <param name="logger">The logger.</param>
  146. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  147. private static bool PerformUpdateIfNeeded(ServerApplicationPaths appPaths, ILogger logger)
  148. {
  149. return false;
  150. }
  151. public static void Shutdown()
  152. {
  153. _applicationTaskCompletionSource.SetResult (true);
  154. }
  155. public static void Restart()
  156. {
  157. // Second instance will start first, so dispose so that the http ports will be available to the new instance
  158. _appHost.Dispose();
  159. // Right now this method will just shutdown, but not restart
  160. Shutdown ();
  161. }
  162. }
  163. class NoCheckCertificatePolicy : ICertificatePolicy
  164. {
  165. public bool CheckValidationResult (ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
  166. {
  167. return true;
  168. }
  169. }
  170. }