MonoApp.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.IsoMounter;
  3. using MediaBrowser.Model.Logging;
  4. using MediaBrowser.Server.Startup.Common;
  5. using Mono.Unix.Native;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Reflection;
  9. using System.Text.RegularExpressions;
  10. using MediaBrowser.Model.System;
  11. using MediaBrowser.Server.Implementations.Persistence;
  12. using MediaBrowser.Server.Startup.Common.FFMpeg;
  13. using MediaBrowser.Server.Startup.Common.Networking;
  14. using OperatingSystem = MediaBrowser.Server.Startup.Common.OperatingSystem;
  15. namespace MediaBrowser.Server.Mono.Native
  16. {
  17. public class MonoApp : INativeApp
  18. {
  19. protected StartupOptions StartupOptions { get; private set; }
  20. protected ILogger Logger { get; private set; }
  21. public MonoApp(StartupOptions startupOptions, ILogger logger)
  22. {
  23. StartupOptions = startupOptions;
  24. Logger = logger;
  25. }
  26. /// <summary>
  27. /// Shutdowns this instance.
  28. /// </summary>
  29. public void Shutdown()
  30. {
  31. MainClass.Shutdown();
  32. }
  33. /// <summary>
  34. /// Determines whether this instance [can self restart].
  35. /// </summary>
  36. /// <value><c>true</c> if this instance can self restart; otherwise, <c>false</c>.</value>
  37. public bool CanSelfRestart
  38. {
  39. get
  40. {
  41. // A restart script must be provided
  42. return StartupOptions.ContainsOption("-restartpath");
  43. }
  44. }
  45. /// <summary>
  46. /// Restarts this instance.
  47. /// </summary>
  48. public void Restart(StartupOptions startupOptions)
  49. {
  50. MainClass.Restart(startupOptions);
  51. }
  52. /// <summary>
  53. /// Gets a value indicating whether this instance can self update.
  54. /// </summary>
  55. /// <value><c>true</c> if this instance can self update; otherwise, <c>false</c>.</value>
  56. public bool CanSelfUpdate
  57. {
  58. get
  59. {
  60. return false;
  61. }
  62. }
  63. public bool SupportsAutoRunAtStartup
  64. {
  65. get { return false; }
  66. }
  67. public List<Assembly> GetAssembliesWithParts()
  68. {
  69. var list = new List<Assembly>();
  70. if (Environment.OperatingSystem == Startup.Common.OperatingSystem.Linux)
  71. {
  72. list.AddRange(GetLinuxAssemblies());
  73. }
  74. list.Add(GetType().Assembly);
  75. return list;
  76. }
  77. private IEnumerable<Assembly> GetLinuxAssemblies()
  78. {
  79. var list = new List<Assembly>();
  80. list.Add(typeof(LinuxIsoManager).Assembly);
  81. return list;
  82. }
  83. public void AuthorizeServer(int udpPort, int httpServerPort, int httpsPort, string applicationPath, string tempDirectory)
  84. {
  85. }
  86. private NativeEnvironment _nativeEnvironment;
  87. public NativeEnvironment Environment
  88. {
  89. get { return _nativeEnvironment ?? (_nativeEnvironment = GetEnvironmentInfo()); }
  90. }
  91. public bool SupportsRunningAsService
  92. {
  93. get
  94. {
  95. return false;
  96. }
  97. }
  98. public bool IsRunningAsService
  99. {
  100. get
  101. {
  102. return false;
  103. }
  104. }
  105. public bool SupportsLibraryMonitor
  106. {
  107. get
  108. {
  109. return Environment.OperatingSystem != Startup.Common.OperatingSystem.Osx;
  110. }
  111. }
  112. public void ConfigureAutoRun(bool autorun)
  113. {
  114. }
  115. public INetworkManager CreateNetworkManager(ILogger logger)
  116. {
  117. return new NetworkManager(logger);
  118. }
  119. private NativeEnvironment GetEnvironmentInfo()
  120. {
  121. var info = new NativeEnvironment
  122. {
  123. OperatingSystem = Startup.Common.OperatingSystem.Linux
  124. };
  125. var uname = GetUnixName();
  126. var sysName = uname.sysname ?? string.Empty;
  127. if (string.Equals(sysName, "Darwin", StringComparison.OrdinalIgnoreCase))
  128. {
  129. info.OperatingSystem = Startup.Common.OperatingSystem.Osx;
  130. }
  131. else if (string.Equals(sysName, "Linux", StringComparison.OrdinalIgnoreCase))
  132. {
  133. info.OperatingSystem = Startup.Common.OperatingSystem.Linux;
  134. }
  135. else if (string.Equals(sysName, "BSD", StringComparison.OrdinalIgnoreCase))
  136. {
  137. info.OperatingSystem = Startup.Common.OperatingSystem.Bsd;
  138. }
  139. var archX86 = new Regex("(i|I)[3-6]86");
  140. if (archX86.IsMatch(uname.machine))
  141. {
  142. info.SystemArchitecture = Architecture.X86;
  143. }
  144. else if (string.Equals(uname.machine, "x86_64", StringComparison.OrdinalIgnoreCase))
  145. {
  146. info.SystemArchitecture = Architecture.X64;
  147. }
  148. else if (uname.machine.StartsWith("arm", StringComparison.OrdinalIgnoreCase))
  149. {
  150. info.SystemArchitecture = Architecture.Arm;
  151. }
  152. else if (System.Environment.Is64BitOperatingSystem)
  153. {
  154. info.SystemArchitecture = Architecture.X64;
  155. }
  156. else
  157. {
  158. info.SystemArchitecture = Architecture.X86;
  159. }
  160. info.OperatingSystemVersionString = string.IsNullOrWhiteSpace(sysName) ?
  161. System.Environment.OSVersion.VersionString :
  162. sysName;
  163. return info;
  164. }
  165. private Uname _unixName;
  166. private Uname GetUnixName()
  167. {
  168. if (_unixName == null)
  169. {
  170. var uname = new Uname();
  171. try
  172. {
  173. Utsname utsname;
  174. var callResult = Syscall.uname(out utsname);
  175. if (callResult == 0)
  176. {
  177. uname.sysname = utsname.sysname ?? string.Empty;
  178. uname.machine = utsname.machine ?? string.Empty;
  179. }
  180. }
  181. catch (Exception ex)
  182. {
  183. Logger.ErrorException("Error getting unix name", ex);
  184. }
  185. _unixName = uname;
  186. }
  187. return _unixName;
  188. }
  189. public class Uname
  190. {
  191. public string sysname = string.Empty;
  192. public string machine = string.Empty;
  193. }
  194. public FFMpegInstallInfo GetFfmpegInstallInfo()
  195. {
  196. return GetInfo(Environment);
  197. }
  198. public void LaunchUrl(string url)
  199. {
  200. throw new NotImplementedException();
  201. }
  202. public IDbConnector GetDbConnector()
  203. {
  204. return new DbConnector(Logger);
  205. }
  206. public static FFMpegInstallInfo GetInfo(NativeEnvironment environment)
  207. {
  208. var info = new FFMpegInstallInfo();
  209. // Windows builds: http://ffmpeg.zeranoe.com/builds/
  210. // Linux builds: http://johnvansickle.com/ffmpeg/
  211. // OS X builds: http://ffmpegmac.net/
  212. // OS X x64: http://www.evermeet.cx/ffmpeg/
  213. switch (environment.OperatingSystem)
  214. {
  215. case OperatingSystem.Osx:
  216. case OperatingSystem.Bsd:
  217. break;
  218. case OperatingSystem.Linux:
  219. info.ArchiveType = "7z";
  220. info.Version = "20160215";
  221. break;
  222. }
  223. // No version available - user requirement
  224. info.DownloadUrls = new string[] { };
  225. return info;
  226. }
  227. public void EnableLoopback(string appName)
  228. {
  229. }
  230. }
  231. }