public static bool SetPeerRespondingState(this IPeerRepository repository, PeerId peerId, bool isResponding, DateTime timestampUtc) { var peer = repository.Get(peerId); if (peer == null || peer.TimestampUtc > timestampUtc) { return(false); } peer.Peer.IsResponding = isResponding; peer.TimestampUtc = timestampUtc; repository.AddOrUpdatePeer(peer); return(true); }
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); }
public void Handle(RegisterPeerCommand message) { if (_blacklistedMachines.Contains(Context !.Originator.SenderMachineName !)) { throw new InvalidOperationException($"Peer {Context.SenderId} on host {Context.Originator.SenderMachineName} is not allowed to register on this directory"); } var peerTimestampUtc = message.Peer.TimestampUtc; if (!peerTimestampUtc.HasValue) { throw new InvalidOperationException("The TimestampUtc must be provided when registering"); } var utcNow = SystemDateTime.UtcNow; if (_configuration.MaxAllowedClockDifferenceWhenRegistering != null && peerTimestampUtc.Value > utcNow + _configuration.MaxAllowedClockDifferenceWhenRegistering) { throw new InvalidOperationException($"The client provided timestamp [{peerTimestampUtc}] is too far ahead of the the server's current time [{utcNow}]"); } var stopwatch = Stopwatch.StartNew(); var peerDescriptor = message.Peer; peerDescriptor.Peer.IsUp = true; peerDescriptor.Peer.IsResponding = true; var existingPeer = _peerRepository.Get(peerDescriptor.PeerId); if (IsPeerInConflict(existingPeer, peerDescriptor)) { throw new DomainException(DirectoryErrorCodes.PeerAlreadyExists, string.Format("Peer {0} already exists (running on {1})", peerDescriptor.PeerId, existingPeer.Peer.EndPoint)); } _peerRepository.RemoveAllDynamicSubscriptionsForPeer(peerDescriptor.PeerId, DateTime.SpecifyKind(peerDescriptor.TimestampUtc !.Value, DateTimeKind.Utc)); _peerRepository.AddOrUpdatePeer(peerDescriptor); _bus.Publish(new PeerStarted(peerDescriptor)); var registredPeerDescriptors = _peerRepository.GetPeers(loadDynamicSubscriptions: true); _bus.Reply(new RegisterPeerResponse(registredPeerDescriptors.ToArray())); _speedReporter.ReportRegistrationDuration(stopwatch.Elapsed); }
public static PeerDescriptor UpdatePeerSubscriptions(this IPeerRepository repository, PeerId peerId, Subscription[] subscriptions, DateTime?timestampUtc) { var peerDescriptor = repository.Get(peerId); if (peerDescriptor == null) { throw new InvalidOperationException(string.Format("The specified Peer ({0}) does not exist.", peerId)); } if (peerDescriptor.TimestampUtc > timestampUtc) { return(null); } peerDescriptor.TimestampUtc = timestampUtc; peerDescriptor.Subscriptions = subscriptions; repository.AddOrUpdatePeer(peerDescriptor); return(peerDescriptor); }
public void AddOrUpdatePeerEntry(PeerDescriptor peerDescriptor) { _peerRepository.AddOrUpdatePeer(peerDescriptor); }