aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeSignalRClient.cs
blob: e2c4a571796f8012cf10bb6008463dae688adc97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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;
        }

        /// <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>
        /// <returns></returns>
        /// <exception cref="AuthenticationException"></exception>
        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<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)
                        {
                            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();
            }
        }
    }
}