Exemple #1
0
        private MqttIpcServer(MqttIpcServerConfiguration configuration, IIpcSerializer serializer, IIpcPacketRouter router, IIpcPacketHandlersContainer packetHandlersContainer)
        {
            _serializer = serializer;
            _router     = router;
            _packetHandlersContainer = packetHandlersContainer;
            string clientName = configuration.ClientName;
            string endPoint   = configuration.EndPoint;


            _client        = new MqttFactory().CreateManagedMqttClient(new MqttNetLogger(clientName));
            _packetFactory = new PacketContainerFactory();
            _queues        = new HashSet <string>();
            _packetHandlersContainer.Registered   += (sender, type) => CheckRouting(type).ConfigureAwait(false).GetAwaiter().GetResult();
            _packetHandlersContainer.Unregistered += (sender, type) =>
            {
                IRoutingInformation infos = _router.GetRoutingInformationsAsync(type).ConfigureAwait(false).GetAwaiter().GetResult();
                _client.UnsubscribeAsync(infos.IncomingTopic).ConfigureAwait(false).GetAwaiter().GetResult();
            };
            ManagedMqttClientOptions options = new ManagedMqttClientOptionsBuilder()
                                               .WithAutoReconnectDelay(TimeSpan.FromSeconds(5))
                                               .WithClientOptions(new MqttClientOptionsBuilder()
                                                                  .WithClientId(clientName)
                                                                  .WithTcpServer(endPoint)
                                                                  .Build())
                                               .Build();

            _client.ApplicationMessageReceived += (sender, args) => OnMessage(args.ClientId, args.ApplicationMessage);
            _client.StartAsync(options).ConfigureAwait(false).GetAwaiter().GetResult();
            _client.Connected    += (sender, args) => _log.Info($"[CONNECTED] {clientName} is connected on MQTT Broker {endPoint}");
            _client.Disconnected += (sender, args) => _log.Info($"[DISCONNECTED] {clientName} has been disconnected on MQTT Broker {endPoint}");
        }
Exemple #2
0
        private async Task <IRoutingInformation> CheckRouting(Type type)
        {
            IRoutingInformation routingInfos = await _router.GetRoutingInformationsAsync(type);

            if (string.IsNullOrEmpty(routingInfos.OutgoingTopic) || _responsesQueues.Contains(routingInfos.OutgoingTopic))
            {
                return(routingInfos);
            }

            await _client.SubscribeAsync(routingInfos.OutgoingTopic);

            _responsesQueues.Add(routingInfos.OutgoingTopic);
            return(routingInfos);
        }
Exemple #3
0
        private async Task <IRoutingInformation> CheckRouting(Type type)
        {
            IRoutingInformation routingInfos = await _router.GetRoutingInformationsAsync(type);

            if (string.IsNullOrEmpty(routingInfos.IncomingTopic) || _queues.Contains(routingInfos.IncomingTopic))
            {
                return(routingInfos);
            }

            await _client.SubscribeAsync(routingInfos.IncomingTopic);

            _queues.Add(routingInfos.IncomingTopic);
            _log.Info($"Subscribed to topic : {routingInfos.IncomingTopic}");
            return(routingInfos);
        }