NativeApp.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. namespace MediaBrowser.Server.Mono.Native
  12. {
  13. /// <summary>
  14. /// Class NativeApp
  15. /// </summary>
  16. public class NativeApp : INativeApp
  17. {
  18. /// <summary>
  19. /// Shutdowns this instance.
  20. /// </summary>
  21. public void Shutdown()
  22. {
  23. MainClass.Shutdown();
  24. }
  25. /// <summary>
  26. /// Restarts this instance.
  27. /// </summary>
  28. public void Restart()
  29. {
  30. }
  31. /// <summary>
  32. /// Determines whether this instance [can self restart].
  33. /// </summary>
  34. /// <returns><c>true</c> if this instance [can self restart]; otherwise, <c>false</c>.</returns>
  35. public bool CanSelfRestart
  36. {
  37. get
  38. {
  39. return false;
  40. }
  41. }
  42. /// <summary>
  43. /// Gets a value indicating whether this instance can self update.
  44. /// </summary>
  45. /// <value><c>true</c> if this instance can self update; otherwise, <c>false</c>.</value>
  46. public bool CanSelfUpdate
  47. {
  48. get
  49. {
  50. return false;
  51. }
  52. }
  53. public bool SupportsAutoRunAtStartup
  54. {
  55. get { return false; }
  56. }
  57. public void PreventSystemStandby()
  58. {
  59. }
  60. public List<Assembly> GetAssembliesWithParts()
  61. {
  62. var list = new List<Assembly>();
  63. if (Environment.OperatingSystem == Startup.Common.OperatingSystem.Linux)
  64. {
  65. list.AddRange(GetLinuxAssemblies());
  66. }
  67. list.Add(GetType().Assembly);
  68. return list;
  69. }
  70. private List<Assembly> GetLinuxAssemblies()
  71. {
  72. var list = new List<Assembly>();
  73. list.Add(typeof(LinuxIsoManager).Assembly);
  74. return list;
  75. }
  76. public void AuthorizeServer(int httpServerPort, string httpServerUrlPrefix, int udpPort, string tempDirectory)
  77. {
  78. }
  79. private NativeEnvironment _nativeEnvironment;
  80. public NativeEnvironment Environment
  81. {
  82. get { return _nativeEnvironment ?? (_nativeEnvironment = GetEnvironmentInfo()); }
  83. }
  84. public bool SupportsRunningAsService
  85. {
  86. get
  87. {
  88. return false;
  89. }
  90. }
  91. public bool IsRunningAsService
  92. {
  93. get
  94. {
  95. return false;
  96. }
  97. }
  98. public void ConfigureAutoRun(bool autorun)
  99. {
  100. }
  101. public INetworkManager CreateNetworkManager(ILogger logger)
  102. {
  103. return new NetworkManager(logger);
  104. }
  105. private NativeEnvironment GetEnvironmentInfo()
  106. {
  107. var info = new NativeEnvironment
  108. {
  109. OperatingSystem = Startup.Common.OperatingSystem.Linux
  110. };
  111. var uname = GetUnixName();
  112. var sysName = uname.sysname ?? string.Empty;
  113. if (string.Equals(sysName, "Darwin", StringComparison.OrdinalIgnoreCase))
  114. {
  115. info.OperatingSystem = Startup.Common.OperatingSystem.Osx;
  116. }
  117. else if (string.Equals(sysName, "Linux", StringComparison.OrdinalIgnoreCase))
  118. {
  119. info.OperatingSystem = Startup.Common.OperatingSystem.Linux;
  120. }
  121. else if (string.Equals(sysName, "BSD", StringComparison.OrdinalIgnoreCase))
  122. {
  123. // TODO: How to detect BSD?
  124. info.OperatingSystem = Startup.Common.OperatingSystem.Bsd;
  125. }
  126. var archX86 = new Regex("(i|I)[3-6]86");
  127. if (archX86.IsMatch(uname.machine))
  128. {
  129. info.SystemArchitecture = Architecture.X86;
  130. }
  131. else if (string.Equals(uname.machine, "x86_64", StringComparison.OrdinalIgnoreCase))
  132. {
  133. info.SystemArchitecture = Architecture.X86_X64;
  134. }
  135. else if (uname.machine.StartsWith("arm", StringComparison.OrdinalIgnoreCase))
  136. {
  137. info.SystemArchitecture = Architecture.Arm;
  138. }
  139. return info;
  140. }
  141. private static Uname GetUnixName()
  142. {
  143. var uname = new Uname();
  144. Utsname utsname;
  145. var callResult = Syscall.uname(out utsname);
  146. if (callResult == 0)
  147. {
  148. uname.sysname = utsname.sysname;
  149. uname.machine = utsname.machine;
  150. }
  151. return uname;
  152. }
  153. public class Uname
  154. {
  155. public string sysname = string.Empty;
  156. public string machine = string.Empty;
  157. }
  158. }
  159. }