Exemplo n.º 1
0
        private void ProcessAsyncResponses(CancellationToken token)
        {
            try
            {
                foreach (var messageContext in asyncQueue.GetConsumingEnumerable(token))
                {
                    try
                    {
                        foreach (var messageOut in messageContext.OutMessages.Cast <Message>())
                        {
                            messageOut.SetDomain(securityProvider.GetDomain(messageOut.Identity));
                            messageOut.RegisterCallbackPoint(messageContext.CallbackReceiverNodeIdentity,
                                                             messageContext.CallbackReceiverIdentity,
                                                             messageContext.CallbackPoint,
                                                             messageContext.CallbackKey);
                            messageOut.SetCorrelationId(messageContext.CorrelationId);
                            messageOut.CopyMessageRouting(messageContext.MessageHops);
                            messageOut.TraceOptions |= messageContext.TraceOptions;

                            localRouterSocket.Send(messageOut);

                            ResponseSent(messageOut, false);
                        }
                    }
                    catch (Exception err)
                    {
                        logger.Error(err);
                    }
                }
            }
            finally
            {
                asyncQueue.Dispose();
            }
        }
Exemplo n.º 2
0
 private IEnumerable <MessageRouteDomainMap> GetActors(IEnumerable <MessageRoute> messageRoutes)
 => messageRoutes.Where(mr => !mr.Receiver.IsSet() || mr.Receiver.IsActor())
 .Select(mr => new MessageRouteDomainMap
 {
     MessageRoute = mr,
     Domain       = securityProvider.GetDomain(mr.Message.Identity)
 });
Exemplo n.º 3
0
 private void CheckPeerConnection(ReceiverIdentifier nodeIdentifier, ClusterMemberMeta meta)
 {
     if (!meta.ConnectionEstablished)
     {
         using (var socket = socketFactory.CreateRouterSocket())
         {
             var uri = new Uri(meta.ScaleOutUri);
             try
             {
                 socket.SetMandatoryRouting();
                 socket.Connect(uri, waitUntilConnected: true);
                 var message = Message.Create(new PingMessage())
                               .As <Message>();
                 message.SetDomain(securityProvider.GetDomain(KinoMessages.Ping.Identity));
                 message.SetSocketIdentity(nodeIdentifier.Identity);
                 message.SignMessage(securityProvider);
                 socket.SendMessage(message);
                 socket.Disconnect(uri);
                 meta.LastKnownHeartBeat = DateTime.UtcNow;
             }
             catch (Exception err)
             {
                 routerLocalSocket.Send(Message.Create(new UnregisterUnreachableNodeMessage {
                     ReceiverNodeIdentity = nodeIdentifier.Identity
                 }));
                 logger.Warn($"Failed trying to check connectivity to node {nodeIdentifier}@{uri.ToSocketAddress()}. Peer deletion scheduled. {err}");
             }
         }
     }
 }
Exemplo n.º 4
0
        private async Task DispatchMessage(IMessage messageIn)
        {
            var messageHandler = actorHandlerMap.Get(messageIn);

            if (messageHandler != null)
            {
                try
                {
                    var response = (await messageHandler(messageIn)).Messages;

                    foreach (var messageOut in response)
                    {
                        messageRouter.Route(SetOutMessageProperties(messageOut));
                    }
                }
                catch (Exception err)
                {
                    var payload = err.BuildExceptionMessage();

                    var messageOut = Message.Create(payload, MessageIdentifiers.Exception);
                    messageRouter.Route(SetOutMessageProperties(messageOut));
                }
            }
            else
            {
                throw new MessageHandlerNotFoundException(messageIn);
            }

            IMessage SetOutMessageProperties(IMessage messageOut)
            {
                messageOut.Domain = securityProvider.GetDomain(messageOut.Identity);

                return(messageOut.CopyMessageProperties(messageIn));
            }
        }
        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);
                        }
                    }
                }
            }
        }
Exemplo n.º 6
0
        private void SendReceiptConfirmation(Message messageIn)
        {
            var messageOut = Message.Create(ReceiptConfirmationMessage.Instance)
                             .As <Message>();

            messageOut.SetDomain(securityProvider.GetDomain(KinoMessages.ReceiptConfirmation.Identity));

            messageOut.RegisterCallbackPoint(messageIn.CallbackReceiverNodeIdentity,
                                             messageIn.CallbackReceiverIdentity,
                                             KinoMessages.ReceiptConfirmation,
                                             messageIn.CallbackKey);
            messageOut.SetCorrelationId(messageIn.CorrelationId);
            messageOut.CopyMessageRouting(messageIn.GetMessageRouting());
            messageOut.TraceOptions |= messageIn.TraceOptions;

            localRouterSocket.Send(messageOut);
        }
Exemplo n.º 7
0
 private IEnumerable <Cluster.MessageRoute> GetActorRoutes(string domain, InternalRouting routes)
 => routes.Actors.SelectMany(r => r.Actors
                             .Where(a => !a.LocalRegistration)
                             .Select(a => new Cluster.MessageRoute
 {
     Receiver = new ReceiverIdentifier(a.Identity),
     Message  = r.Message
 }))
 .Where(r => securityProvider.GetDomain(r.Message.Identity) == domain);
 private IEnumerable <MessageRouteDomainMap> GetActors(InternalRouteRegistration routeRegistration)
 => routeRegistration.ToEnumerable()
 .Where(r => r.ReceiverIdentifier.IsActor())
 .SelectMany(a => a.MessageContracts
             .Where(mc => !mc.KeepRegistrationLocal)
             .Select(mc => new MessageRouteDomainMap
 {
     MessageRoute = new Cluster.MessageRoute
     {
         Receiver = a.ReceiverIdentifier,
         Message  = mc.Message
     },
     Domain = securityProvider.GetDomain(mc.Message.Identity)
 }));
Exemplo n.º 9
0
        public void MessageSignature_IsConsistentlyTransferredViaMultipartMessage()
        {
            var simpleMessage  = new SimpleMessage();
            var securityDomain = securityProvider.GetDomain(simpleMessage.Identity);
            var message        = Message.CreateFlowStartMessage(simpleMessage).As <Message>();

            message.SetDomain(securityDomain);
            message.SignMessage(securityProvider);

            var multipart = new MultipartMessage(message);

            message = Message.FromMultipartMessage(multipart);

            message.VerifySignature(securityProvider);
        }
Exemplo n.º 10
0
        public void Handle(IMessage message, ISocket _)
        {
            if (securityProvider.DomainIsAllowed(message.Domain))
            {
                message.As <Message>().VerifySignature(securityProvider);

                var payload        = message.GetPayload <DiscoverMessageRouteMessage>();
                var internalRoutes = internalRoutingTable.GetAllRoutes();
                var messageRoutes  = new List <Cluster.MessageRoute>();
                if (payload.ReceiverIdentity.IsMessageHub())
                {
                    messageRoutes.AddRange(internalRoutes.MessageHubs
                                           .Where(mh => !mh.LocalRegistration &&
                                                  mh.MessageHub.Equals(new ReceiverIdentifier(payload.ReceiverIdentity)))
                                           .Select(mh => new Cluster.MessageRoute {
                        Receiver = mh.MessageHub
                    }));
                }
                else
                {
                    var messageContract = new MessageIdentifier(payload.MessageContract.Identity, payload.MessageContract.Version, payload.MessageContract.Partition);
                    messageRoutes.AddRange(internalRoutes.Actors
                                           .Where(r => r.Message.Equals(messageContract))
                                           .SelectMany(r => r.Actors
                                                       .Where(a => !a.LocalRegistration)
                                                       .Select(a => new Cluster.MessageRoute
                    {
                        Receiver = a,
                        Message  = messageContract
                    })));
                }
                foreach (var messageRoute in messageRoutes)
                {
                    var domains = messageRoute.Receiver.IsMessageHub()
                                      ? securityProvider.GetAllowedDomains()
                                      : new[] { securityProvider.GetDomain(messageRoute.Message.Identity) };
                    if (domains.Contains(message.Domain))
                    {
                        clusterMonitor.RegisterSelf(messageRoute.ToEnumerable(), message.Domain);
                    }
                    else
                    {
                        logger.Warn($"MessageIdentity {messageRoute.Message} doesn't belong to requested Domain {message.Domain}!");
                    }
                }
            }
        }
Exemplo n.º 11
0
 private IEnumerable <ExternalRouteRemoval> GetUnregistrationRoutes(UnregisterMessageRouteMessage payload, string domain)
 {
     foreach (var route in payload.Routes.SelectMany(r => r.MessageContracts.Select(mc => new MessageRoute
     {
         Receiver = new ReceiverIdentifier(r.ReceiverIdentity),
         Message = new MessageIdentifier(mc.Identity, mc.Version, mc.Partition)
     })))
     {
         if (route.Receiver.IsMessageHub() || securityProvider.GetDomain(route.Message.Identity) == domain)
         {
             yield return(new ExternalRouteRemoval
             {
                 Route = route,
                 NodeIdentifier = payload.ReceiverNodeIdentity
             });
         }
         else
         {
             logger.Warn($"MessageIdentity {route.Message} doesn't belong to requested Domain {domain}!");
         }
     }
 }
Exemplo n.º 12
0
 private void SendDiscoverMessageRouteMessage(MessageRoute messageRoute)
 {
     try
     {
         var scaleOutAddress = scaleOutConfigurationProvider.GetScaleOutAddress();
         var domains         = messageRoute.Receiver.IsMessageHub()
                           ? securityProvider.GetAllowedDomains()
                           : new[] { securityProvider.GetDomain(messageRoute.Message.Identity) };
         foreach (var domain in domains)
         {
             var message = Message.Create(new DiscoverMessageRouteMessage
             {
                 RequestorNodeIdentity = scaleOutAddress.Identity,
                 RequestorUri          = scaleOutAddress.Uri.ToSocketAddress(),
                 ReceiverIdentity      = messageRoute.Receiver.IsMessageHub()
                                                                     ? messageRoute.Receiver.Identity
                                                                     : null,
                 MessageContract = messageRoute.Message != null
                                                                    ? new MessageContract
                 {
                     Version   = messageRoute.Message.Version,
                     Identity  = messageRoute.Message.Identity,
                     Partition = messageRoute.Message.Partition
                 }
                                                                    : null
             })
                           .As <Message>();
             message.SetDomain(domain);
             message.SignMessage(securityProvider);
             autoDiscoverySender.EnqueueMessage(message);
         }
     }
     catch (SecurityException err)
     {
         logger.Error(err);
     }
 }
Exemplo n.º 13
0
        private void CallbackException(Exception err, Message messageIn)
        {
            var messageOut = Message.Create(new ExceptionMessage
            {
                Exception  = err,
                StackTrace = err.StackTrace
            })
                             .As <Message>();

            messageOut.SetDomain(securityProvider.GetDomain(KinoMessages.Exception.Identity));

            if (messageIn != null)
            {
                messageOut.RegisterCallbackPoint(messageIn.CallbackReceiverNodeIdentity,
                                                 messageIn.CallbackReceiverIdentity,
                                                 messageIn.CallbackPoint,
                                                 messageIn.CallbackKey);
                messageOut.SetCorrelationId(messageIn.CorrelationId);
                messageOut.CopyMessageRouting(messageIn.GetMessageRouting());
                messageOut.TraceOptions |= messageIn.TraceOptions;
            }

            localRouterSocket.Send(messageOut);
        }
Exemplo n.º 14
0
        public IPromise EnqueueRequest(IMessage message, CallbackPoint callbackPoint)
        {
            message.As <Message>().SetDomain(securityProvider.GetDomain(message.Identity));

            return(InternalEnqueueRequest(message, callbackPoint));
        }
Exemplo n.º 15
0
 public void SendOneWay(IMessage message)
 {
     message.Domain = securityProvider.GetDomain(message.Identity);
     messageRouter.Route(message);
 }