Exemplo n.º 1
0
        public void StartPeerMonitoring_SendsStartPeerMonitoringMessage()
        {
            var peer   = new Node("tcp://127.0.0.1:8080", Guid.NewGuid().ToByteArray());
            var health = new Health
            {
                Uri = "tcp://127.0.0.2:9090",
                HeartBeatInterval = TimeSpan.FromSeconds(3)
            };

            //
            clusterHealthMonitor.StartPeerMonitoring(peer, health);
            //
            Func <IMessage, bool> isStartMonitoringMessage = msg =>
            {
                if (msg.Equals(KinoMessages.StartPeerMonitoring))
                {
                    var payload = msg.GetPayload <StartPeerMonitoringMessage>();
                    Assert.IsTrue(Unsafe.ArraysEqual(peer.SocketIdentity, payload.SocketIdentity));
                    Assert.AreEqual(peer.Uri.ToSocketAddress(), payload.Uri);
                    Assert.AreEqual(health.Uri, payload.Health.Uri);
                    Assert.AreEqual(health.HeartBeatInterval, payload.Health.HeartBeatInterval);
                    return(true);
                }

                return(false);
            };

            multiplexingSocket.Verify(m => m.Send(It.Is <IMessage>(msg => isStartMonitoringMessage(msg))), Times.Once);
        }
        public void Handle(IMessage message, ISocket _)
        {
            if (securityProvider.DomainIsAllowed(message.Domain))
            {
                message.As <Message>().VerifySignature(securityProvider);

                var payload = message.GetPayload <RegisterExternalMessageRouteMessage>();
                var peer    = new Node(new Uri(payload.Uri), payload.NodeIdentity);
                var health  = new Health
                {
                    Uri = payload.Health.Uri,
                    HeartBeatInterval = payload.Health.HeartBeatInterval
                };
                var peerAddedForMonitoring = false;
                foreach (var route in payload.Routes)
                {
                    var receiver      = new ReceiverIdentifier(route.ReceiverIdentity);
                    var messageRoutes = receiver.IsMessageHub()
                                            ? new MessageRoute {
                        Receiver = receiver
                    }.ToEnumerable()
                                            : route.MessageContracts.Select(mc => new MessageRoute
                    {
                        Receiver = receiver,
                        Message  = new MessageIdentifier(mc.Identity, mc.Version, mc.Partition)
                    });
                    foreach (var messageRoute in messageRoutes)
                    {
                        try
                        {
                            //NOTE: Keep the order in if(...), hence MessageHub is not registered to receive any specific message
                            if (receiver.IsMessageHub() || securityProvider.GetDomain(messageRoute.Message.Identity) == message.Domain)
                            {
                                if (!peerAddedForMonitoring)
                                {
                                    clusterHealthMonitor.AddPeer(peer, health);
                                    peerAddedForMonitoring = true;
                                }

                                externalRoutingTable.AddMessageRoute(new ExternalRouteRegistration
                                {
                                    Route  = messageRoute,
                                    Peer   = peer,
                                    Health = health
                                });
                            }
                            else
                            {
                                logger.Warn($"MessageIdentity {messageRoute.Message} doesn't belong to requested Domain {message.Domain}!");
                            }
                        }
                        catch (Exception err)
                        {
                            logger.Error(err);
                        }
                    }
                }
            }
        }