using System; using System.Collections.Generic; using System.Linq; using System.Security.Authentication; 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; using Tango.Transport; using Tango.Transport.Adapters; namespace Tango.Integration.ExternalBridge { public class ExternalBridgeSignalRClient : ExternalBridgeTcpClient { public ExternalBridgeSignalRClient(String url, String hub, MachineInfo machineInfo) { ComponentName = $"External Bridge SignalR Client {_component_counter++}"; 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, MachineInfo machineInfo) { ComponentName = $"External Bridge SignalR Client {_component_counter++}"; SerialNumber = machine.SerialNumber; IPAddress = machineInfo.IPAddress; Machine = machine; Adapter = new SignalRTransportAdapter(url, hub, SignalRTransportAdapterMode.CreateSession, SerialNumber, null, IPAddress); KeepAliveTimeout = TimeSpan.FromSeconds(5); KeepAliveRetries = 2; UseKeepAlive = false; } /// /// Connects to a remote external bridge service using the specified login. /// /// The login request. /// Optional protocol configuration. /// /// public override async Task Connect(ExternalBridgeLoginRequest login, ConfigureProtocolRequest protocol = null) { if (State != TransportComponentState.Connected) { try { Adapter.EnableCompression = false; GenericProtocol = GenericMessageProtocol.Json; await Adapter.Connect(); State = TransportComponentState.Connected; StartThreads(); LogManager.Log($"{ComponentName}: External Bridge SignalR Client Connected..."); TimeSpan? timeout = null; if (login.RequireSafetyLevelOperations) { timeout = TimeSpan.FromSeconds(35); } var response = await SendRequest(login, new TransportRequestConfig() { ShouldLog = true, Timeout = timeout }); if (protocol != null) { try { var configureResponse = await SendRequest(protocol, new TransportRequestConfig() { ShouldLog = true }); if (configureResponse.Message.Confirmed) { await Task.Delay(500); Adapter.EnableCompression = protocol.EnableCompression; GenericProtocol = protocol.GenericProtocol; } } catch (Exception ex) { if (ForceProtocolConfiguration) { LogManager.Log(ex, $"{ComponentName}: Could not configure remote machine protocol. Could be an old PPC version. (forcing protocol configuration)"); Adapter.EnableCompression = protocol.EnableCompression; GenericProtocol = protocol.GenericProtocol; } else { LogManager.Log(ex, $"{ComponentName}: Could not configure remote machine protocol. Could be an old PPC version."); } } } ApplicationInformation = response.Message.ApplicationInformation; SessionLogger.CreateSession(); DeviceInformation = response.Message.DeviceInformation; if (!response.Message.Authenticated) { await Adapter.Disconnect(); throw new AuthenticationException(response.Container.ErrorMessage); } } catch (Exception ex) { try { await Adapter.Disconnect(); } catch { } throw ex; } ApplyContinuousChannelsConfiguration(); } } } }