Main.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Diagnostics;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Security;
  8. using System.Reflection;
  9. using System.Runtime.InteropServices;
  10. using System.Security.Cryptography.X509Certificates;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using MediaBrowser.Common.Configuration;
  14. using MediaBrowser.Common.Implementations.IO;
  15. using MediaBrowser.Common.Implementations.Logging;
  16. using MediaBrowser.Model.Logging;
  17. using MediaBrowser.Server.Implementations;
  18. using MediaBrowser.Server.Startup.Common;
  19. using MediaBrowser.Server.Startup.Common.Browser;
  20. using Microsoft.Win32;
  21. using Microsoft.Win32;
  22. using MonoMac.AppKit;
  23. using MonoMac.Foundation;
  24. using MonoMac.ObjCRuntime;
  25. namespace MediaBrowser.Server.Mac
  26. {
  27. class MainClass
  28. {
  29. private static ApplicationHost _appHost;
  30. private static ILogger _logger;
  31. static void Main (string[] args)
  32. {
  33. var applicationPath = Assembly.GetEntryAssembly().Location;
  34. var options = new StartupOptions();
  35. // Allow this to be specified on the command line.
  36. var customProgramDataPath = options.GetOption("-programdata");
  37. var appPaths = CreateApplicationPaths(applicationPath, customProgramDataPath);
  38. var logManager = new NlogManager(appPaths.LogDirectoryPath, "server");
  39. logManager.ReloadLogger(LogSeverity.Info);
  40. logManager.AddConsoleOutput();
  41. var logger = _logger = logManager.GetLogger("Main");
  42. ApplicationHost.LogEnvironmentInfo(logger, appPaths, true);
  43. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  44. StartApplication(appPaths, logManager, options);
  45. NSApplication.Init ();
  46. NSApplication.Main (args);
  47. }
  48. public static void AddDependencies(AppController appController){
  49. appController.AppHost = _appHost;
  50. appController.Logger = _logger;
  51. appController.ConfigurationManager = _appHost.ServerConfigurationManager;
  52. appController.Localization = _appHost.LocalizationManager;
  53. }
  54. private static ServerApplicationPaths CreateApplicationPaths(string applicationPath, string programDataPath)
  55. {
  56. if (string.IsNullOrEmpty(programDataPath))
  57. {
  58. // TODO: Use CommonApplicationData? Will we always have write access?
  59. programDataPath = Path.Combine(Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), "mediabrowser-server");
  60. }
  61. // Within the mac bundle, go uo two levels then down into Resources folder
  62. var resourcesPath = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName (applicationPath)), "Resources");
  63. return new ServerApplicationPaths(programDataPath, applicationPath, resourcesPath);
  64. }
  65. /// <summary>
  66. /// Runs the application.
  67. /// </summary>
  68. /// <param name="appPaths">The app paths.</param>
  69. /// <param name="logManager">The log manager.</param>
  70. /// <param name="options">The options.</param>
  71. private static void StartApplication(ServerApplicationPaths appPaths,
  72. ILogManager logManager,
  73. StartupOptions options)
  74. {
  75. SystemEvents.SessionEnding += SystemEvents_SessionEnding;
  76. // Allow all https requests
  77. ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
  78. var fileSystem = new CommonFileSystem(logManager.GetLogger("FileSystem"), false, true);
  79. var nativeApp = new NativeApp();
  80. _appHost = new ApplicationHost(appPaths, logManager, options, fileSystem, "MBServer.Mono", false, nativeApp);
  81. if (options.ContainsOption("-v")) {
  82. Console.WriteLine (_appHost.ApplicationVersion.ToString());
  83. return;
  84. }
  85. Console.WriteLine ("appHost.Init");
  86. var initProgress = new Progress<double>();
  87. var task = _appHost.Init(initProgress);
  88. Task.WaitAll(task);
  89. Task.Run (() => _appHost.RunStartupTasks());
  90. }
  91. /// <summary>
  92. /// Handles the SessionEnding event of the SystemEvents control.
  93. /// </summary>
  94. /// <param name="sender">The source of the event.</param>
  95. /// <param name="e">The <see cref="SessionEndingEventArgs"/> instance containing the event data.</param>
  96. static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
  97. {
  98. if (e.Reason == SessionEndReasons.SystemShutdown)
  99. {
  100. Shutdown();
  101. }
  102. }
  103. public static void Shutdown()
  104. {
  105. ShutdownApp();
  106. }
  107. private static void ShutdownApp()
  108. {
  109. _logger.Info ("Calling ApplicationHost.Dispose");
  110. _appHost.Dispose ();
  111. _logger.Info("AppController.Terminate");
  112. AppController.Instance.Terminate ();
  113. }
  114. /// <summary>
  115. /// Handles the UnhandledException event of the CurrentDomain control.
  116. /// </summary>
  117. /// <param name="sender">The source of the event.</param>
  118. /// <param name="e">The <see cref="UnhandledExceptionEventArgs"/> instance containing the event data.</param>
  119. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  120. {
  121. var exception = (Exception)e.ExceptionObject;
  122. new UnhandledExceptionWriter(_appHost.ServerConfigurationManager.ApplicationPaths, _logger, _appHost.LogManager).Log(exception);
  123. if (!Debugger.IsAttached)
  124. {
  125. Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(exception));
  126. }
  127. }
  128. }
  129. class NoCheckCertificatePolicy : ICertificatePolicy
  130. {
  131. public bool CheckValidationResult (ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
  132. {
  133. return true;
  134. }
  135. }
  136. }