2014-10-07 01:58:46 +02:00
|
|
|
using MediaBrowser.Common.Implementations.IO;
|
2013-09-26 23:20:26 +02:00
|
|
|
using MediaBrowser.Common.Implementations.Logging;
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
using MediaBrowser.Server.Implementations;
|
2014-11-09 19:24:57 +01:00
|
|
|
using MediaBrowser.Server.Mono.Native;
|
|
|
|
using MediaBrowser.Server.Startup.Common;
|
2013-09-26 23:20:26 +02:00
|
|
|
using Microsoft.Win32;
|
2013-09-24 23:06:21 +02:00
|
|
|
using System;
|
2013-09-26 23:20:26 +02:00
|
|
|
using System.Diagnostics;
|
|
|
|
using System.IO;
|
2013-10-14 17:08:33 +02:00
|
|
|
using System.Net;
|
|
|
|
using System.Net.Security;
|
2014-10-07 01:58:46 +02:00
|
|
|
using System.Reflection;
|
2013-10-14 17:08:33 +02:00
|
|
|
using System.Security.Cryptography.X509Certificates;
|
2013-09-26 23:20:26 +02:00
|
|
|
using System.Threading.Tasks;
|
2013-09-24 23:06:21 +02:00
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Mono
|
|
|
|
{
|
2013-09-26 23:20:26 +02:00
|
|
|
public class MainClass
|
2013-09-24 23:06:21 +02:00
|
|
|
{
|
2013-09-26 23:20:26 +02:00
|
|
|
private static ApplicationHost _appHost;
|
|
|
|
|
|
|
|
private static ILogger _logger;
|
|
|
|
|
2013-09-24 23:06:21 +02:00
|
|
|
public static void Main (string[] args)
|
|
|
|
{
|
2014-11-14 07:27:10 +01:00
|
|
|
var applicationPath = Assembly.GetEntryAssembly().Location;
|
2014-04-10 17:06:54 +02:00
|
|
|
|
2014-09-14 17:26:33 +02:00
|
|
|
var options = new StartupOptions();
|
|
|
|
|
2014-04-10 17:06:54 +02:00
|
|
|
// Allow this to be specified on the command line.
|
2014-09-14 17:26:33 +02:00
|
|
|
var customProgramDataPath = options.GetOption("-programdata");
|
2013-12-04 15:52:38 +01:00
|
|
|
|
2014-04-10 17:06:54 +02:00
|
|
|
var appPaths = CreateApplicationPaths(applicationPath, customProgramDataPath);
|
2013-09-26 23:20:26 +02:00
|
|
|
|
|
|
|
var logManager = new NlogManager(appPaths.LogDirectoryPath, "server");
|
|
|
|
logManager.ReloadLogger(LogSeverity.Info);
|
2014-01-09 05:44:51 +01:00
|
|
|
logManager.AddConsoleOutput();
|
2013-09-26 23:20:26 +02:00
|
|
|
|
|
|
|
var logger = _logger = logManager.GetLogger("Main");
|
|
|
|
|
2014-11-14 07:27:10 +01:00
|
|
|
ApplicationHost.LogEnvironmentInfo(logger, appPaths, true);
|
2013-09-26 23:20:26 +02:00
|
|
|
|
|
|
|
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
|
|
|
|
|
|
|
|
if (PerformUpdateIfNeeded(appPaths, logger))
|
|
|
|
{
|
|
|
|
logger.Info("Exiting to perform application update.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2014-09-14 17:27:22 +02:00
|
|
|
RunApplication(appPaths, logManager, options);
|
2013-09-26 23:20:26 +02:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
logger.Info("Shutting down");
|
|
|
|
|
|
|
|
_appHost.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-10 17:06:54 +02:00
|
|
|
private static ServerApplicationPaths CreateApplicationPaths(string applicationPath, string programDataPath)
|
2013-09-26 23:20:26 +02:00
|
|
|
{
|
2014-04-10 17:06:54 +02:00
|
|
|
if (string.IsNullOrEmpty(programDataPath))
|
|
|
|
{
|
|
|
|
return new ServerApplicationPaths(applicationPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ServerApplicationPaths(programDataPath, applicationPath);
|
2013-09-26 23:20:26 +02:00
|
|
|
}
|
|
|
|
|
2014-11-14 07:27:10 +01:00
|
|
|
private static readonly RemoteCertificateValidationCallback IgnoreCertificates = new RemoteCertificateValidationCallback(delegate { return true; });
|
2013-10-07 18:22:19 +02:00
|
|
|
|
2014-11-14 07:27:10 +01:00
|
|
|
private static readonly TaskCompletionSource<bool> ApplicationTaskCompletionSource = new TaskCompletionSource<bool>();
|
2013-12-05 17:50:21 +01:00
|
|
|
|
2014-09-14 17:27:22 +02:00
|
|
|
private static void RunApplication(ServerApplicationPaths appPaths, ILogManager logManager, StartupOptions options)
|
2013-09-26 23:20:26 +02:00
|
|
|
{
|
|
|
|
SystemEvents.SessionEnding += SystemEvents_SessionEnding;
|
|
|
|
|
2013-10-14 17:08:33 +02:00
|
|
|
// Allow all https requests
|
2014-11-14 07:27:10 +01:00
|
|
|
ServicePointManager.ServerCertificateValidationCallback = IgnoreCertificates;
|
2013-10-14 17:08:33 +02:00
|
|
|
|
2014-10-07 01:58:46 +02:00
|
|
|
var fileSystem = new CommonFileSystem(logManager.GetLogger("FileSystem"), false, true);
|
|
|
|
|
2014-11-09 19:24:57 +01:00
|
|
|
var nativeApp = new NativeApp();
|
|
|
|
|
|
|
|
_appHost = new ApplicationHost(appPaths, logManager, options, fileSystem, "MBServer.Mono", false, nativeApp);
|
2014-10-02 02:28:16 +02:00
|
|
|
|
|
|
|
if (options.ContainsOption("-v")) {
|
|
|
|
Console.WriteLine (_appHost.ApplicationVersion.ToString());
|
|
|
|
return;
|
|
|
|
}
|
2013-09-26 23:20:26 +02:00
|
|
|
|
2013-12-05 17:50:21 +01:00
|
|
|
Console.WriteLine ("appHost.Init");
|
|
|
|
|
2013-12-25 20:26:49 +01:00
|
|
|
var initProgress = new Progress<double>();
|
|
|
|
|
|
|
|
var task = _appHost.Init(initProgress);
|
2013-09-26 23:20:26 +02:00
|
|
|
Task.WaitAll (task);
|
|
|
|
|
2013-12-05 17:50:21 +01:00
|
|
|
Console.WriteLine ("Running startup tasks");
|
|
|
|
|
2013-09-26 23:20:26 +02:00
|
|
|
task = _appHost.RunStartupTasks();
|
|
|
|
Task.WaitAll (task);
|
|
|
|
|
2014-11-14 07:27:10 +01:00
|
|
|
task = ApplicationTaskCompletionSource.Task;
|
2013-09-27 19:04:35 +02:00
|
|
|
|
2013-12-05 17:50:21 +01:00
|
|
|
Task.WaitAll (task);
|
2013-09-27 19:04:35 +02:00
|
|
|
}
|
|
|
|
|
2013-09-26 23:20:26 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Handles the SessionEnding event of the SystemEvents control.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sender">The source of the event.</param>
|
|
|
|
/// <param name="e">The <see cref="SessionEndingEventArgs"/> instance containing the event data.</param>
|
|
|
|
static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
|
|
|
|
{
|
|
|
|
if (e.Reason == SessionEndReasons.SystemShutdown)
|
|
|
|
{
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Handles the UnhandledException event of the CurrentDomain control.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sender">The source of the event.</param>
|
|
|
|
/// <param name="e">The <see cref="UnhandledExceptionEventArgs"/> instance containing the event data.</param>
|
|
|
|
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
|
|
|
|
{
|
|
|
|
var exception = (Exception)e.ExceptionObject;
|
|
|
|
|
2013-10-13 23:22:25 +02:00
|
|
|
LogUnhandledException(exception);
|
2013-09-26 23:20:26 +02:00
|
|
|
|
|
|
|
if (!Debugger.IsAttached)
|
|
|
|
{
|
|
|
|
Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(exception));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-13 23:22:25 +02:00
|
|
|
private static void LogUnhandledException(Exception ex)
|
|
|
|
{
|
|
|
|
_logger.ErrorException("UnhandledException", ex);
|
|
|
|
|
|
|
|
_appHost.LogManager.Flush ();
|
|
|
|
|
|
|
|
var path = Path.Combine(_appHost.ServerConfigurationManager.ApplicationPaths.LogDirectoryPath, "crash_" + Guid.NewGuid() + ".txt");
|
|
|
|
|
|
|
|
var builder = LogHelper.GetLogMessage(ex);
|
|
|
|
|
2013-12-05 17:50:21 +01:00
|
|
|
Console.WriteLine ("UnhandledException");
|
|
|
|
Console.WriteLine (builder.ToString());
|
|
|
|
|
2013-10-13 23:22:25 +02:00
|
|
|
File.WriteAllText(path, builder.ToString());
|
|
|
|
}
|
|
|
|
|
2013-09-26 23:20:26 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Performs the update if needed.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="appPaths">The app paths.</param>
|
|
|
|
/// <param name="logger">The logger.</param>
|
|
|
|
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
|
|
|
|
private static bool PerformUpdateIfNeeded(ServerApplicationPaths appPaths, ILogger logger)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Shutdown()
|
|
|
|
{
|
2014-11-14 07:27:10 +01:00
|
|
|
ApplicationTaskCompletionSource.SetResult (true);
|
2013-09-26 23:20:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void Restart()
|
|
|
|
{
|
2013-10-08 05:06:12 +02:00
|
|
|
// Second instance will start first, so dispose so that the http ports will be available to the new instance
|
2013-09-26 23:20:26 +02:00
|
|
|
_appHost.Dispose();
|
|
|
|
|
2013-10-08 05:06:12 +02:00
|
|
|
// Right now this method will just shutdown, but not restart
|
|
|
|
Shutdown ();
|
2013-09-26 23:20:26 +02:00
|
|
|
}
|
2013-09-24 23:06:21 +02:00
|
|
|
}
|
2013-10-14 17:08:33 +02:00
|
|
|
|
|
|
|
class NoCheckCertificatePolicy : ICertificatePolicy
|
|
|
|
{
|
|
|
|
public bool CheckValidationResult (ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2013-09-24 23:06:21 +02:00
|
|
|
}
|