Exemplo n.º 1
0
 private async Task SendAsync(PacketContainer container, IRoutingInformation routingInfos)
 {
     await _client.PublishAsync(builder => builder
                                .WithPayload(_serializer.Serialize(container))
                                .WithTopic(routingInfos.OutgoingTopic)
                                .WithExactlyOnceQoS());
 }
Exemplo n.º 2
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}");
        }
Exemplo n.º 3
0
        public async Task ResponseAsync <T>(T response, Type packetType) where T : ISyncRpcResponse
        {
            PacketContainer     container    = _packetFactory.ToPacket(response);
            IRoutingInformation routingInfos = await CheckRouting(packetType);

            await SendAsync(container, routingInfos);
        }
Exemplo n.º 4
0
        public async Task <IRoutingInformation> RegisterAsync(Type type)
        {
            if (type.IsGenericType)
            {
                return(await RegisterGenericRequestAsync(type));
            }

            string requestTopic  = "";
            string responseTopic = "";

            if (type.GetInterfaces().Any(s => s == typeof(ISyncRpcRequest)))
            {
                requestTopic  = type.FullName.ToLower() + RequestTopicSuffix;
                responseTopic = type.FullName.ToLower() + ResponseTopicSuffix;
            }
            else if (type.GetInterfaces().Any(s => s == typeof(IAsyncRpcRequest)))
            {
                requestTopic = type.FullName.ToLower() + PacketTopicSuffix;
            }
            else if (type.IsNested && type.GetInterfaces().Any(s => s == typeof(ISyncRpcResponse)))
            {
                responseTopic = type.FullName.ToLower().Replace('+', '.');
            }

            IRoutingInformation routingInfos = await _routingInformationFactory.Create(requestTopic, responseTopic);

            await RegisterAsync(type, routingInfos);

            return(routingInfos);
        }
Exemplo n.º 5
0
        private async Task <IRoutingInformation> RegisterGenericRequestAsync(Type type)
        {
            // in case they want to register "runtime" generics objects
            Type evaluatedType = type.BaseType ?? type;

            var newTopic = new StringBuilder();

            newTopic.Append(type.FullName);

            foreach (Type i in evaluatedType.GenericTypeArguments)
            {
                newTopic.AppendFormat("/{0}", i.Name.ToLower());
            }

            string requestTopic  = newTopic.ToString();
            string responseTopic = "";

            if (type.GetInterfaces().Any(s => s == typeof(ISyncRpcRequest)))
            {
                requestTopic += RequestTopicSuffix;
                responseTopic = newTopic + ResponseTopicSuffix;
            }
            else if (type.GetInterfaces().Any(s => s == typeof(IAsyncRpcRequest)))
            {
                requestTopic += PacketTopicSuffix;
            }

            IRoutingInformation routingInfos = await _routingInformationFactory.Create(requestTopic, responseTopic);

            await RegisterAsync(type, routingInfos);

            return(routingInfos);
        }
Exemplo n.º 6
0
        public void Register(Type type, IRoutingInformation routingInformation)
        {
            if (_infos.ContainsKey(type))
            {
                return;
            }

            _infos.Add(type, routingInformation);
        }
Exemplo n.º 7
0
        public async Task ResponseAsync <T>(T response, Type packetType) where T : IIpcResponse
        {
            PacketContainer container = _packetFactory.ToPacket <T>(response);

#if DEBUG
            _log.Debug($"[SENT] Packet [{container.Type}]");
#endif
            IRoutingInformation routingInfos = await CheckRouting(packetType);
            await SendAsync(container, routingInfos);
        }
Exemplo n.º 8
0
        public Task RegisterAsync(Type type, IRoutingInformation routingInformation)
        {
            if (_infos.ContainsKey(type))
            {
                return(Task.CompletedTask);
            }

            _infos.Add(type, routingInformation);
            return(Task.CompletedTask);
        }
Exemplo n.º 9
0
        private async Task SendAsync(PacketContainer container)
        {
            IRoutingInformation infos = await CheckRouting(container.Type);

            await _client.PublishAsync(builder =>
                                       builder
                                       .WithPayload(_serializer.Serialize(container))
                                       .WithTopic(infos.IncomingTopic)
                                       .WithExactlyOnceQoS()
                                       );
        }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
0
        private async Task SendAsync <T>(PacketContainer container)
        {
            IRoutingInformation infos = GetRoutingInformations <T>();

            MessageSent.WithLabels(_options.ClientOptions.ClientId, infos.Topic).Inc();
            await _client.PublishAsync(builder =>
                                       builder
                                       .WithPayload(_serializer.Serialize(container))
                                       .WithTopic(infos.Topic)
                                       .WithExactlyOnceQoS()
                                       );

            Console.WriteLine("Sent message to topic : " + infos.Topic);
        }
Exemplo n.º 12
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);
        }
Exemplo n.º 13
0
        private async Task <IRoutingInformation> CheckRouting(Type type)
        {
            IRoutingInformation routingInfos = _router.GetRoutingInformations(type);

            Console.WriteLine($"[SERVER] Adding topic : {routingInfos.Topic}");
            if (string.IsNullOrEmpty(routingInfos.Topic) || _queues.Contains(routingInfos.Topic))
            {
                return(routingInfos);
            }

            await _client.SubscribeAsync(routingInfos.Topic);

            _queues.Add(routingInfos.Topic);
            return(routingInfos);
        }
Exemplo n.º 14
0
        public IRoutingInformation Register(Type type)
        {
            if (type.IsGenericType)
            {
                return(RegisterGenericRequestAsync(type));
            }

            string requestTopic = "";

            if (type.GetInterfaces().Any(s => s == typeof(IAsyncRpcRequest)))
            {
                requestTopic = type.FullName.ToHypenCase();
            }

            IRoutingInformation routingInfos = _routingInformationFactory.Create(requestTopic);

            Register(type, routingInfos);
            return(routingInfos);
        }
Exemplo n.º 15
0
        private IRoutingInformation RegisterGenericRequestAsync(Type type)
        {
            // in case they want to register "runtime" generics objects
            Type evaluatedType = type.BaseType ?? type;

            var newTopic = new StringBuilder();

            newTopic.Append(type.GetFriendlyName().ToHypenCase());
            foreach (Type i in evaluatedType.GenericTypeArguments)
            {
                newTopic.AppendFormat("/{0}", i.Name.ToHypenCase());
            }

            string requestTopic = newTopic.ToString();


            IRoutingInformation routingInfos = _routingInformationFactory.Create(requestTopic);

            Register(type, routingInfos);
            return(routingInfos);
        }
Exemplo n.º 16
0
        private void HandlersContainer_OnUnregistered(object sender, Type type)
        {
            IRoutingInformation infos = _router.GetRoutingInformations(type);

            _client.UnsubscribeAsync(infos.Topic).ConfigureAwait(false).GetAwaiter().GetResult();
        }