Exemplo n.º 1
0
        /// <summary>
        /// Activates the flow of messages inside the dataflow-network.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <exception cref="DataflowNetworkFailedException">Thrown when the dataflow-network has encountered an exception during its lifetime.</exception>
        public void Start <T>(ISourceAgent <T> source)
        {
            if (disposed)
            {
                throw new ObjectDisposedException(null, "DataflowNetwork already disposed.");
            }

            if (m_SourceAgent == null || m_SourceAgent != source)
            {
                throw new ArgumentException("SourceAgent not linked with this network.", "source");
            }

            // Attach sinks.
            foreach (var nullLink in m_NullLinks)
            {
                nullLink();
            }

            try
            {
                // Tell the producer to start producing items.
                var mainTask = source.StartAsync();
                m_ConstituentTasks.Add(mainTask);

                // Block and wait for all the dataflow-network constituents to complete their work.
                Task.WaitAll(m_ConstituentTasks.ToArray(), m_Cts.Token);
                m_LogAgent.LogInfo(DataflowNetworkConstituent.Network, m_Name, "Network finished successfully.");
                DumpStats();
                m_LogAgent.Complete();
            }
            catch (AggregateException ex)
            {
                m_LogAgent.LogError(DataflowNetworkConstituent.Network, m_Name, ex.Flatten(), "The network encountered an unhandled exception.");
                throw new DataflowNetworkFailedException("The datanetwork " + m_Name + " failed. See the inner exception for more details", ex);
            }
            catch (OperationCanceledException)
            {
                var exceptions = m_ConstituentTasks.Where(task => task.IsFaulted).Select(task => task.Exception);
                foreach (var ex in exceptions)
                {
                    m_LogAgent.LogError(DataflowNetworkConstituent.Network, m_Name, ex.Flatten(),
                                        "The network encountered an unhandled exception.");
                }
            }
            catch (Exception ex)
            {
                m_LogAgent.LogError(DataflowNetworkConstituent.Network, m_Name, ex, "The network encountered an unhandled exception.");
                throw new DataflowNetworkFailedException("The datanetwork " + m_Name + " failed. See the inner exception for more details", ex);
            }
            finally
            {
                m_ConstituentTasks.Clear();
                m_Cts.Cancel();
                GC.Collect();
                Dispose();
            }
        }
Exemplo n.º 2
0
 public void Link <T>(ISourceAgent <IDataflowMessage <T> > source, ITargetBlock <IDataflowMessage <T> > target)
 {
     if (m_SourceAgent != null)
     {
         throw new InvalidOperationException("Source agent already registered in this network.");
     }
     source.LinkTo(target, CreateDefaultLinkOptions(), CreateDefaultFilter <T>());
     m_SourceAgent = source;
 }
Exemplo n.º 3
0
        public void Link <T>(ISourceAgent <IDataflowMessage <T> > source, ITargetBlock <IDataflowMessage <T> > target, Predicate <IDataflowMessage <T> > predicate)
        {
            if (m_SourceAgent != null)
            {
                throw new InvalidOperationException("Source agent already registered in this network.");
            }

            var combinedFilter = ComposeFilterWithAnd(CreateDefaultFilter <T>(), predicate);

            source.LinkTo(target, CreateDefaultLinkOptions(), combinedFilter);
            m_SourceAgent = source;
        }