PageRenderTime 57ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/PeerCastStation/PeerCastStation.WPF/PeerCastAppViewModel.cs

https://github.com/kumaryu/peercaststation
C# | 214 lines | 178 code | 21 blank | 15 comment | 9 complexity | 4f433b3b79f249a76c1900ffe5e33476 MD5 | raw file
  1. // PeerCastStation, a P2P streaming servent.
  2. // Copyright (C) 2013 PROGRE (djyayutto@gmail.com)
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. using System;
  17. using System.Linq;
  18. using PeerCastStation.Core;
  19. using PeerCastStation.WPF.ChannelLists;
  20. using PeerCastStation.WPF.Commons;
  21. using PeerCastStation.WPF.Dialogs;
  22. using PeerCastStation.WPF.Logs;
  23. using System.Windows;
  24. using System.Net.Sockets;
  25. namespace PeerCastStation.WPF
  26. {
  27. class PeerCastAppViewModel : ViewModelBase, IDisposable, IPeerCastMonitor
  28. {
  29. private readonly PeerCastApplication application;
  30. public PeerCastApplication Model { get { return application; } }
  31. public string WindowTitle { get; private set; }
  32. private string GetPortStatus(AddressFamily family)
  33. {
  34. switch (application.PeerCast.GetPortStatus(family)) {
  35. case Core.PortStatus.Open:
  36. return "開放";
  37. case Core.PortStatus.Firewalled:
  38. return "未開放";
  39. case Core.PortStatus.Unavailable:
  40. return "利用不可";
  41. case Core.PortStatus.Unknown:
  42. default:
  43. return "開放状態不明";
  44. }
  45. }
  46. public string PortStatus
  47. {
  48. get
  49. {
  50. var peerCast = application.PeerCast;
  51. return "リレー可能ポート:" + String.Join(", ",
  52. peerCast.OutputListeners
  53. .Where(listener => (listener.GlobalOutputAccepts & OutputStreamType.Relay)!=0)
  54. .Select(listener => listener.LocalEndPoint.Port)
  55. .Distinct()
  56. .Select(port => port.ToString())
  57. .ToArray()
  58. )
  59. + " IPv4:" + GetPortStatus(AddressFamily.InterNetwork)
  60. + " IPv6:" + GetPortStatus(AddressFamily.InterNetworkV6);
  61. }
  62. }
  63. private readonly ChannelListViewModel channelList;
  64. public ChannelListViewModel ChannelList { get { return channelList; } }
  65. public string Version { get { return this.application.PeerCast.AgentName; } }
  66. private readonly LogViewModel log = new LogViewModel();
  67. public LogViewModel Log { get { return log; } }
  68. internal VersionInfoViewModel VersionInfo
  69. {
  70. get { return new VersionInfoViewModel(application); }
  71. }
  72. private readonly WPFSettings settings;
  73. internal PeerCastAppViewModel(PeerCastApplication application)
  74. {
  75. this.application = application;
  76. settings = application.Settings.Get<WPFSettings>();
  77. var peerCast = application.PeerCast;
  78. channelList = new ChannelListViewModel(peerCast);
  79. peerCast.AddChannelMonitor(this);
  80. WindowTitle = CreateWindowTitle();
  81. }
  82. public void Dispose()
  83. {
  84. application.PeerCast.RemoveChannelMonitor(this);
  85. log.Dispose();
  86. }
  87. public void UpdateStatus()
  88. {
  89. OnPropertyChanged("PortStatus");
  90. channelList.UpdateChannelList();
  91. UpdateWindowTitle();
  92. log.UpdateLog();
  93. }
  94. public void OnChannelChanged(PeerCastChannelAction action, Channel channel)
  95. {
  96. Application.Current.Dispatcher.BeginInvoke(new Action(() => {
  97. channelList.UpdateChannelList();
  98. }));
  99. }
  100. public void OnTimer()
  101. {
  102. }
  103. public void OpenBrowserUI()
  104. {
  105. var listener =
  106. application.PeerCast.FindListener(System.Net.IPAddress.Loopback, OutputStreamType.Interface) ??
  107. application.PeerCast.FindListener(System.Net.IPAddress.IPv6Loopback, OutputStreamType.Interface) ??
  108. application.PeerCast.FindListener(System.Net.IPAddress.Loopback, OutputStreamType.All) ??
  109. application.PeerCast.FindListener(System.Net.IPAddress.IPv6Loopback, OutputStreamType.All);
  110. if (listener != null) {
  111. var endpoint = listener.LocalEndPoint;
  112. var host =
  113. endpoint.Address.Equals(System.Net.IPAddress.Any) ||
  114. endpoint.Address.Equals(System.Net.IPAddress.IPv6Any) ?
  115. String.Format("localhost:{0}", endpoint.Port) :
  116. endpoint.ToString();
  117. System.Diagnostics.Process.Start(
  118. new System.Diagnostics.ProcessStartInfo($"http://{host}/html/index.html") {
  119. UseShellExecute = true,
  120. }
  121. );
  122. }
  123. }
  124. public void OpenHelp()
  125. {
  126. var listener =
  127. application.PeerCast.FindListener(System.Net.IPAddress.Loopback, OutputStreamType.Interface) ??
  128. application.PeerCast.FindListener(System.Net.IPAddress.IPv6Loopback, OutputStreamType.Interface) ??
  129. application.PeerCast.FindListener(System.Net.IPAddress.Loopback, OutputStreamType.All) ??
  130. application.PeerCast.FindListener(System.Net.IPAddress.IPv6Loopback, OutputStreamType.All);
  131. if (listener != null) {
  132. var endpoint = listener.LocalEndPoint;
  133. var host =
  134. endpoint.Address.Equals(System.Net.IPAddress.Any) ||
  135. endpoint.Address.Equals(System.Net.IPAddress.IPv6Any) ?
  136. String.Format("localhost:{0}", endpoint.Port) :
  137. endpoint.ToString();
  138. System.Diagnostics.Process.Start(
  139. new System.Diagnostics.ProcessStartInfo($"http://{host}/help/index.html") {
  140. UseShellExecute = true,
  141. }
  142. );
  143. }
  144. }
  145. public void Quit()
  146. {
  147. application.Stop();
  148. }
  149. private string CreateWindowTitle()
  150. {
  151. switch (settings.WindowTitleMode) {
  152. case WindowTitleMode.Simple:
  153. return System.Text.RegularExpressions.Regex.Replace(application.PeerCast.AgentName, "/.*", "");
  154. case WindowTitleMode.Version:
  155. return application.PeerCast.AgentName;
  156. case WindowTitleMode.ChannelStats:
  157. var cnt = application.PeerCast.Channels.Count;
  158. var (localDirects, localRelays, totalDirects, totalRelays) =
  159. application.PeerCast.Channels
  160. .Aggregate((0, 0, 0, 0), (r, c) =>
  161. (r.Item1 + c.LocalDirects, r.Item2 + c.LocalRelays, r.Item3 + c.TotalDirects, r.Item4 + c.TotalRelays)
  162. );
  163. return $"{cnt}ch ({totalDirects}/{totalRelays}) [{localDirects}/{localRelays}]";
  164. default:
  165. return "";
  166. }
  167. }
  168. private WindowTitleMode lastWindowTitleMode = WindowTitleMode.Simple;
  169. private void UpdateWindowTitle()
  170. {
  171. switch (settings.WindowTitleMode) {
  172. case WindowTitleMode.Simple:
  173. if (lastWindowTitleMode!=settings.WindowTitleMode) {
  174. WindowTitle = CreateWindowTitle();
  175. OnPropertyChanged(nameof(WindowTitle));
  176. lastWindowTitleMode = settings.WindowTitleMode;
  177. }
  178. break;
  179. case WindowTitleMode.Version:
  180. if (lastWindowTitleMode!=settings.WindowTitleMode) {
  181. WindowTitle = CreateWindowTitle();
  182. OnPropertyChanged(nameof(WindowTitle));
  183. }
  184. break;
  185. case WindowTitleMode.ChannelStats:
  186. WindowTitle = CreateWindowTitle();
  187. OnPropertyChanged(nameof(WindowTitle));
  188. break;
  189. }
  190. lastWindowTitleMode = settings.WindowTitleMode;
  191. }
  192. }
  193. }