2013-09-26 21:20:26 +00:00
|
|
|
using MediaBrowser.Common.Constants;
|
|
|
|
using MediaBrowser.Common.Implementations.Logging;
|
|
|
|
using MediaBrowser.Common.Implementations.Updates;
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
using MediaBrowser.Server.Implementations;
|
|
|
|
using MediaBrowser.ServerApplication;
|
2013-09-27 17:04:35 +00:00
|
|
|
using MediaBrowser.ServerApplication.Native;
|
2013-09-26 21:20:26 +00:00
|
|
|
using Microsoft.Win32;
|
2013-09-24 21:06:21 +00:00
|
|
|
using System;
|
2013-09-26 21:20:26 +00:00
|
|
|
using System.Diagnostics;
|
|
|
|
using System.IO;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Windows;
|
2013-10-14 15:08:33 +00:00
|
|
|
using System.Net;
|
|
|
|
using System.Net.Security;
|
|
|
|
using System.Security.Cryptography.X509Certificates;
|
2013-09-26 21:20:26 +00:00
|
|
|
using System.Threading.Tasks;
|
2013-10-31 14:03:23 +00:00
|
|
|
using System.Reflection;
|
2014-01-23 04:12:28 +00:00
|
|
|
// MONOMKBUNDLE: For the embedded version, mkbundle tool
|
|
|
|
#if MONOMKBUNDLE
|
|
|
|
using Mono.Unix;
|
|
|
|
using Mono.Unix.Native;
|
|
|
|
using System.Text;
|
|
|
|
#endif
|
2013-09-24 21:06:21 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Mono
|
|
|
|
{
|
2013-09-26 21:20:26 +00:00
|
|
|
public class MainClass
|
2013-09-24 21:06:21 +00:00
|
|
|
{
|
2013-09-26 21:20:26 +00:00
|
|
|
private static ApplicationHost _appHost;
|
|
|
|
|
|
|
|
private static ILogger _logger;
|
|
|
|
|
2013-09-24 21:06:21 +00:00
|
|
|
public static void Main (string[] args)
|
|
|
|
{
|
2014-01-23 04:12:28 +00:00
|
|
|
//GetEntryAssembly is empty when running from a mkbundle package
|
|
|
|
#if MONOMKBUNDLE
|
|
|
|
var applicationPath = GetExecutablePath();
|
|
|
|
#else
|
2013-12-04 14:52:38 +00:00
|
|
|
var applicationPath = Assembly.GetEntryAssembly ().Location;
|
2014-01-23 04:12:28 +00:00
|
|
|
#endif
|
2013-12-04 14:52:38 +00:00
|
|
|
|
|
|
|
var appPaths = CreateApplicationPaths(applicationPath);
|
2013-09-26 21:20:26 +00:00
|
|
|
|
|
|
|
var logManager = new NlogManager(appPaths.LogDirectoryPath, "server");
|
|
|
|
logManager.ReloadLogger(LogSeverity.Info);
|
2014-01-09 04:44:51 +00:00
|
|
|
logManager.AddConsoleOutput();
|
2013-09-26 21:20:26 +00:00
|
|
|
|
|
|
|
var logger = _logger = logManager.GetLogger("Main");
|
|
|
|
|
|
|
|
BeginLog(logger);
|
|
|
|
|
|
|
|
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
|
|
|
|
|
|
|
|
if (PerformUpdateIfNeeded(appPaths, logger))
|
|
|
|
{
|
|
|
|
logger.Info("Exiting to perform application update.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
RunApplication(appPaths, logManager);
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
logger.Info("Shutting down");
|
|
|
|
|
|
|
|
_appHost.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-04 14:52:38 +00:00
|
|
|
private static ServerApplicationPaths CreateApplicationPaths(string applicationPath)
|
2013-09-26 21:20:26 +00:00
|
|
|
{
|
2013-12-04 14:52:38 +00:00
|
|
|
return new ServerApplicationPaths(applicationPath);
|
2013-09-26 21:20:26 +00:00
|
|
|
}
|
|
|
|
|
2013-10-07 16:22:19 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Determines whether this instance [can self restart].
|
|
|
|
/// </summary>
|
|
|
|
/// <returns><c>true</c> if this instance [can self restart]; otherwise, <c>false</c>.</returns>
|
|
|
|
public static bool CanSelfRestart
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a value indicating whether this instance can self update.
|
|
|
|
/// </summary>
|
|
|
|
/// <value><c>true</c> if this instance can self update; otherwise, <c>false</c>.</value>
|
|
|
|
public static bool CanSelfUpdate
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-14 15:08:33 +00:00
|
|
|
private static RemoteCertificateValidationCallback _ignoreCertificates = new RemoteCertificateValidationCallback(delegate { return true; });
|
|
|
|
|
2013-12-05 16:50:21 +00:00
|
|
|
private static TaskCompletionSource<bool> _applicationTaskCompletionSource = new TaskCompletionSource<bool>();
|
|
|
|
|
2013-09-26 21:20:26 +00:00
|
|
|
private static void RunApplication(ServerApplicationPaths appPaths, ILogManager logManager)
|
|
|
|
{
|
|
|
|
SystemEvents.SessionEnding += SystemEvents_SessionEnding;
|
|
|
|
|
2013-10-14 15:08:33 +00:00
|
|
|
// Allow all https requests
|
|
|
|
ServicePointManager.ServerCertificateValidationCallback = _ignoreCertificates;
|
|
|
|
|
2014-02-02 02:23:27 +00:00
|
|
|
_appHost = new ApplicationHost(appPaths, logManager, false);
|
2013-09-26 21:20:26 +00:00
|
|
|
|
2013-12-05 16:50:21 +00:00
|
|
|
Console.WriteLine ("appHost.Init");
|
|
|
|
|
2013-12-25 19:26:49 +00:00
|
|
|
var initProgress = new Progress<double>();
|
|
|
|
|
|
|
|
var task = _appHost.Init(initProgress);
|
2013-09-26 21:20:26 +00:00
|
|
|
Task.WaitAll (task);
|
|
|
|
|
2013-12-05 16:50:21 +00:00
|
|
|
Console.WriteLine ("Running startup tasks");
|
|
|
|
|
2013-09-26 21:20:26 +00:00
|
|
|
task = _appHost.RunStartupTasks();
|
|
|
|
Task.WaitAll (task);
|
|
|
|
|
2013-12-05 16:50:21 +00:00
|
|
|
task = _applicationTaskCompletionSource.Task;
|
2013-09-27 17:04:35 +00:00
|
|
|
|
2013-12-05 16:50:21 +00:00
|
|
|
Task.WaitAll (task);
|
2013-09-27 17:04:35 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 21:20:26 +00: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>
|
|
|
|
/// Begins the log.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="logger">The logger.</param>
|
|
|
|
private static void BeginLog(ILogger logger)
|
|
|
|
{
|
|
|
|
logger.Info("Media Browser Server started");
|
|
|
|
logger.Info("Command line: {0}", string.Join(" ", Environment.GetCommandLineArgs()));
|
|
|
|
|
|
|
|
logger.Info("Server: {0}", Environment.MachineName);
|
|
|
|
logger.Info("Operating system: {0}", Environment.OSVersion.ToString());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <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 21:22:25 +00:00
|
|
|
LogUnhandledException(exception);
|
2013-09-26 21:20:26 +00:00
|
|
|
|
|
|
|
if (!Debugger.IsAttached)
|
|
|
|
{
|
|
|
|
Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(exception));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-13 21:22:25 +00: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 16:50:21 +00:00
|
|
|
Console.WriteLine ("UnhandledException");
|
|
|
|
Console.WriteLine (builder.ToString());
|
|
|
|
|
2013-10-13 21:22:25 +00:00
|
|
|
File.WriteAllText(path, builder.ToString());
|
|
|
|
}
|
|
|
|
|
2013-09-26 21:20:26 +00: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()
|
|
|
|
{
|
2013-12-05 16:50:21 +00:00
|
|
|
_applicationTaskCompletionSource.SetResult (true);
|
2013-09-26 21:20:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void Restart()
|
|
|
|
{
|
2013-10-08 03:06:12 +00:00
|
|
|
// Second instance will start first, so dispose so that the http ports will be available to the new instance
|
2013-09-26 21:20:26 +00:00
|
|
|
_appHost.Dispose();
|
|
|
|
|
2013-10-08 03:06:12 +00:00
|
|
|
// Right now this method will just shutdown, but not restart
|
|
|
|
Shutdown ();
|
2013-09-26 21:20:26 +00:00
|
|
|
}
|
2014-01-23 04:12:28 +00:00
|
|
|
|
|
|
|
// Return the running process path
|
|
|
|
#if MONOMKBUNDLE
|
|
|
|
public static string GetExecutablePath()
|
|
|
|
{
|
|
|
|
var builder = new StringBuilder (8192);
|
|
|
|
if (Syscall.readlink("/proc/self/exe", builder) >= 0)
|
|
|
|
return builder.ToString ();
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-09-24 21:06:21 +00:00
|
|
|
}
|
2013-10-14 15:08:33 +00:00
|
|
|
|
|
|
|
class NoCheckCertificatePolicy : ICertificatePolicy
|
|
|
|
{
|
|
|
|
public bool CheckValidationResult (ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2013-09-24 21:06:21 +00:00
|
|
|
}
|