private void BuildConnectionWithAgent(XNodeConfiguration nodeConfig)
        {
            string serviceUrl = CheckAndFixServiceUrl(nodeConfig.ServiceUrl);
            string location   = $"{serviceUrl}/realtime/v2/storage";

            _connection = new HubConnectionBuilder()
                          .AddJsonProtocol(opts =>
            {
                opts.PayloadSerializerOptions.Converters.Add(new JsonStringEnumConverter());
            })
                          .WithUrl($"{serviceUrl}/realtime/v2/storage", option =>
            {
                var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
                if (env == "Development")
                {
                    option.HttpMessageHandlerFactory = (message) =>
                    {
                        if (message is HttpClientHandler httpClientHandler)
                        {
                            httpClientHandler.ServerCertificateCustomValidationCallback +=
                                (sender, certificate, chain, sslPolicyErrors) => { return(true); }
                        }
                        ;
                        return(message);
                    };
                }
                else
                {
                    option.HttpMessageHandlerFactory = (message) =>
                    {
                        if (message is HttpClientHandler httpClientHandler)
                        {
                            httpClientHandler.ClientCertificateOptions = ClientCertificateOption.Manual;
                            httpClientHandler.SslProtocols             = SslProtocols.Tls12;
                            var certLocation = Path.Combine(SystemLocations.GetConfigCertificateDirectory(), nodeConfig.CertificateFile);
                            httpClientHandler.ClientCertificates.Add(new X509Certificate2(certLocation, nodeConfig.CertificatePassword));
                        }

                        return(message);
                    };
                }

                option.Headers["x-andyx-storage-name"] = dataStorageConfig.Name;

                option.Headers["x-andyx-storage-username"] = nodeConfig.Username;
                option.Headers["x-andyx-storage-password"] = nodeConfig.Password;

                option.Headers["x-andyx-storage-status"]             = dataStorageConfig.Status.ToString();
                option.Headers["x-andyx-storage-agent-id"]           = agentId;
                option.Headers["x-andyx-storage-agent-max"]          = agentConfiguration.MaxNumber.ToString();
                option.Headers["x-andyx-storage-agent-min"]          = agentConfiguration.MinNumber.ToString();
                option.Headers["x-andyx-storage-agent-loadbalanced"] = agentConfiguration.LoadBalanced.ToString();
            })
                          .WithAutomaticReconnect()
                          .Build();
        }
Exemple #2
0
        public XNodeEventService(ILogger <SystemService> logger,
                                 string agentId,
                                 XNodeConfiguration nodeConfig,
                                 DataStorageConfiguration dataStorageConfig,
                                 AgentConfiguration agentConfiguration,
                                 IXNodeConnectionRepository xNodeConnectionRepository,
                                 TenantIOService tenantIOService,
                                 ProducerIOService producerIOService,
                                 ConsumerIOService consumerIOService,
                                 MessageIOService messageIOService)
        {
            this.logger = logger;
            this.xNodeConnectionRepository = xNodeConnectionRepository;
            this.tenantIOService           = tenantIOService;
            this.producerIOService         = producerIOService;
            this.consumerIOService         = consumerIOService;
            this.messageIOService          = messageIOService;
            this.agentId    = agentId;
            this.nodeConfig = nodeConfig;

            var provider = new XNodeConnectionProvider(nodeConfig, dataStorageConfig, agentConfiguration, agentId);

            _connection = provider.GetHubConnection();

            _connection.On <AgentConnectedArgs>("StorageConnected", connectedArgs => StorageConnected?.Invoke(connectedArgs));
            _connection.On <AgentDisconnectedArgs>("StorageDisconnected", disconnectedArgs => StorageDisconnected?.Invoke(disconnectedArgs));

            _connection.On <TenantCreatedArgs>("TenantCreated", tenantCreated => TenantCreated?.Invoke(tenantCreated));
            _connection.On <TenantUpdatedArgs>("TenantUpdated", tenantUpdated => TenantUpdated?.Invoke(tenantUpdated));

            _connection.On <ProductCreatedArgs>("ProductCreated", productCreated => ProductCreated?.Invoke(productCreated));
            _connection.On <ProductUpdatedArgs>("ProductUpdated", productUpdated => ProductUpdated?.Invoke(productUpdated));

            _connection.On <ComponentCreatedArgs>("ComponentCreated", componentCreated => ComponentCreated?.Invoke(componentCreated));
            _connection.On <ComponentUpdatedArgs>("ComponentUpdated", componentUpdated => ComponentUpdated?.Invoke(componentUpdated));

            _connection.On <TopicCreatedArgs>("TopicCreated", topicCreated => TopicCreated?.Invoke(topicCreated));
            _connection.On <TopicUpdatedArgs>("TopicUpdated", topicUpdated => TopicUpdated?.Invoke(topicUpdated));

            _connection.On <ProducerConnectedArgs>("ProducerConnected", producerConnected => ProducerConnected?.Invoke(producerConnected));
            _connection.On <ProducerDisconnectedArgs>("ProducerDisconnected", producerDisconnected => ProducerDisconnected?.Invoke(producerDisconnected));

            _connection.On <ConsumerConnectedArgs>("ConsumerConnected", consumerConnected => ConsumerConnected?.Invoke(consumerConnected));
            _connection.On <ConsumerDisconnectedArgs>("ConsumerDisconnected", consumerDisconnected => ConsumerDisconnected?.Invoke(consumerDisconnected));
            _connection.On <ConsumerConnectedArgs>("ConsumerUnacknowledgedMessagesRequested", consumerConnected => ConsumerUnacknowledgedMessagesRequested?.Invoke(consumerConnected));
            _connection.On <MessageAcknowledgedArgs>("MessageAcknowledged", messageAcked => MessageAcknowledged?.Invoke(messageAcked));

            _connection.On <MessageStoredArgs>("MessageStored", msgStored => MessageStored?.Invoke(msgStored));

            InitializeEventHandlers();

            ConnectAsync();

            xNodeConnectionRepository.AddService(nodeConfig.ServiceUrl, agentId, this);
        }
 public XNodeConnectionProvider(XNodeConfiguration nodeConfig,
                                DataStorageConfiguration dataStorageConfig,
                                AgentConfiguration agentConfiguration,
                                string agentId)
 {
     this.nodeConfig         = nodeConfig;
     this.dataStorageConfig  = dataStorageConfig;
     this.agentConfiguration = agentConfiguration;
     this.agentId            = agentId;
     ConnectToXNode();
 }