Пример #1
0
        public void TestPayload()
        {
            using (var stream = new MemoryStream())
            {
                var source = new ForwardedMulticastData(
                    "host",
                    "user",
                    "__admin__",
                    "heartbeat",
                    true,
                    new DataPacket[]
                {
                    new DataPacket(
                        new HashSet <int> {
                        1
                    },
                        new byte[] { 1, 2, 3, 4, 5, 6 })
                });

                source.Write(new DataWriter(stream));
                stream.Seek(0, SeekOrigin.Begin);
                var dest = Message.Read(new DataReader(stream));
                Assert.AreEqual(source, dest);
            }
        }
Пример #2
0
        public void OnAuthorizationResponse(object?sender, AuthorizationResponseEventArg args)
        {
            if (args.Response.IsAuthorizationRequired && (args.Response.Entitlements == null || args.Response.Entitlements.Count == 0))
            {
                // Inform the subscriber that they are not entitled.
                var message = new ForwardedMulticastData(string.Empty, string.Empty, args.Response.Feed, args.Response.Topic, true, null);
                try
                {
                    args.Requester.SendMessage(message);
                }
                catch (Exception error)
                {
                    _logger.LogDebug(error, "Failed to send to {Requester} multi cast message {Message}", args.Requester, message);
                }

                return;
            }

            AddSubscription(
                args.Requester,
                args.Response.Feed,
                args.Response.Topic,
                args.Response.IsAuthorizationRequired,
                args.Response.Entitlements,
                !args.IsInitial);
        }
Пример #3
0
 private void RaiseOnDataOrHeartbeat(ForwardedMulticastData message)
 {
     if (message.Feed == "__admin__" && message.Topic == "heartbeat")
     {
         RaiseOnHeartbeat();
     }
     else
     {
         RaiseOnData(message.User, message.Address, message.Feed, message.Topic, message.Data, message.IsImage);
     }
 }
Пример #4
0
        private void OnStaleTopic(FeedTopic staleFeedTopic)
        {
            // Inform subscribers by sending an image with no data.
            var staleMessage = new ForwardedMulticastData(string.Empty, string.Empty, staleFeedTopic.Feed, staleFeedTopic.Topic, true, null);

            foreach (var subscriber in _repository.GetSubscribersToFeedAndTopic(staleFeedTopic.Feed, staleFeedTopic.Topic).Select(x => x.Key))
            {
                try
                {
                    subscriber.SendMessage(staleMessage);
                }
                catch (Exception error)
                {
                    _logger.LogDebug(error, "Failed to inform {Subscriber} of stale {StaleFeedTopic}", subscriber, staleFeedTopic);
                }
            }
        }
Пример #5
0
        private void OnStaleFeedTopic(FeedTopic staleFeedTopic)
        {
            // Inform subscribers by sending an image with no data.
            var staleMessage = new ForwardedMulticastData(string.Empty, IPAddress.None, staleFeedTopic.Feed, staleFeedTopic.Topic, true, null);

            foreach (var subscriber in _repository.GetSubscribersToFeedAndTopic(staleFeedTopic.Feed, staleFeedTopic.Topic).Select(x => x.Key))
            {
                try
                {
                    subscriber.SendMessage(staleMessage);
                }
                catch (Exception exception)
                {
                    Log.Debug($"Failed to inform {subscriber} of stale {staleFeedTopic}", exception);
                }
            }
        }
Пример #6
0
 public void TestHeartbeat()
 {
     using (var stream = new MemoryStream())
     {
         var source = new ForwardedMulticastData(
             "user",
             "host",
             "__admin__",
             "heartbeat",
             true,
             null);
         source.Write(new DataWriter(stream));
         stream.Seek(0, SeekOrigin.Begin);
         var dest = Message.Read(new DataReader(stream));
         Assert.AreEqual(source, dest);
     }
 }
        public void SendMulticastData(Interactor?publisher, IEnumerable <KeyValuePair <Interactor, AuthorizationInfo> > subscribers, MulticastData multicastData)
        {
            if (!(publisher == null || publisher.HasRole(multicastData.Feed, Role.Publish)))
            {
                _logger.LogWarning("Rejected request from {Publisher} to publish to Feed {Feed}", publisher, multicastData.Feed);
                return;
            }

            if (publisher != null)
            {
                publisher.Metrics.MulticastMessages[multicastData.Feed].Inc();
            }

            foreach (var subscriberAndAuthorizationInfo in subscribers)
            {
                var subscriber    = subscriberAndAuthorizationInfo.Key;
                var authorization = subscriberAndAuthorizationInfo.Value;

                var subscriberMulticastData = new ForwardedMulticastData(
                    publisher?.UserForFeed(multicastData.Feed) ?? "internal",
                    publisher?.HostForFeed(multicastData.Feed) ?? "localhost",
                    multicastData.Feed,
                    multicastData.Topic,
                    multicastData.IsImage,
                    GetAuthorizedData(multicastData.DataPackets, authorization));

                _logger.LogDebug("Sending multicast data from {Publisher} to {Subscriber}: {Message}", publisher, subscriber, subscriberMulticastData);

                if (publisher != null)
                {
                    _repository.AddPublisher(publisher, subscriberMulticastData.Feed, subscriberMulticastData.Topic);
                }

                try
                {
                    subscriber.SendMessage(subscriberMulticastData);
                }
                catch (Exception error)
                {
                    _logger.LogDebug(error, "Failed to send to subscriber {Subscriber} multicast data {Message}", subscriber, subscriberMulticastData);
                }
            }
        }
Пример #8
0
        internal void OnAuthorizationResponse(object sender, AuthorizationResponseEventArg args)
        {
            if (args.Response.IsAuthorizationRequired && (args.Response.Entitlements == null || args.Response.Entitlements.Length == 0))
            {
                var message = new ForwardedMulticastData(string.Empty, IPAddress.None, args.Response.Feed, args.Response.Topic, true, null);
                try
                {
                    args.Requester.SendMessage(message);
                }
                catch (Exception exception)
                {
                    Log.Debug($"Failed to send to {args.Requester} multi cast message {message}", exception);
                }

                return;
            }

            _repository.AddSubscription(args.Requester, args.Response.Feed, args.Response.Topic, new AuthorizationInfo(args.Response.IsAuthorizationRequired, new HashSet <Guid>(args.Response.Entitlements ?? new Guid[0])));
            _notificationManager.ForwardSubscription(args.Requester, new SubscriptionRequest(args.Response.Feed, args.Response.Topic, true));
        }