From 7594cea7dc1b3fef5be77fa06c71a954d024723d Mon Sep 17 00:00:00 2001 From: Roy Ben Shabat Date: Thu, 26 Mar 2020 08:41:04 +0200 Subject: Added support for public IP address on SignalR connection. Fixed issue with ExternalBridgeScanner. Added Connection Time. --- .../Connection/IMachineProvider.cs | 5 ++++ .../Connection/DefaultMachineProvider.cs | 33 ++++++++++++++++++++-- .../Tango.FSE.UI/Panes/ConnectedMachinePane.xaml | 8 ++++-- .../Tango.FSE.UI/Panes/MachineConnectionPane.xaml | 2 +- .../ExternalBridge/ExternalBridgeScanner.cs | 6 ++-- .../ExternalBridge/ExternalBridgeSignalRClient.cs | 17 +++++------ .../ExternalBridge/Web/MachineInfo.cs | 6 ++++ .../Adapters/SignalRTransportAdapter.cs | 8 ++++-- .../Tango.Transport/TransportAdapterBase.cs | 8 +++++- .../Tango.MachineService/Hubs/ExternalBridgeHub.cs | 30 +++++++++++++++++++- 10 files changed, 101 insertions(+), 22 deletions(-) (limited to 'Software/Visual_Studio') diff --git a/Software/Visual_Studio/FSE/Tango.FSE.Common/Connection/IMachineProvider.cs b/Software/Visual_Studio/FSE/Tango.FSE.Common/Connection/IMachineProvider.cs index be4748617..8055f7177 100644 --- a/Software/Visual_Studio/FSE/Tango.FSE.Common/Connection/IMachineProvider.cs +++ b/Software/Visual_Studio/FSE/Tango.FSE.Common/Connection/IMachineProvider.cs @@ -35,6 +35,11 @@ namespace Tango.FSE.Common.Connection /// bool IsConnected { get; } + /// + /// Gets the connection time span. + /// + TimeSpan ConnectionTime { get; } + /// /// Gets a value indicating whether this instance is busy connecting/disconnecting. /// diff --git a/Software/Visual_Studio/FSE/Tango.FSE.UI/Connection/DefaultMachineProvider.cs b/Software/Visual_Studio/FSE/Tango.FSE.UI/Connection/DefaultMachineProvider.cs index c97837fcf..25c2d31a9 100644 --- a/Software/Visual_Studio/FSE/Tango.FSE.UI/Connection/DefaultMachineProvider.cs +++ b/Software/Visual_Studio/FSE/Tango.FSE.UI/Connection/DefaultMachineProvider.cs @@ -25,9 +25,6 @@ namespace Tango.FSE.UI.Connection { public class DefaultMachineProvider : ExtendedObject, IMachineProvider { - private List _eventRegistrations; - private String _lastMachineSerialNumber; - private class EventRegistration { public Delegate Handler { get; set; } @@ -58,6 +55,11 @@ namespace Tango.FSE.UI.Connection } } + private List _eventRegistrations; + private String _lastMachineSerialNumber; + private DateTime _connectionStartTime; + private System.Timers.Timer _connectionTimer; + /// /// Occurs when machine operator has connected. /// @@ -97,6 +99,16 @@ namespace Tango.FSE.UI.Connection private set { _isConnected = value; RaisePropertyChangedAuto(); } } + private TimeSpan _connectionTime; + /// + /// Gets the connection time span. + /// + public TimeSpan ConnectionTime + { + get { return _connectionTime; } + set { _connectionTime = value; RaisePropertyChangedAuto(); } + } + private bool _isBusy; /// /// Gets a value indicating whether this instance is busy connecting/disconnecting. @@ -125,6 +137,10 @@ namespace Tango.FSE.UI.Connection MachineOperator = new ExternalBridgeTcpClient("N/A", "N/A"); MachineOperator.StateChanged += MachineOperator_StateChanged; MachineOperator.As().SessionClosed += DefaultMachineProvider_SessionClosed; + + _connectionTimer = new System.Timers.Timer(1000); + _connectionTimer.Elapsed += _connectionTimer_Elapsed; + _connectionTimer.Start(); } /// @@ -352,10 +368,13 @@ namespace Tango.FSE.UI.Connection }); _lastMachineSerialNumber = machineOperator.SerialNumber; + _connectionStartTime = DateTime.Now; } protected virtual void OnMachineDisconnected(IExternalBridgeClient machineOperator, Exception exception) { + LogManager.Log($"Machine disconnected. Total connection time: {DateTime.Now - _connectionStartTime}."); + MachineDisconnected?.Invoke(this, new MachineDisconnectedEventArgs() { MachineOperator = machineOperator, @@ -363,6 +382,14 @@ namespace Tango.FSE.UI.Connection }); } + private void _connectionTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) + { + if (IsConnected) + { + ConnectionTime = DateTime.Now - _connectionStartTime; + } + } + /// /// Reassigns the event handlers from the previous connected machine to a new one. /// diff --git a/Software/Visual_Studio/FSE/Tango.FSE.UI/Panes/ConnectedMachinePane.xaml b/Software/Visual_Studio/FSE/Tango.FSE.UI/Panes/ConnectedMachinePane.xaml index 5f4bdf547..0e01ab255 100644 --- a/Software/Visual_Studio/FSE/Tango.FSE.UI/Panes/ConnectedMachinePane.xaml +++ b/Software/Visual_Studio/FSE/Tango.FSE.UI/Panes/ConnectedMachinePane.xaml @@ -48,9 +48,7 @@ - - - + FPGA Version 1: @@ -66,6 +64,10 @@ + + + + diff --git a/Software/Visual_Studio/FSE/Tango.FSE.UI/Panes/MachineConnectionPane.xaml b/Software/Visual_Studio/FSE/Tango.FSE.UI/Panes/MachineConnectionPane.xaml index 2f0ca8ae8..00b7574cb 100644 --- a/Software/Visual_Studio/FSE/Tango.FSE.UI/Panes/MachineConnectionPane.xaml +++ b/Software/Visual_Studio/FSE/Tango.FSE.UI/Panes/MachineConnectionPane.xaml @@ -105,7 +105,7 @@ S/N: - Hub: + Address: Organization: diff --git a/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeScanner.cs b/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeScanner.cs index 21767174f..160555550 100644 --- a/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeScanner.cs +++ b/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeScanner.cs @@ -135,7 +135,7 @@ namespace Tango.Integration.ExternalBridge bool signalRStarted = false; - _connection.StateChanged += (x) => + _connection.StateChanged += (x) => { if (x.NewState == ConnectionState.Connected) { @@ -298,11 +298,11 @@ namespace Tango.Integration.ExternalBridge if (knownMachine == null) { - newMachine = new ExternalBridgeSignalRClient(SignalRConfiguration.Address, SignalRConfiguration.Hub, machine.SerialNumber); + newMachine = new ExternalBridgeSignalRClient(SignalRConfiguration.Address, SignalRConfiguration.Hub, machine); } else { - newMachine = new ExternalBridgeSignalRClient(SignalRConfiguration.Address, SignalRConfiguration.Hub, knownMachine); + newMachine = new ExternalBridgeSignalRClient(SignalRConfiguration.Address, SignalRConfiguration.Hub, knownMachine,machine); } ThreadsHelper.InvokeUINow(() => diff --git a/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeSignalRClient.cs b/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeSignalRClient.cs index 1596d2c9b..343fb302f 100644 --- a/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeSignalRClient.cs +++ b/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeSignalRClient.cs @@ -6,6 +6,7 @@ using System.Text; using System.Threading.Tasks; using Tango.BL; using Tango.BL.Entities; +using Tango.Integration.ExternalBridge.Web; using Tango.Integration.Operation; using Tango.PMR.Integration; using Tango.Settings; @@ -16,28 +17,28 @@ namespace Tango.Integration.ExternalBridge { public class ExternalBridgeSignalRClient : ExternalBridgeTcpClient { - public ExternalBridgeSignalRClient(String url, String hub, String serialNumber) + public ExternalBridgeSignalRClient(String url, String hub, MachineInfo machineInfo) { ComponentName = $"External Bridge SignalR Client {_component_counter++}"; - SerialNumber = serialNumber; - IPAddress = hub; - Machine = ObservablesStaticCollections.Instance.Machines.SingleOrDefault(x => x.SerialNumber == serialNumber); - Adapter = new SignalRTransportAdapter(url, hub, SignalRTransportAdapterMode.CreateSession, serialNumber); + SerialNumber = machineInfo.SerialNumber; + IPAddress = machineInfo.IPAddress; + Machine = ObservablesStaticCollections.Instance.Machines.SingleOrDefault(x => x.SerialNumber == SerialNumber); + Adapter = new SignalRTransportAdapter(url, hub, SignalRTransportAdapterMode.CreateSession, SerialNumber, null, IPAddress); KeepAliveTimeout = TimeSpan.FromSeconds(5); KeepAliveRetries = 2; UseKeepAlive = false; } - public ExternalBridgeSignalRClient(String url, String hub, Machine machine) + public ExternalBridgeSignalRClient(String url, String hub, Machine machine, MachineInfo machineInfo) { ComponentName = $"External Bridge SignalR Client {_component_counter++}"; SerialNumber = machine.SerialNumber; - IPAddress = hub; + IPAddress = machineInfo.IPAddress; Machine = machine; - Adapter = new SignalRTransportAdapter(url, hub, SignalRTransportAdapterMode.CreateSession, SerialNumber); + Adapter = new SignalRTransportAdapter(url, hub, SignalRTransportAdapterMode.CreateSession, SerialNumber, null, IPAddress); KeepAliveTimeout = TimeSpan.FromSeconds(5); KeepAliveRetries = 2; diff --git a/Software/Visual_Studio/Tango.Integration/ExternalBridge/Web/MachineInfo.cs b/Software/Visual_Studio/Tango.Integration/ExternalBridge/Web/MachineInfo.cs index a7e3f90ca..a33d073d1 100644 --- a/Software/Visual_Studio/Tango.Integration/ExternalBridge/Web/MachineInfo.cs +++ b/Software/Visual_Studio/Tango.Integration/ExternalBridge/Web/MachineInfo.cs @@ -10,5 +10,11 @@ namespace Tango.Integration.ExternalBridge.Web { public String SerialNumber { get; set; } public String Organization { get; set; } + public String IPAddress { get; set; } + + public MachineInfo() + { + IPAddress = "Unknown"; + } } } diff --git a/Software/Visual_Studio/Tango.Transport/Adapters/SignalRTransportAdapter.cs b/Software/Visual_Studio/Tango.Transport/Adapters/SignalRTransportAdapter.cs index a8f272292..def4cd741 100644 --- a/Software/Visual_Studio/Tango.Transport/Adapters/SignalRTransportAdapter.cs +++ b/Software/Visual_Studio/Tango.Transport/Adapters/SignalRTransportAdapter.cs @@ -78,7 +78,7 @@ namespace Tango.Transport.Adapters /// The adapter mode. /// The machine serial number (when creating session). /// The session identifier (when joining session). - public SignalRTransportAdapter(String url, String hub, SignalRTransportAdapterMode mode, String serialNumber = null, String sessionID = null) : this() + public SignalRTransportAdapter(String url, String hub, SignalRTransportAdapterMode mode, String serialNumber = null, String sessionID = null, String ipAddress = null) : this() { Url = url; Hub = hub; @@ -86,6 +86,11 @@ namespace Tango.Transport.Adapters SerialNumber = serialNumber; SessionID = sessionID; Address = sessionID; + + if (!String.IsNullOrWhiteSpace(ipAddress)) + { + Address = ipAddress; + } } /// @@ -184,7 +189,6 @@ namespace Tango.Transport.Adapters { LogManager.Log("Creating SignalR adapter Session..."); SessionID = await _proxy.Invoke("CreateSession", SerialNumber); - Address = SessionID; } else { diff --git a/Software/Visual_Studio/Tango.Transport/TransportAdapterBase.cs b/Software/Visual_Studio/Tango.Transport/TransportAdapterBase.cs index 2696f5698..bdcf0ee64 100644 --- a/Software/Visual_Studio/Tango.Transport/TransportAdapterBase.cs +++ b/Software/Visual_Studio/Tango.Transport/TransportAdapterBase.cs @@ -77,10 +77,16 @@ namespace Tango.Transport protected set { _transferRate = value; RaisePropertyChanged(nameof(TransferRate)); } } + private String _address; /// /// Gets or sets the channel address. /// - public String Address { get; set; } + public String Address + { + get { return _address; } + set { _address = value; RaisePropertyChangedAuto(); } + } + private TransportComponentState _state; /// diff --git a/Software/Visual_Studio/Web/Tango.MachineService/Hubs/ExternalBridgeHub.cs b/Software/Visual_Studio/Web/Tango.MachineService/Hubs/ExternalBridgeHub.cs index 4dd04d33e..9b7f734d2 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService/Hubs/ExternalBridgeHub.cs +++ b/Software/Visual_Studio/Web/Tango.MachineService/Hubs/ExternalBridgeHub.cs @@ -43,6 +43,8 @@ namespace Tango.MachineService.Hubs _externalBridges.RemoveAll(x => x.MachineInfo.SerialNumber == machineInfo.SerialNumber); _sessions.RemoveAll(x => x.ExternalBridge.ConnectionID == Context.ConnectionId); + machineInfo.IPAddress = GetIpAddress(); + _externalBridges.Add(new ExternalBridgeInfo() { MachineInfo = machineInfo, @@ -69,7 +71,7 @@ namespace Tango.MachineService.Hubs public String CreateSession(String serialNumber) { var externalBridge = _externalBridges.SingleOrDefault(x => x.MachineInfo.SerialNumber == serialNumber); - + if (externalBridge != null) { var existingSession = GetSession(); @@ -157,5 +159,31 @@ namespace Tango.MachineService.Hubs return null; } + + protected string GetIpAddress() + { + try + { + string ipAddress; + object tempObject; + + Context.Request.Environment.TryGetValue("server.RemoteIpAddress", out tempObject); + + if (tempObject != null) + { + ipAddress = (string)tempObject; + } + else + { + ipAddress = ""; + } + + return ipAddress; + } + catch + { + return String.Empty; + } + } } } \ No newline at end of file -- cgit v1.3.1