BaseMonoApp.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 List<Assembly> GetAssembliesWithParts()
  64. {
  65. var list = new List<Assembly>();
  66. if (Environment.OperatingSystem == Startup.Common.OperatingSystem.Linux)
  67. {
  68. list.AddRange(GetLinuxAssemblies());
  69. }
  70. list.Add(GetType().Assembly);
  71. return list;
  72. }
  73. private IEnumerable<Assembly> GetLinuxAssemblies()
  74. {
  75. var list = new List<Assembly>();
  76. list.Add(typeof(LinuxIsoManager).Assembly);
  77. return list;
  78. }
  79. public void AuthorizeServer(int udpPort, int httpServerPort, int httpsPort, string applicationPath, string tempDirectory)
  80. {
  81. }
  82. private NativeEnvironment _nativeEnvironment;
  83. public NativeEnvironment Environment
  84. {
  85. get { return _nativeEnvironment ?? (_nativeEnvironment = GetEnvironmentInfo()); }
  86. }
  87. public bool SupportsRunningAsService
  88. {
  89. get
  90. {
  91. return false;
  92. }
  93. }
  94. public bool IsRunningAsService
  95. {
  96. get
  97. {
  98. return false;
  99. }
  100. }
  101. public bool SupportsLibraryMonitor
  102. {
  103. get
  104. {
  105. return Environment.OperatingSystem != Startup.Common.OperatingSystem.Osx;
  106. }
  107. }
  108. public void ConfigureAutoRun(bool autorun)
  109. {
  110. }
  111. public INetworkManager CreateNetworkManager(ILogger logger)
  112. {
  113. return new NetworkManager(logger);
  114. }
  115. private NativeEnvironment GetEnvironmentInfo()
  116. {
  117. var info = new NativeEnvironment
  118. {
  119. OperatingSystem = Startup.Common.OperatingSystem.Linux
  120. };
  121. var uname = GetUnixName();
  122. var sysName = uname.sysname ?? string.Empty;
  123. if (string.Equals(sysName, "Darwin", StringComparison.OrdinalIgnoreCase))
  124. {
  125. info.OperatingSystem = Startup.Common.OperatingSystem.Osx;
  126. }
  127. else if (string.Equals(sysName, "Linux", StringComparison.OrdinalIgnoreCase))
  128. {
  129. info.OperatingSystem = Startup.Common.OperatingSystem.Linux;
  130. }
  131. else if (string.Equals(sysName, "BSD", StringComparison.OrdinalIgnoreCase))
  132. {
  133. info.OperatingSystem = Startup.Common.OperatingSystem.Bsd;
  134. }
  135. var archX86 = new Regex("(i|I)[3-6]86");
  136. if (archX86.IsMatch(uname.machine))
  137. {
  138. info.SystemArchitecture = Architecture.X86;
  139. }
  140. else if (string.Equals(uname.machine, "x86_64", StringComparison.OrdinalIgnoreCase))
  141. {
  142. info.SystemArchitecture = Architecture.X64;
  143. }
  144. else if (uname.machine.StartsWith("arm", StringComparison.OrdinalIgnoreCase))
  145. {
  146. info.SystemArchitecture = Architecture.Arm;
  147. }
  148. else if (System.Environment.Is64BitOperatingSystem)
  149. {
  150. info.SystemArchitecture = Architecture.X64;
  151. }
  152. else
  153. {
  154. info.SystemArchitecture = Architecture.X86;
  155. }
  156. info.OperatingSystemVersionString = string.IsNullOrWhiteSpace(sysName) ?
  157. System.Environment.OSVersion.VersionString :
  158. sysName;
  159. return info;
  160. }
  161. private Uname _unixName;
  162. private Uname GetUnixName()
  163. {
  164. if (_unixName == null)
  165. {
  166. var uname = new Uname();
  167. try
  168. {
  169. Utsname utsname;
  170. var callResult = Syscall.uname(out utsname);
  171. if (callResult == 0)
  172. {
  173. uname.sysname = utsname.sysname ?? string.Empty;
  174. uname.machine = utsname.machine ?? string.Empty;
  175. }
  176. }
  177. catch (Exception ex)
  178. {
  179. Logger.ErrorException("Error getting unix name", ex);
  180. }
  181. _unixName = uname;
  182. }
  183. return _unixName;
  184. }
  185. public class Uname
  186. {
  187. public string sysname = string.Empty;
  188. public string machine = string.Empty;
  189. }
  190. public FFMpegInstallInfo GetFfmpegInstallInfo()
  191. {
  192. return GetInfo(Environment);
  193. }
  194. public void LaunchUrl(string url)
  195. {
  196. throw new NotImplementedException();
  197. }
  198. public IDbConnector GetDbConnector()
  199. {
  200. return new DbConnector(Logger);
  201. }
  202. public static FFMpegInstallInfo GetInfo(NativeEnvironment environment)
  203. {
  204. var info = new FFMpegInstallInfo();
  205. // Windows builds: http://ffmpeg.zeranoe.com/builds/
  206. // Linux builds: http://johnvansickle.com/ffmpeg/
  207. // OS X builds: http://ffmpegmac.net/
  208. // OS X x64: http://www.evermeet.cx/ffmpeg/
  209. switch (environment.OperatingSystem)
  210. {
  211. case OperatingSystem.Osx:
  212. case OperatingSystem.Bsd:
  213. break;
  214. case OperatingSystem.Linux:
  215. info.ArchiveType = "7z";
  216. info.Version = "20160215";
  217. break;
  218. }
  219. // No version available - user requirement
  220. info.DownloadUrls = new string[] { };
  221. return info;
  222. }
  223. public void EnableLoopback(string appName)
  224. {
  225. }
  226. }
  227. }