public AccountSampleEventWriter(SampleEventKey sampleEventKey, IServerStreamWriter <SampleEventArgsMessage> responseStream,
                                 LogControl logControl, Action <SampleEventKey> removeAction)
 {
     _sampleEventKey       = sampleEventKey;
     _responseStream       = responseStream;
     _logControl           = logControl;
     _removeAction         = removeAction;
     _account              = Accounts.RequireAccount(_sampleEventKey.AccountName);
     _account.SampleEvent += AccountOnSampleEvent;
 }
        public override async Task Subscribe(IAsyncStreamReader <SubscribeRequest> requestStream, IServerStreamWriter <SampleEventArgsMessage> responseStream, ServerCallContext context)
        {
            var requesterHeader = context.RequestHeaders.FirstOrDefault(x => x.Key == Constants.Requester.ToLower()); // Must be lower case. Ugh!
            var peer            = context.Peer;
            var requester       = requesterHeader == null ? $"Anonymous@{peer}" : $"{requesterHeader.Value}@{peer}";

            while (await requestStream.MoveNext())
            {
                var request = requestStream.Current;
                var key     = new SampleEventKey(request.AccountName, requester);
                if (request.StartStop)
                {
                    var writer = new AccountSampleEventWriter(key, responseStream, _logControl, RemoveWriter);
                    _eventWriters.TryAdd(key, writer);
                    OnAccountNameAdded(request.AccountName);
                    _logControl.LogMessage($"Added subscription to SampleEvents for {AccountNameAdded}");
                }
                else
                {
                    RemoveWriter(key);
                    return;
                }
            }
        }
 private void RemoveWriter(SampleEventKey key)
 {
     _eventWriters.TryRemove(key, out _);
     OnAccountNameRemoved(key.AccountName);
     _logControl.LogMessage($"Removed subscription to SampleEvents for {AccountNameAdded}");
 }