Ejemplo n.º 1
0
        private async Task TryRegisterOnDirectoryAsync(IBus bus, PeerDescriptor selfDescriptor)
        {
            var directoryPeers = GetDirectoryPeers().ToList();

            foreach (var directoryPeer in directoryPeers)
            {
                try
                {
                    if (await TryRegisterOnDirectoryAsync(bus, selfDescriptor, directoryPeer).ConfigureAwait(false))
                    {
                        return;
                    }
                }
                catch (TimeoutException ex)
                {
                    _logger.Error(ex);
                }
            }

            var directoryPeersText = string.Join(", ", directoryPeers.Select(peer => "{" + peer + "}"));
            var message            = $"Unable to register peer on directory (tried: {directoryPeersText}) after {_configuration.RegistrationTimeout}";

            _logger.Error(message);
            throw new TimeoutException(message);
        }
Ejemplo n.º 2
0
        private void AddOrUpdatePeerEntry(PeerDescriptor peerDescriptor, bool shouldRaisePeerUpdated)
        {
            var subscriptions = peerDescriptor.Subscriptions ?? Array.Empty <Subscription>();

            var peerEntry = _peers.AddOrUpdate(peerDescriptor.PeerId, key => CreatePeerEntry(), (key, entry) => UpdatePeerEntry(entry));

            peerEntry.SetSubscriptions(subscriptions, peerDescriptor.TimestampUtc);

            if (shouldRaisePeerUpdated)
            {
                PeerUpdated?.Invoke(peerDescriptor.Peer.Id, PeerUpdateAction.Started);
            }

            var observedSubscriptions = GetObservedSubscriptions(subscriptions);

            if (observedSubscriptions.Count > 0)
            {
                PeerSubscriptionsUpdated?.Invoke(peerDescriptor.PeerId, observedSubscriptions);
            }

            PeerEntry CreatePeerEntry() => new PeerEntry(peerDescriptor, _globalSubscriptionsIndex);

            PeerEntry UpdatePeerEntry(PeerEntry entry)
            {
                entry.Peer.EndPoint       = peerDescriptor.Peer.EndPoint;
                entry.Peer.IsUp           = peerDescriptor.Peer.IsUp;
                entry.Peer.IsResponding   = peerDescriptor.Peer.IsResponding;
                entry.IsPersistent        = peerDescriptor.IsPersistent;
                entry.TimestampUtc        = peerDescriptor.TimestampUtc ?? DateTime.UtcNow;
                entry.HasDebuggerAttached = peerDescriptor.HasDebuggerAttached;

                return(entry);
            }
        }
Ejemplo n.º 3
0
        private async Task <bool> TryRegisterOnDirectoryAsync(IBus bus, PeerDescriptor self, Peer directoryPeer)
        {
            try
            {
                var registration = await bus.Send(new RegisterPeerCommand(self), directoryPeer).WithTimeoutAsync(_configuration.RegistrationTimeout).ConfigureAwait(false);

                var response = (RegisterPeerResponse?)registration.Response;
                if (response?.PeerDescriptors == null)
                {
                    return(false);
                }

                if (registration.ErrorCode == DirectoryErrorCodes.PeerAlreadyExists)
                {
                    _logger.InfoFormat("Register rejected for {0}, the peer already exists in the directory", new RegisterPeerCommand(self).Peer.PeerId);
                    return(false);
                }

                response.PeerDescriptors?.ForEach(peer => AddOrUpdatePeerEntry(peer, shouldRaisePeerUpdated: false));

                return(true);
            }
            catch (TimeoutException ex)
            {
                _logger.Error(ex);
                return(false);
            }
        }
Ejemplo n.º 4
0
        private bool TryRegisterOnDirectory(IBus bus, PeerDescriptor self, Peer directoryPeer)
        {
            var registration = bus.Send(new RegisterPeerCommand(self), directoryPeer);

            if (!registration.Wait(_configuration.RegistrationTimeout))
            {
                return(false);
            }

            var response = (RegisterPeerResponse)registration.Result.Response;

            if (response == null || response.PeerDescriptors == null)
            {
                return(false);
            }

            if (registration.Result.ErrorCode == DirectoryErrorCodes.PeerAlreadyExists)
            {
                _logger.InfoFormat("Register rejected for {0}, the peer already exists in the directory", new RegisterPeerCommand(self).Peer.PeerId);
                return(false);
            }

            if (response.PeerDescriptors != null)
            {
                response.PeerDescriptors.ForEach(AddOrUpdatePeerEntry);
            }

            return(true);
        }
Ejemplo n.º 5
0
 internal PeerDescriptor(PeerDescriptor other)
 {
     Peer = new Peer(other.Peer);
     Subscriptions = ArrayUtil.Copy(other.Subscriptions) ?? ArrayUtil.Empty<Subscription>();
     IsPersistent = other.IsPersistent;
     TimestampUtc = other.TimestampUtc;
     HasDebuggerAttached = other.HasDebuggerAttached;
 }
Ejemplo n.º 6
0
 internal PeerDescriptor(PeerDescriptor other)
 {
     Peer                = new Peer(other.Peer);
     Subscriptions       = ArrayUtil.Copy(other.Subscriptions) ?? ArrayUtil.Empty <Subscription>();
     IsPersistent        = other.IsPersistent;
     TimestampUtc        = other.TimestampUtc;
     HasDebuggerAttached = other.HasDebuggerAttached;
 }
            public PeerEntry(PeerDescriptor descriptor, ConcurrentDictionary <MessageTypeId, PeerSubscriptionTree> globalSubscriptionsIndex)
            {
                Peer                = new Peer(descriptor.Peer);
                IsPersistent        = descriptor.IsPersistent;
                TimestampUtc        = descriptor.TimestampUtc ?? DateTime.UtcNow;
                HasDebuggerAttached = descriptor.HasDebuggerAttached;

                _globalSubscriptionsIndex = globalSubscriptionsIndex;
            }
Ejemplo n.º 8
0
        private void TryRegisterOnDirectory(IBus bus, PeerDescriptor selfDescriptor)
        {
            var directoryPeers = GetDirectoryPeers().ToList();

            if (!directoryPeers.Any(peer => TryRegisterOnDirectory(bus, selfDescriptor, peer)))
            {
                throw new TimeoutException(string.Format("Unable to register peer on directory (tried: {0})", string.Join(", ", directoryPeers.Select(peer => "{" + peer + "}"))));
            }
        }
 public PeerEntry(PeerDescriptor descriptor, ConcurrentDictionary<MessageTypeId, PeerSubscriptionTree> globalSubscriptionsIndex)
 {
     Peer = new Peer(descriptor.Peer);
     IsPersistent = descriptor.IsPersistent;
     TimestampUtc = descriptor.TimestampUtc ?? DateTime.UtcNow;
     HasDebuggerAttached = descriptor.HasDebuggerAttached;
     
     _globalSubscriptionsIndex = globalSubscriptionsIndex;
 }
Ejemplo n.º 10
0
        public void should_return_the_corresponding_peer()
        {
            var descriptor = new PeerDescriptor(new PeerId("theID"), "tcp://endpoint:123", true, true, true, SystemDateTime.UtcNow, new[] { new Subscription(new MessageTypeId(typeof(string))) });

            var peer = descriptor.Peer;

            peer.Id.ShouldEqual(descriptor.Peer.Id);
            peer.EndPoint.ShouldEqual(descriptor.Peer.EndPoint);
            peer.IsUp.ShouldEqual(descriptor.Peer.IsUp);
        }
Ejemplo n.º 11
0
        private void TryRegisterOnDirectory(IBus bus, PeerDescriptor selfDescriptor)
        {
            var directoryPeers = GetDirectoryPeers().ToList();

            if (directoryPeers.Any(peer => TryRegisterOnDirectory(bus, selfDescriptor, peer)))
            {
                return;
            }

            var directoryPeersText = string.Join(", ", directoryPeers.Select(peer => "{" + peer + "}"));
            var message            = $"Unable to register peer on directory (tried: {directoryPeersText}) after {_configuration.RegistrationTimeout}";

            _logger.Error(message);
            throw new TimeoutException(message);
        }
Ejemplo n.º 12
0
        public Task RegisterAsync(IBus bus, Peer self, IEnumerable <Subscription> subscriptions)
        {
            _self = self;

            var selfDescriptor = new PeerDescriptor(self.Id, self.EndPoint, false, self.IsUp, self.IsResponding, SystemDateTime.UtcNow, subscriptions.ToArray())
            {
                HasDebuggerAttached = Debugger.IsAttached
            };

            _peerRepository.AddOrUpdatePeer(selfDescriptor);
            _pingStopwatch.Restart();

            bus.Publish(new PeerStarted(selfDescriptor));

            Registered?.Invoke();

            return(Task.CompletedTask);
        }
Ejemplo n.º 13
0
        private void AddOrUpdatePeerEntry(PeerDescriptor peerDescriptor)
        {
            var subscriptions = peerDescriptor.Subscriptions ?? ArrayUtil.Empty <Subscription>();

            var peerEntry = _peers.AddOrUpdate(peerDescriptor.PeerId, (key) => new PeerEntry(peerDescriptor, _globalSubscriptionsIndex), (key, entry) =>
            {
                entry.Peer.EndPoint       = peerDescriptor.Peer.EndPoint;
                entry.Peer.IsUp           = peerDescriptor.Peer.IsUp;
                entry.Peer.IsResponding   = peerDescriptor.Peer.IsResponding;
                entry.IsPersistent        = peerDescriptor.IsPersistent;
                entry.TimestampUtc        = peerDescriptor.TimestampUtc ?? DateTime.UtcNow;
                entry.HasDebuggerAttached = peerDescriptor.HasDebuggerAttached;

                return(entry);
            });

            peerEntry.SetSubscriptions(subscriptions, peerDescriptor.TimestampUtc);
        }
Ejemplo n.º 14
0
 public PeerStarted(PeerDescriptor peerDescriptor)
 {
     PeerDescriptor = peerDescriptor;
 }
Ejemplo n.º 15
0
 public PeerSubscriptionsUpdated(PeerDescriptor peerDescriptor)
 {
     PeerDescriptor = peerDescriptor;
 }
Ejemplo n.º 16
0
 public RegisterPeerCommand(PeerDescriptor peer)
 {
     Peer = peer;
 }
Ejemplo n.º 17
0
 public RegisterPeerResponse(PeerDescriptor[] peerDescriptors)
 {
     PeerDescriptors = peerDescriptors;
 }
Ejemplo n.º 18
0
 public PeerStarted(PeerDescriptor peerDescriptor)
 {
     PeerDescriptor = peerDescriptor;
 }
Ejemplo n.º 19
0
 public RegisterPeerCommand(PeerDescriptor peer)
 {
     Peer = peer;
 }
Ejemplo n.º 20
0
 public PeerSubscriptionsUpdated(PeerDescriptor peerDescriptor)
 {
     PeerDescriptor = peerDescriptor;
 }