BaseMonoApp.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Model.Logging;
  3. using MediaBrowser.Server.Startup.Common;
  4. using Mono.Unix.Native;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Reflection;
  8. using System.Text.RegularExpressions;
  9. namespace MediaBrowser.Server.Mac
  10. {
  11. public abstract class BaseMonoApp : INativeApp
  12. {
  13. /// <summary>
  14. /// Shutdowns this instance.
  15. /// </summary>
  16. public abstract void Shutdown();
  17. /// <summary>
  18. /// Restarts this instance.
  19. /// </summary>
  20. public virtual void Restart(StartupOptions options)
  21. {
  22. throw new NotImplementedException();
  23. }
  24. /// <summary>
  25. /// Determines whether this instance [can self restart].
  26. /// </summary>
  27. /// <returns><c>true</c> if this instance [can self restart]; otherwise, <c>false</c>.</returns>
  28. public virtual bool CanSelfRestart
  29. {
  30. get
  31. {
  32. return false;
  33. }
  34. }
  35. /// <summary>
  36. /// Gets a value indicating whether this instance can self update.
  37. /// </summary>
  38. /// <value><c>true</c> if this instance can self update; otherwise, <c>false</c>.</value>
  39. public bool CanSelfUpdate
  40. {
  41. get
  42. {
  43. return false;
  44. }
  45. }
  46. public bool SupportsAutoRunAtStartup
  47. {
  48. get { return false; }
  49. }
  50. public void PreventSystemStandby()
  51. {
  52. }
  53. public List<Assembly> GetAssembliesWithParts()
  54. {
  55. var list = new List<Assembly>();
  56. list.Add(GetType().Assembly);
  57. return list;
  58. }
  59. public void AuthorizeServer(int udpPort, int httpServerPort, int httpsPort, string tempDirectory)
  60. {
  61. }
  62. private NativeEnvironment _nativeEnvironment;
  63. public NativeEnvironment Environment
  64. {
  65. get { return _nativeEnvironment ?? (_nativeEnvironment = GetEnvironmentInfo()); }
  66. }
  67. public bool SupportsRunningAsService
  68. {
  69. get
  70. {
  71. return false;
  72. }
  73. }
  74. public bool IsRunningAsService
  75. {
  76. get
  77. {
  78. return false;
  79. }
  80. }
  81. public void ConfigureAutoRun(bool autorun)
  82. {
  83. }
  84. public INetworkManager CreateNetworkManager(ILogger logger)
  85. {
  86. return new NetworkManager(logger);
  87. }
  88. private NativeEnvironment GetEnvironmentInfo()
  89. {
  90. var info = new NativeEnvironment
  91. {
  92. OperatingSystem = Startup.Common.OperatingSystem.Linux
  93. };
  94. var uname = GetUnixName();
  95. var sysName = uname.sysname ?? string.Empty;
  96. info.OperatingSystem = Startup.Common.OperatingSystem.Osx;
  97. var archX86 = new Regex("(i|I)[3-6]86");
  98. if (archX86.IsMatch(uname.machine))
  99. {
  100. info.SystemArchitecture = Architecture.X86;
  101. }
  102. else if (string.Equals(uname.machine, "x86_64", StringComparison.OrdinalIgnoreCase))
  103. {
  104. info.SystemArchitecture = Architecture.X86_X64;
  105. }
  106. else if (uname.machine.StartsWith("arm", StringComparison.OrdinalIgnoreCase))
  107. {
  108. info.SystemArchitecture = Architecture.Arm;
  109. }
  110. info.OperatingSystemVersionString = string.IsNullOrWhiteSpace(sysName) ?
  111. System.Environment.OSVersion.VersionString :
  112. sysName;
  113. return info;
  114. }
  115. private Uname _unixName;
  116. private Uname GetUnixName()
  117. {
  118. if (_unixName == null)
  119. {
  120. var uname = new Uname();
  121. Utsname utsname;
  122. var callResult = Syscall.uname(out utsname);
  123. if (callResult == 0)
  124. {
  125. uname.sysname = utsname.sysname;
  126. uname.machine = utsname.machine;
  127. }
  128. _unixName = uname;
  129. }
  130. return _unixName;
  131. }
  132. private class Uname
  133. {
  134. public string sysname = string.Empty;
  135. public string machine = string.Empty;
  136. }
  137. }
  138. }