| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using Implementation.Interfaces; |
| | | 5 | | using ReFlex.Core.Common.Components; |
| | | 6 | | using ReFlex.Core.Networking.Components; |
| | | 7 | | using ReFlex.Core.Networking.Interfaces; |
| | | 8 | | using ReFlex.Core.Networking.Util; |
| | | 9 | | using NLog; |
| | | 10 | | using Prism.Events; |
| | | 11 | | |
| | | 12 | | namespace Implementation.Components |
| | | 13 | | { |
| | | 14 | | public class NetworkManager : INetworkManager |
| | | 15 | | { |
| | | 16 | | private IServer _server; |
| | 0 | 17 | | private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); |
| | 0 | 18 | | private string _address = Localhost; |
| | 0 | 19 | | private string _endpoint = "/ReFlex"; |
| | 0 | 20 | | private int _port = 8080; |
| | | 21 | | private readonly IEventAggregator _evtAggregator; |
| | | 22 | | |
| | | 23 | | |
| | 0 | 24 | | public static string Localhost = "127.0.0.1"; |
| | | 25 | | |
| | | 26 | | public string ServerAddress |
| | | 27 | | { |
| | 0 | 28 | | get => _server?.Address; |
| | | 29 | | } |
| | | 30 | | |
| | 0 | 31 | | public bool IsRunning => _server?.IsStarted ?? false; |
| | | 32 | | |
| | | 33 | | public string Address |
| | | 34 | | { |
| | 0 | 35 | | get => _address; |
| | | 36 | | set |
| | 0 | 37 | | { |
| | 0 | 38 | | if (_address == value) |
| | 0 | 39 | | return; |
| | 0 | 40 | | _address = value; |
| | 0 | 41 | | UpdateServerAddress(); |
| | 0 | 42 | | } |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | public string Endpoint |
| | | 46 | | { |
| | 0 | 47 | | get => _endpoint; |
| | | 48 | | set |
| | 0 | 49 | | { |
| | 0 | 50 | | if (_endpoint == value) |
| | 0 | 51 | | return; |
| | 0 | 52 | | _endpoint = value; |
| | 0 | 53 | | UpdateServerAddress(); |
| | 0 | 54 | | } |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | public int Port |
| | | 58 | | { |
| | 0 | 59 | | get => _port; |
| | | 60 | | set |
| | 0 | 61 | | { |
| | 0 | 62 | | if (_port == value) |
| | 0 | 63 | | return; |
| | 0 | 64 | | _port = value; |
| | 0 | 65 | | UpdateServerAddress(); |
| | 0 | 66 | | } |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | public NetworkInterface Type |
| | | 70 | | { |
| | 0 | 71 | | get => _server?.Type ?? NetworkInterface.None; |
| | | 72 | | set |
| | 0 | 73 | | { |
| | 0 | 74 | | ChangeServer(value); |
| | 0 | 75 | | } |
| | | 76 | | } |
| | | 77 | | |
| | 0 | 78 | | public NetworkManager(IEventAggregator eventAggregator) |
| | 0 | 79 | | { |
| | 0 | 80 | | _evtAggregator = eventAggregator; |
| | 0 | 81 | | } |
| | | 82 | | |
| | | 83 | | public void Run() |
| | 0 | 84 | | { |
| | 0 | 85 | | if (_server == null) |
| | 0 | 86 | | return; |
| | | 87 | | |
| | 0 | 88 | | _server?.Start(); |
| | 0 | 89 | | Logger?.Info("Server started succesfully on port " + Port); |
| | 0 | 90 | | } |
| | | 91 | | |
| | | 92 | | public void Broadcast(ICollection<Interaction> interactions) |
| | 0 | 93 | | { |
| | 0 | 94 | | if (_server == null || !_server.IsStarted) |
| | 0 | 95 | | return; |
| | | 96 | | |
| | 0 | 97 | | _server.Broadcast(interactions); |
| | 0 | 98 | | Logger?.Trace("Server broadcast some interactions: " + interactions.FirstOrDefault()); |
| | 0 | 99 | | } |
| | | 100 | | |
| | | 101 | | public void Stop() |
| | 0 | 102 | | { |
| | 0 | 103 | | if (_server == null) |
| | 0 | 104 | | return; |
| | | 105 | | |
| | 0 | 106 | | _server.Stop(); |
| | 0 | 107 | | Logger?.Info("Server stopped"); |
| | 0 | 108 | | } |
| | | 109 | | |
| | | 110 | | private void ChangeServer(NetworkInterface type) |
| | 0 | 111 | | { |
| | 0 | 112 | | Stop(); |
| | | 113 | | |
| | 0 | 114 | | switch (type) |
| | | 115 | | { |
| | | 116 | | case NetworkInterface.Websockets: |
| | 0 | 117 | | _server = new WebSocketServer(Address, Port, Endpoint); |
| | 0 | 118 | | break; |
| | | 119 | | case NetworkInterface.Tcp: |
| | 0 | 120 | | _server = new NetworkServer(Address, Port); |
| | 0 | 121 | | break; |
| | | 122 | | case NetworkInterface.None: |
| | 0 | 123 | | break; |
| | | 124 | | default: |
| | 0 | 125 | | throw new ArgumentOutOfRangeException(nameof(type), type, null); |
| | | 126 | | } |
| | 0 | 127 | | } |
| | | 128 | | |
| | | 129 | | private void UpdateServerAddress() |
| | 0 | 130 | | { |
| | 0 | 131 | | if (_server == null) |
| | 0 | 132 | | { |
| | 0 | 133 | | ChangeServer(Type); |
| | 0 | 134 | | return; |
| | | 135 | | } |
| | | 136 | | |
| | 0 | 137 | | _server.Address = $"{_address}:{_port}{_endpoint}"; |
| | 0 | 138 | | } |
| | | 139 | | } |
| | | 140 | | } |