BaseMonoApp.cs 7.8 KB

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