private static bool DisconnectIfEmpty(IDictionary <TKey, IPipelineSink <TMessage> > sinks, TKey correlationId)
        {
            IPipelineSink <TMessage> keySink = null;

            if (!sinks.TryGetValue(correlationId, out keySink))
            {
                return(false);
            }

            CorrelatedMessageSinkRouter <TMessage, TKey> router = keySink as CorrelatedMessageSinkRouter <TMessage, TKey>;

            if (router == null)
            {
                return(false);
            }

            if (router.SinkCount != 0)
            {
                return(false);
            }

            sinks.Remove(correlationId);

            router.Dispose();

            return(sinks.Count == 0);
        }
Esempio n. 2
0
        bool Disconnect(TKey correlationId, IPipelineSink <T> sink)
        {
            return(_output.Set(sinks =>
            {
                CorrelatedMessageSinkRouter <T, TMessage, TKey> keySink;
                if (sinks.TryGetValue(correlationId, out keySink) == false)
                {
                    return sinks;
                }

                var result = new Dictionary <TKey, CorrelatedMessageSinkRouter <T, TMessage, TKey> >(sinks);

                List <IPipelineSink <T> > outputSinks = keySink.Sinks.Where(x => x != sink).ToList();
                if (outputSinks.Count == 0)
                {
                    result.Remove(correlationId);
                }
                else
                {
                    result[correlationId] = new CorrelatedMessageSinkRouter <T, TMessage, TKey>(correlationId,
                                                                                                outputSinks);
                }

                return result;
            }) != null);
        }
Esempio n. 3
0
        public UnsubscribeAction Connect(TKey correlationId, IPipelineSink <T> sink)
        {
            _output.Set(sinks =>
            {
                CorrelatedMessageSinkRouter <T, TMessage, TKey> keySink;
                if (sinks.TryGetValue(correlationId, out keySink) == false)
                {
                    keySink = new CorrelatedMessageSinkRouter <T, TMessage, TKey>(correlationId);
                    keySink.Connect(sink);

                    return(new Dictionary <TKey, CorrelatedMessageSinkRouter <T, TMessage, TKey> >(sinks)
                    {
                        { correlationId, keySink }
                    });
                }

                var result = new Dictionary <TKey, CorrelatedMessageSinkRouter <T, TMessage, TKey> >(sinks);

                keySink = new CorrelatedMessageSinkRouter <T, TMessage, TKey>(correlationId, keySink.Sinks);
                keySink.Connect(sink);
                result[correlationId] = keySink;

                return(result);
            });

            return(() => Disconnect(correlationId, sink));
        }
        public UnsubscribeAction Connect(TKey correlationId, IPipelineSink <TMessage> sink)
        {
            IPipelineSink <TMessage> keySink = null;

            if (_sinks.ReadLock(x => x.TryGetValue(correlationId, out keySink)) == false)
            {
                // we need to add the sink if it doesn't exist
                _sinks.WriteLock(x =>
                {
                    if (x.TryGetValue(correlationId, out keySink) == false)
                    {
                        MessageRouter <TMessage> keyRouter = new CorrelatedMessageSinkRouter <TMessage, TKey>(correlationId);

                        x.Add(correlationId, keyRouter);

                        keySink = keyRouter;
                    }
                });
            }

            return(keySink.ConnectToRouter(sink, () => _sinks.WriteLock(x => DisconnectIfEmpty(x, correlationId))));
        }