Ejemplo n.º 1
0
        // Adds an output adapter to the dependency chain.
        private void AddOutputAdapter(IOutputAdapter adapter, ISet <IAdapter> dependencyChain, IInputAdapter[] inputAdapterCollection, IActionAdapter[] actionAdapterCollection, IOutputAdapter[] outputAdapterCollection)
        {
            HashSet <MeasurementKey> inputMeasurementKeys = new HashSet <MeasurementKey>(adapter.InputMeasurementKeys());

            // Adds the adapter to the chain
            dependencyChain.Add(adapter);

            if ((object)inputAdapterCollection != null)
            {
                // Checks all input adapters to determine whether they also need to be
                // added to the chain as a result of this adapter being added to the chain
                foreach (IInputAdapter inputAdapter in inputAdapterCollection)
                {
                    if (!dependencyChain.Contains(inputAdapter) && inputMeasurementKeys.Overlaps(inputAdapter.OutputMeasurementKeys()))
                    {
                        AddInputAdapter(inputAdapter, dependencyChain, inputAdapterCollection, actionAdapterCollection, outputAdapterCollection);
                    }
                }
            }

            if ((object)actionAdapterCollection != null)
            {
                // Checks all action adapters to determine whether they also need to be
                // added to the chain as a result of this adapter being added to the chain
                foreach (IActionAdapter actionAdapter in actionAdapterCollection)
                {
                    if (actionAdapter.RespectOutputDemands && !dependencyChain.Contains(actionAdapter) && inputMeasurementKeys.Overlaps(actionAdapter.OutputMeasurementKeys()))
                    {
                        AddActionAdapter(actionAdapter, dependencyChain, inputAdapterCollection, actionAdapterCollection, outputAdapterCollection);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        // Searches the assemblies in the current directory for historian implementations.
        // The historians are output adapters for which the initial value of
        // OutputIsForArchive is true.
        private List <Type> GetHistorianTypes()
        {
            DescriptionAttribute descriptionAttribute;

            // This crazy linq expression will only load output adapters where:
            //      OutputIsForArchive = true and BrowsableState = Always
            // then order the list by:
            //      default type is listed first
            //      types in default assembly listed second
            //      items that have a description sorted above those that don't
            //      then in alphabetical order
            return(typeof(IOutputAdapter).LoadImplementations(true).Where(type =>
            {
                try
                {
                    IOutputAdapter adapter = Activator.CreateInstance(type) as IOutputAdapter;
                    return (adapter != null) && adapter.OutputIsForArchive;
                }
                catch
                {
                    return false;
                }
            })
                   .Distinct()
                   .Where(type => GetEditorBrowsableState(type) == EditorBrowsableState.Always)
                   .Select(type => Tuple.Create(type, GetDescription(type)))
                   .OrderByDescending(pair => pair.Item1.FullName == DefaultType)
                   .ThenByDescending(pair => Path.GetFileName(pair.Item1.Assembly.Location) == DefaultAssembly)
                   .ThenByDescending(pair => pair.Item2.Item2)
                   .ThenBy(pair => pair.Item2.Item1)
                   .Select(pair => pair.Item1)
                   .ToList());
        }
Ejemplo n.º 3
0
 public Game(IOutputAdapter outputAdapter, IGameEngine gameEngine, IPlayer firstPlayer, IPlayer secondPlayer, int waitBetweenMoves = 0)
 {
     _outputAdapter = outputAdapter;
     _gameEngine = gameEngine;
     _players[0] = firstPlayer;
     _players[1] = secondPlayer;
     _waitBetweenMoves = waitBetweenMoves;
 }
Ejemplo n.º 4
0
 // Searches the assemblies in the current directory for historian implementations.
 // The historians are output adapters for which the initial value of
 // OutputIsForArchive is true.
 private List <Type> GetHistorianTypes()
 {
     return(typeof(IOutputAdapter).LoadImplementations(true).Where(type =>
     {
         IOutputAdapter adapter = Activator.CreateInstance(type) as IOutputAdapter;
         return (adapter != null) && adapter.OutputIsForArchive;
     }).ToList());
 }
Ejemplo n.º 5
0
 public Game(IOutputAdapter outputAdapter, IGameEngine gameEngine, IPlayer firstPlayer, IPlayer secondPlayer, int waitBetweenMoves = 0)
 {
     _outputAdapter    = outputAdapter;
     _gameEngine       = gameEngine;
     _players[0]       = firstPlayer;
     _players[1]       = secondPlayer;
     _waitBetweenMoves = waitBetweenMoves;
 }
Ejemplo n.º 6
0
 public void Run(IOutputAdapter output)
 {
     _processor        = new Processor();
     _processor.Output = output;
     while (_processor.PC < _statements.Count)
     {
         var cmd = _statements[_processor.PC];
         _processor.PC++;
         cmd.Exec(_processor);
     }
 }
Ejemplo n.º 7
0
        public QueuedEventProcessor(IActionHandler actionHandler, IOutputAdapter outputAdapter, ILogger logger, long capacity = 1)
        {
            ActionHandler = actionHandler;
            OutputAdapter = outputAdapter;
            TokenSource   = new CancellationTokenSource();
            Token         = TokenSource.Token;
            Capacity      = capacity;
            Logger        = logger;
            Queue         = new BlockingCollection <Event>();

            if (Capacity <= 0)
            {
                Capacity = 1;
            }

            ConsumerTask = Task.Run(async() => await ProcessEvents(), Token);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Event handler for monitoring unprocessed measurements.
        /// </summary>
        /// <param name="sender">Event source reference to adapter, typically an output adapter, that is reporting the number of unprocessed measurements.</param>
        /// <param name="e">Event arguments containing number of queued (i.e., unprocessed) measurements in the source adapter.</param>
        /// <remarks>
        /// Time-series framework uses this handler to monitor the number of unprocessed measurements in output adapters.<br/>
        /// This method is typically called once every five seconds.
        /// </remarks>
        public virtual void UnprocessedMeasurementsHandler(object sender, EventArgs <int> e)
        {
            int unprocessedMeasurements = e.Argument;

            if (unprocessedMeasurements > m_measurementDumpingThreshold)
            {
                IOutputAdapter outputAdapter = sender as IOutputAdapter;

                if (outputAdapter != null)
                {
                    // If an output adapter queue size exceeds the defined measurement dumping threshold,
                    // then the queue will be truncated before system runs out of memory
                    outputAdapter.RemoveMeasurements(m_measurementDumpingThreshold);
                    OnStatusMessage(sender, "[{0}] System exercised evasive action to conserve memory and dumped {1:N0} unprocessed measurements from the output queue :(", UpdateType.Alarm, outputAdapter.Name, m_measurementDumpingThreshold);
                    OnStatusMessage(sender, "[{0}] NOTICE: Adapter may be offline or processing data too slowly to keep up with incoming data volume. It may be necessary to adjust measurement threshold configuration settings and/or increase amount of available system memory.", UpdateType.Warning, outputAdapter.Name);
                }
                else
                {
                    // It is only expected that output adapters will be mapped to this handler, but in case
                    // another adapter type uses this handler we will still display a message
                    OnStatusMessage(sender, "[{0}] CRITICAL: There are {1:N0} unprocessed measurements in the adapter queue - but sender \"{2}\" is not an IOutputAdapter, so no evasive action can be exercised.", UpdateType.Warning, GetDerivedName(sender), unprocessedMeasurements, sender.GetType().Name);
                }
            }
            else if (unprocessedMeasurements > m_measurementWarningThreshold)
            {
                if (unprocessedMeasurements >= m_measurementDumpingThreshold - m_measurementWarningThreshold)
                {
                    OnStatusMessage(sender, "[{0}] CRITICAL: There are {1:N0} unprocessed measurements in the output queue.", UpdateType.Warning, GetDerivedName(sender), unprocessedMeasurements);
                }
                else
                {
                    OnStatusMessage(sender, "[{0}] There are {1:N0} unprocessed measurements in the output queue.", UpdateType.Warning, GetDerivedName(sender), unprocessedMeasurements);
                }
            }

            // Bubble message up to any event subscribers
            OnUnprocessedMeasurements(sender, e.Argument);
        }
Ejemplo n.º 9
0
 public PyramidPrinter(IOutputAdapter outputAdapter)
 {
     _outputAdapter = outputAdapter;
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Method for distributing new measurements in a routed fashion.
        /// </summary>
        /// <param name="newMeasurements">Collection of new measurements.</param>
        /// <remarks>
        /// Time-series framework uses this handler to directly route new measurements to the action and output adapters.
        /// </remarks>
        public virtual void RoutedMeasurementsHandler(IEnumerable <IMeasurement> newMeasurements)
        {
            if ((object)m_actionRoutes == null || (object)m_outputRoutes == null)
            {
                return;
            }

            List <IActionAdapter> actionRoutes;
            List <IOutputAdapter> outputRoutes;
            Dictionary <IActionAdapter, List <IMeasurement> > actionMeasurements = new Dictionary <IActionAdapter, List <IMeasurement> >();
            Dictionary <IOutputAdapter, List <IMeasurement> > outputMeasurements = new Dictionary <IOutputAdapter, List <IMeasurement> >();
            List <IMeasurement> measurements;
            MeasurementKey      key;

            m_adapterRoutesCacheLock.EnterReadLock();

            try
            {
                // Loop through each new measurement and look for destination routes
                foreach (IMeasurement measurement in newMeasurements)
                {
                    key = measurement.Key;

                    if (m_actionRoutes.TryGetValue(key, out actionRoutes))
                    {
                        // Add measurements for each destination action adapter route
                        foreach (IActionAdapter actionAdapter in actionRoutes)
                        {
                            if (!actionMeasurements.TryGetValue(actionAdapter, out measurements))
                            {
                                measurements = new List <IMeasurement>();
                                actionMeasurements.Add(actionAdapter, measurements);
                            }

                            measurements.Add(measurement);
                        }
                    }

                    if (m_outputRoutes.TryGetValue(key, out outputRoutes))
                    {
                        // Add measurements for each destination output adapter route
                        foreach (IOutputAdapter outputAdapter in outputRoutes)
                        {
                            if (!outputMeasurements.TryGetValue(outputAdapter, out measurements))
                            {
                                measurements = new List <IMeasurement>();
                                outputMeasurements.Add(outputAdapter, measurements);
                            }

                            measurements.Add(measurement);
                        }
                    }
                }

                // Send broadcast action measurements
                foreach (IActionAdapter actionAdapter in m_actionBroadcastRoutes)
                {
                    if (actionAdapter.Enabled)
                    {
                        actionAdapter.QueueMeasurementsForProcessing(newMeasurements);
                    }
                }

                // Send broadcast output measurements
                foreach (IOutputAdapter outputAdapter in m_outputBroadcastRoutes)
                {
                    if (outputAdapter.Enabled)
                    {
                        outputAdapter.QueueMeasurementsForProcessing(newMeasurements);
                    }
                }
            }
            finally
            {
                m_adapterRoutesCacheLock.ExitReadLock();
            }

            // Send routed action measurements
            foreach (KeyValuePair <IActionAdapter, List <IMeasurement> > actionAdapterMeasurements in actionMeasurements)
            {
                IActionAdapter actionAdapter = actionAdapterMeasurements.Key;

                if (actionAdapter.Enabled)
                {
                    actionAdapter.QueueMeasurementsForProcessing(actionAdapterMeasurements.Value);
                }
            }

            // Send routed output measurements
            foreach (KeyValuePair <IOutputAdapter, List <IMeasurement> > outputAdapterMeasurements in outputMeasurements)
            {
                IOutputAdapter outputAdapter = outputAdapterMeasurements.Key;

                if (outputAdapter.Enabled)
                {
                    outputAdapter.QueueMeasurementsForProcessing(outputAdapterMeasurements.Value);
                }
            }
        }
Ejemplo n.º 11
0
 internal AbstractOutputAdapter(Processor p, IOutputAdapter <OutputType> Adapter)
     : base(p)
 {
     outputAdapter = Adapter;
     DebugName     = "CSVOutputAdapter";
 }
Ejemplo n.º 12
0
 // DIP: all dependencies are passed via constructor injection
 public AppEngineTestable(IAdapterFactory factory, IFileHelper helper, IOutputAdapter output)
 {
     _factory = factory;
     _helper  = helper;
     _output  = output;
 }
Ejemplo n.º 13
0
 public void Setup()
 {
     _outputAdapter = new StringBuilderOutputAdapter();
 }
Ejemplo n.º 14
0
        // Adds an output adapter to the dependency chain.
        private void AddOutputAdapter(IOutputAdapter adapter, ISet<IAdapter> dependencyChain, IInputAdapter[] inputAdapterCollection, IActionAdapter[] actionAdapterCollection, IOutputAdapter[] outputAdapterCollection)
        {
            HashSet<MeasurementKey> inputMeasurementKeys = new HashSet<MeasurementKey>(adapter.InputMeasurementKeys());

            // Adds the adapter to the chain
            dependencyChain.Add(adapter);

            if ((object)inputAdapterCollection != null)
            {
                // Checks all input adapters to determine whether they also need to be
                // added to the chain as a result of this adapter being added to the chain
                foreach (IInputAdapter inputAdapter in inputAdapterCollection)
                {
                    if (!dependencyChain.Contains(inputAdapter) && inputMeasurementKeys.Overlaps(inputAdapter.OutputMeasurementKeys()))
                        AddInputAdapter(inputAdapter, dependencyChain, inputAdapterCollection, actionAdapterCollection, outputAdapterCollection);
                }
            }

            if ((object)actionAdapterCollection != null)
            {
                // Checks all action adapters to determine whether they also need to be
                // added to the chain as a result of this adapter being added to the chain
                foreach (IActionAdapter actionAdapter in actionAdapterCollection)
                {
                    if (actionAdapter.RespectOutputDemands && !dependencyChain.Contains(actionAdapter) && inputMeasurementKeys.Overlaps(actionAdapter.OutputMeasurementKeys()))
                        AddActionAdapter(actionAdapter, dependencyChain, inputAdapterCollection, actionAdapterCollection, outputAdapterCollection);
                }
            }
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Determines the set of adapters in the dependency chain for all adapters in the system which are either not connect or demand or are demanded.
        /// </summary>
        /// <param name="inputAdapterCollection">Collection of input adapters at start of routing table calculation.</param>
        /// <param name="actionAdapterCollection">Collection of action adapters at start of routing table calculation.</param>
        /// <param name="outputAdapterCollection">Collection of output adapters at start of routing table calculation.</param>
        protected virtual ISet<IAdapter> TraverseDependencyChain(IInputAdapter[] inputAdapterCollection, IActionAdapter[] actionAdapterCollection, IOutputAdapter[] outputAdapterCollection)
        {
            ISet<IAdapter> dependencyChain = new HashSet<IAdapter>();

            if ((object)inputAdapterCollection != null)
            {
                foreach (IInputAdapter inputAdapter in inputAdapterCollection)
                {
                    if (inputAdapter.AutoStart && !dependencyChain.Contains(inputAdapter))
                        AddInputAdapter(inputAdapter, dependencyChain, inputAdapterCollection, actionAdapterCollection, outputAdapterCollection);
                }
            }

            if ((object)actionAdapterCollection != null)
            {
                foreach (IActionAdapter actionAdapter in actionAdapterCollection)
                {
                    if (actionAdapter.AutoStart && !dependencyChain.Contains(actionAdapter))
                        AddActionAdapter(actionAdapter, dependencyChain, inputAdapterCollection, actionAdapterCollection, outputAdapterCollection);
                }
            }

            if ((object)outputAdapterCollection != null)
            {
                foreach (IOutputAdapter outputAdapter in outputAdapterCollection)
                {
                    if (outputAdapter.AutoStart && !dependencyChain.Contains(outputAdapter))
                        AddOutputAdapter(outputAdapter, dependencyChain, inputAdapterCollection, actionAdapterCollection, outputAdapterCollection);
                }
            }

            return dependencyChain;
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Determines the set of adapters in the dependency chain that produces the set of signals in the
        /// <paramref name="inputMeasurementKeysRestriction"/> and returns the set of input signals required by the
        /// adapters in the chain and the set of output signals produced by the adapters in the chain.
        /// </summary>
        /// <param name="inputMeasurementKeysRestriction">The set of signals that must be produced by the dependency chain.</param>
        /// <param name="inputAdapterCollection">Collection of input adapters at start of routing table calculation.</param>
        /// <param name="actionAdapterCollection">Collection of action adapters at start of routing table calculation.</param>
        /// <param name="outputAdapterCollection">Collection of output adapters at start of routing table calculation.</param>
        protected virtual ISet<IAdapter> TraverseDependencyChain(ISet<MeasurementKey> inputMeasurementKeysRestriction, IInputAdapter[] inputAdapterCollection, IActionAdapter[] actionAdapterCollection, IOutputAdapter[] outputAdapterCollection)
        {
            ISet<IAdapter> dependencyChain = new HashSet<IAdapter>();

            if ((object)inputAdapterCollection != null)
            {
                foreach (IInputAdapter inputAdapter in inputAdapterCollection)
                {
                    if (!dependencyChain.Contains(inputAdapter) && inputMeasurementKeysRestriction.Overlaps(inputAdapter.OutputMeasurementKeys()))
                        AddInputAdapter(inputAdapter, dependencyChain, inputAdapterCollection, actionAdapterCollection, outputAdapterCollection);
                }
            }

            if ((object)actionAdapterCollection != null)
            {
                foreach (IActionAdapter actionAdapter in actionAdapterCollection)
                {
                    if (!dependencyChain.Contains(actionAdapter) && inputMeasurementKeysRestriction.Overlaps(actionAdapter.OutputMeasurementKeys()))
                        AddActionAdapter(actionAdapter, dependencyChain, inputAdapterCollection, actionAdapterCollection, outputAdapterCollection);
                }
            }

            return dependencyChain;
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Starts or stops connect on demand adapters based on current state of demanded input or output signals.
        /// </summary>
        /// <param name="inputMeasurementKeysRestriction">The set of signals to be produced by the chain of adapters to be handled.</param>
        /// <param name="inputAdapterCollection">Collection of input adapters at start of routing table calculation.</param>
        /// <param name="actionAdapterCollection">Collection of action adapters at start of routing table calculation.</param>
        /// <param name="outputAdapterCollection">Collection of output adapters at start of routing table calculation.</param>
        /// <remarks>
        /// Set the <paramref name="inputMeasurementKeysRestriction"/> to null to use full adapter routing demands.
        /// </remarks>
        protected virtual void HandleConnectOnDemandAdapters(ISet<MeasurementKey> inputMeasurementKeysRestriction, IInputAdapter[] inputAdapterCollection, IActionAdapter[] actionAdapterCollection, IOutputAdapter[] outputAdapterCollection)
        {
            ISet<IAdapter> dependencyChain;

            ISet<MeasurementKey> inputSignals;
            ISet<MeasurementKey> outputSignals;
            ISet<MeasurementKey> requestedInputSignals;
            ISet<MeasurementKey> requestedOutputSignals;

            if (inputMeasurementKeysRestriction.Any())
            {
                // When an input signals restriction has been defined, determine the set of adapters
                // by walking the dependency chain of the restriction
                dependencyChain = TraverseDependencyChain(inputMeasurementKeysRestriction, inputAdapterCollection, actionAdapterCollection, outputAdapterCollection);
            }
            else
            {
                // Determine the set of adapters in the dependency chain for all adapters in the system
                dependencyChain = TraverseDependencyChain(inputAdapterCollection, actionAdapterCollection, outputAdapterCollection);
            }

            // Get the full set of requested input and output signals in the entire dependency chain
            inputSignals = new HashSet<MeasurementKey>(dependencyChain.SelectMany(adapter => adapter.InputMeasurementKeys()));
            outputSignals = new HashSet<MeasurementKey>(dependencyChain.SelectMany(adapter => adapter.OutputMeasurementKeys()));

            // Turn connect on demand input adapters on or off based on whether they are part of the dependency chain
            if ((object)inputAdapterCollection != null)
            {
                foreach (IInputAdapter inputAdapter in inputAdapterCollection)
                {
                    if (!inputAdapter.AutoStart)
                    {
                        if (dependencyChain.Contains(inputAdapter))
                        {
                            requestedOutputSignals = new HashSet<MeasurementKey>(inputAdapter.OutputMeasurementKeys());
                            requestedOutputSignals.IntersectWith(inputSignals);
                            inputAdapter.RequestedOutputMeasurementKeys = requestedOutputSignals.ToArray();
                            inputAdapter.Enabled = true;
                        }
                        else
                        {
                            inputAdapter.RequestedOutputMeasurementKeys = null;
                            inputAdapter.Enabled = false;
                        }
                    }
                }
            }

            // Turn connect on demand action adapters on or off based on whether they are part of the dependency chain
            if ((object)actionAdapterCollection != null)
            {
                foreach (IActionAdapter actionAdapter in actionAdapterCollection)
                {
                    if (!actionAdapter.AutoStart)
                    {
                        if (dependencyChain.Contains(actionAdapter))
                        {
                            if (actionAdapter.RespectInputDemands)
                            {
                                requestedInputSignals = new HashSet<MeasurementKey>(actionAdapter.InputMeasurementKeys());
                                requestedInputSignals.IntersectWith(outputSignals);
                                actionAdapter.RequestedInputMeasurementKeys = requestedInputSignals.ToArray();
                            }

                            if (actionAdapter.RespectOutputDemands)
                            {
                                requestedOutputSignals = new HashSet<MeasurementKey>(actionAdapter.OutputMeasurementKeys());
                                requestedOutputSignals.IntersectWith(inputSignals);
                                actionAdapter.RequestedOutputMeasurementKeys = requestedOutputSignals.ToArray();
                            }

                            actionAdapter.Enabled = true;
                        }
                        else
                        {
                            actionAdapter.RequestedInputMeasurementKeys = null;
                            actionAdapter.RequestedOutputMeasurementKeys = null;
                            actionAdapter.Enabled = false;
                        }
                    }
                }
            }

            // Turn connect on demand output adapters on or off based on whether they are part of the dependency chain
            if ((object)outputAdapterCollection != null)
            {
                foreach (IOutputAdapter outputAdapter in outputAdapterCollection)
                {
                    if (!outputAdapter.AutoStart)
                    {
                        if (dependencyChain.Contains(outputAdapter))
                        {
                            requestedInputSignals = new HashSet<MeasurementKey>(outputAdapter.OutputMeasurementKeys());
                            requestedInputSignals.IntersectWith(inputSignals);
                            outputAdapter.RequestedInputMeasurementKeys = requestedInputSignals.ToArray();
                            outputAdapter.Enabled = true;
                        }
                        else
                        {
                            outputAdapter.RequestedInputMeasurementKeys = null;
                            outputAdapter.Enabled = false;
                        }
                    }
                }
            }
        }