diff options
Diffstat (limited to 'Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeTcpClient.cs')
| -rw-r--r-- | Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeTcpClient.cs | 207 |
1 files changed, 50 insertions, 157 deletions
diff --git a/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeTcpClient.cs b/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeTcpClient.cs index 0b2a0c608..8acff77d4 100644 --- a/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeTcpClient.cs +++ b/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeTcpClient.cs @@ -14,7 +14,6 @@ using Tango.PMR; using Tango.PMR.Common; using Tango.PMR.Connection; using Tango.PMR.Integration; -using Tango.PMR.MachineStatus; using Tango.Settings; using Tango.Transport; using Tango.Transport.Adapters; @@ -31,8 +30,6 @@ namespace Tango.Integration.ExternalBridge { private bool _logs_sent; - public event EventHandler<LogItemBase> ApplicationLogAvailable; - #region Properties private String _serialNumber; @@ -74,113 +71,54 @@ namespace Tango.Integration.ExternalBridge } } - public bool InjectApplicationLogsToDefaultLogManager { get; set; } = true; - /// <summary> /// Gets a value indicating whether this client requires authentication. /// </summary> public bool RequiresAuthentication => true; - /// <summary> - /// Gets or sets the login request message when using <see cref="Connect"/>. - /// </summary> - public ExternalBridgeLoginRequest LoginRequest { get; set; } - - /// <summary> - /// Gets or sets the configure protocol request message when using <see cref="Connect"/>. - /// </summary> - public ConfigureProtocolRequest ConfigureProtocolRequest { get; set; } - - private ApplicationInformation _applicationInformation; - /// <summary> - /// Gets or sets the remote application information (PPC). - /// </summary> - public ApplicationInformation ApplicationInformation - { - get { return _applicationInformation; } - protected set { _applicationInformation = value; RaisePropertyChangedAuto(); } - } - #endregion - /// <summary> - /// Connects to a remote external bridge service using the specified <see cref="LoginRequest"/>. - /// </summary> - /// <param name="login">The login.</param> - /// <returns></returns> - /// <exception cref="AuthenticationException">The machine password is invalid.</exception> public override Task Connect() { - if (LoginRequest == null) - { - throw new InvalidOperationException("No LoginRequest was not specified."); - } - return Connect(LoginRequest, ConfigureProtocolRequest); + throw new NotImplementedException("External Bridge TCP client must connect through the dedicated connect method."); } /// <summary> /// Connects to a remote external bridge service using the specified login. /// </summary> - /// <param name="login">The login request.</param> - /// <param name="protocol">Optional protocol configuration.</param> + /// <param name="login">The login.</param> /// <returns></returns> - /// <exception cref="AuthenticationException"></exception> - public virtual async Task Connect(ExternalBridgeLoginRequest login, ConfigureProtocolRequest protocol = null) + /// <exception cref="AuthenticationException">The machine password is invalid.</exception> + public async Task Connect(ExternalBridgeLoginRequest login) { if (State != TransportComponentState.Connected) { try { - Adapter.EnableCompression = false; - GenericProtocol = GenericMessageProtocol.Json; - + Adapter = new TcpTransportAdapter(IPAddress, SettingsManager.Default.GetOrCreate<IntegrationSettings>().ExternalBridgeServicePort); await Adapter.Connect(); State = TransportComponentState.Connected; StartThreads(); - LogManager.Log($"{ComponentName}: External Bridge TCP Client Connected..."); + LogManager.Log("External Bridge TCP Client Connected..."); - TimeSpan? timeout = null; - if (login.RequireSafetyLevelOperations) - { - timeout = TimeSpan.FromSeconds(35); - } - - var response = await SendRequest<ExternalBridgeLoginRequest, ExternalBridgeLoginResponse>(login, new TransportRequestConfig() { ShouldLog = true, Timeout = timeout }); - - if (protocol != null) - { - try - { - var configureResponse = await SendRequest<ConfigureProtocolRequest, ConfigureProtocolResponse>(protocol, new TransportRequestConfig() { ShouldLog = true }); - if (configureResponse.Message.Confirmed) - { - await Task.Delay(500); - Adapter.EnableCompression = protocol.EnableCompression; - GenericProtocol = protocol.GenericProtocol; - } - } - catch (Exception ex) - { - LogManager.Log(ex, $"{ComponentName}: Could not configure remote machine protocol. Could be an old PPC version."); - } - } - - ApplicationInformation = response.Message.ApplicationInformation; - - SessionLogger.CreateSession(); + LogRequestSent(login); + var response = await SendRequest<ExternalBridgeLoginRequest, ExternalBridgeLoginResponse>(login); DeviceInformation = response.Message.DeviceInformation; if (!response.Message.Authenticated) { await Adapter.Disconnect(); throw new AuthenticationException(response.Container.ErrorMessage); } + + Status = MachineStatuses.ReadyToDye; } catch (Exception ex) { + LogRequestFailed(login, ex); try { await Adapter.Disconnect(); @@ -190,23 +128,15 @@ namespace Tango.Integration.ExternalBridge throw ex; } - ApplyContinuousChannelsConfiguration(); + OnEnableDiagnosticsChanged(EnableDiagnostics); + OnEnableEmbeddedDebuggingChanged(EnableEmbeddedDebugging); + OnEnableEventsNotification(EnableEventsNotification); + OnEnableApplicationLogsChanged(EnableApplicationLogs); + OnEnableMachineStatusUpdatesChanged(EnableMachineStatusUpdates); } } - protected virtual void ApplyContinuousChannelsConfiguration() - { - OnEnableDiagnosticsChanged(EnableDiagnostics); - OnEnableEmbeddedDebuggingChanged(EnableEmbeddedDebugging); - OnEnableEventsNotification(EnableEventsNotification); - OnEnableApplicationLogsChanged(EnableApplicationLogs); - OnEnableMachineStatusUpdatesChanged(EnableMachineStatusUpdates); - OnEnableInkFillingStatus(EnableInkFillingStatus); - //TODO: Uncomment this only when Machine Studio enables automatic thread loading (ExternalBridgeTCPClient). - //OnEnableAutomaticThreadLoadingChanged(EnableAutomaticThreadLoading); - } - - protected async void OnEnableApplicationLogsChanged(bool value) + private async void OnEnableApplicationLogsChanged(bool value) { if (value && State == TransportComponentState.Connected && !_logs_sent) { @@ -215,13 +145,14 @@ namespace Tango.Integration.ExternalBridge bool responseLogged = false; _logs_sent = true; - SendContinuousRequest<StartApplicationLogsRequest, StartApplicationLogsResponse>(request, new TransportContinuousRequestConfig() { ShouldLog = true }).ObserveOn(new NewThreadScheduler()) + SendContinuousRequest<StartApplicationLogsRequest, StartApplicationLogsResponse>(request).ObserveOn(new NewThreadScheduler()) .Subscribe ( (response) => { if (!responseLogged) { + LogResponseReceived(response.Message); responseLogged = true; } @@ -230,11 +161,18 @@ namespace Tango.Integration.ExternalBridge (ex) => { _logs_sent = false; + + if (!(ex is ContinuousResponseAbortedException)) + { + LogRequestFailed(request, ex); + } }, () => { _logs_sent = false; }); + + LogRequestSent(request); } else if (_logs_sent) { @@ -246,9 +184,14 @@ namespace Tango.Integration.ExternalBridge try { - var res = await SendRequest<StopApplicationLogsRequest, StopApplicationLogsResponse>(req, new TransportRequestConfig() { ShouldLog = true }); + LogRequestSent(req); + var res = await SendRequest<StopApplicationLogsRequest, StopApplicationLogsResponse>(req); + LogResponseReceived(res.Message); + } + catch (Exception ex) + { + LogRequestFailed(req, ex); } - catch { } } } } @@ -261,13 +204,7 @@ namespace Tango.Integration.ExternalBridge { LogItemBase log = LogItemBase.Deserialize(response.Message.LogItem.ToArray()); log.LogObject = "External Bridge"; - - if (InjectApplicationLogsToDefaultLogManager) - { - LogManager.Log(log); - } - - ApplicationLogAvailable?.Invoke(this, log); + LogManager.Log(log); } } catch (Exception ex) @@ -281,32 +218,27 @@ namespace Tango.Integration.ExternalBridge if (State == TransportComponentState.Connected) { ExternalBridgeLogoutRequest request = new ExternalBridgeLogoutRequest(); + LogRequestSent(request); try { - var response = await SendRequest<ExternalBridgeLogoutRequest, ExternalBridgeLogoutResponse>(request, new TransportRequestConfig() { ShouldLog = true }); - } - catch { } + var response = await SendRequest<ExternalBridgeLogoutRequest, ExternalBridgeLogoutResponse>(request); + LogResponseReceived(response.Message); - Status = MachineStatuses.Standby; + Status = MachineStatuses.Standby; + } + catch (Exception ex) + { + LogRequestFailed(request, ex); + } } State = TransportComponentState.Disconnected; - - NotifyContinuousRequestMessagesDisconnection(); - - SessionLogger.EndSession(); - if (Adapter != null) { await Adapter.Disconnect(); } - LogManager.Log($"{ComponentName} disconnected."); - } - - internal ExternalBridgeTcpClient() - { - + LogManager.Log("External Bridge TCP client disconnected."); } /// <summary> @@ -316,34 +248,13 @@ namespace Tango.Integration.ExternalBridge /// <param name="ipAddress">The machine IP address.</param> public ExternalBridgeTcpClient(String serialNumber, String ipAddress) { - ComponentName = $"External Bridge TCP Client {_component_counter++}"; SerialNumber = serialNumber; - - if (ObservablesStaticCollections.Instance.IsInitialized) - { - Machine = ObservablesStaticCollections.Instance.Machines.SingleOrDefault(x => x.SerialNumber == serialNumber); - } - IPAddress = ipAddress; - KeepAliveTimeout = TimeSpan.FromSeconds(5); - KeepAliveRetries = 2; - UseKeepAlive = false; - EnableDiagnostics = true; - - Adapter = new TcpTransportAdapter(IPAddress, SettingsManager.Default.GetOrCreate<IntegrationSettings>().ExternalBridgeServicePort); - } - - public ExternalBridgeTcpClient(Machine machine, String ipAddress) - { - ComponentName = $"External Bridge TCP Client {_component_counter++}"; - Machine = machine; - SerialNumber = Machine.SerialNumber; + Machine = ObservablesStaticCollections.Instance.Machines.SingleOrDefault(x => x.SerialNumber == serialNumber); IPAddress = ipAddress; KeepAliveTimeout = TimeSpan.FromSeconds(5); KeepAliveRetries = 2; UseKeepAlive = false; EnableDiagnostics = true; - - Adapter = new TcpTransportAdapter(IPAddress, SettingsManager.Default.GetOrCreate<IntegrationSettings>().ExternalBridgeServicePort); } /// <summary> @@ -361,22 +272,13 @@ namespace Tango.Integration.ExternalBridge /// Called when a new request has been received. /// </summary> /// <param name="request">The request.</param> - protected async override void OnRequestReceived(RequestReceivedEventArgs e) + protected async override void OnRequestReceived(MessageContainer request) { - base.OnRequestReceived(e); - - var container = e.Container; + base.OnRequestReceived(request); - if (container.Type == MessageType.ExternalBridgeLogoutRequest) + if (request.Type == MessageType.ExternalBridgeLogoutRequest) { - try - { - await SendResponse<ExternalBridgeLogoutResponse>(new ExternalBridgeLogoutResponse(), container.Token); - } - catch { } - - await Task.Delay(2000); - + await SendResponse<ExternalBridgeLogoutResponse>(new ExternalBridgeLogoutResponse(), request.Token); try { State = TransportComponentState.Disconnected; @@ -387,19 +289,10 @@ namespace Tango.Integration.ExternalBridge LogManager.Log("External Bridge TCP client disconnected by the remote host."); } catch { } - - SessionLogger.EndSession(); SessionClosed?.Invoke(this, new EventArgs()); - - NotifyContinuousRequestMessagesDisconnection(); } } - protected override void OnMachineStateChanged(MachineState state) - { - //Do Nothing... - } - /// <summary> /// Occurs when the remote host has closed the session. /// </summary> @@ -408,7 +301,7 @@ namespace Tango.Integration.ExternalBridge /// <summary> /// Gets the database machine associated with this client. /// </summary> - public Machine Machine { get; protected set; } + public Machine Machine { get; private set; } /// <summary> /// Sets the database machine. |
