Esempio n. 1
0
#pragma warning disable CS0162 // Disabled since the flag can be set while debugging
        /// <summary>
        /// TBD
        /// </summary>
        /// <param name="logic">TBD</param>
        /// <param name="evt">TBD</param>
        /// <param name="handler">TBD</param>
        public void RunAsyncInput(GraphStageLogic logic, object evt, Action <object> handler)
        {
            if (!IsStageCompleted(logic))
            {
                if (IsDebug)
                {
                    Console.WriteLine($"{Name} ASYNC {evt} ({handler}) [{logic}]");
                }
                var currentInterpreterHolder = CurrentInterpreter.Value;
                var previousInterpreter      = currentInterpreterHolder[0];
                currentInterpreterHolder[0] = this;
                try
                {
                    ActiveStage = logic;
                    try
                    {
                        handler(evt);
                    }
                    catch (Exception e)
                    {
                        logic.FailStage(e);
                    }
                    AfterStageHasRun(logic);
                }
                finally
                {
                    currentInterpreterHolder[0] = previousInterpreter;
                }
            }
        }
Esempio n. 2
0
 public AsyncInput(GraphInterpreterShell shell, GraphStageLogic logic, object @event, Action <object> handler)
 {
     Shell   = shell;
     Logic   = logic;
     Event   = @event;
     Handler = handler;
 }
Esempio n. 3
0
 /// <summary>
 /// TBD
 /// </summary>
 /// <param name="logic">TBD</param>
 internal void AfterStageHasRun(GraphStageLogic logic)
 {
     if (IsStageCompleted(logic))
     {
         RunningStagesCount--;
         FinalizeStage(logic);
     }
 }
Esempio n. 4
0
 /// <summary>
 /// TBD
 /// </summary>
 /// <param name="id">Identifier of the connection. Corresponds to the array slot in the <see cref="GraphAssembly"/></param>
 /// <param name="inOwnerId">Identifier of the owner of the input side of the connection. Corresponds to the array slot in the <see cref="GraphAssembly"/></param>
 /// <param name="inOwner">The stage logic that corresponds to the input side of the connection.</param>
 /// <param name="outOwnerId">Identifier of the owner of the output side of the connection. Corresponds to the array slot in the <see cref="GraphAssembly"/></param>
 /// <param name="outOwner">The stage logic that corresponds to the output side of the connection.</param>
 /// <param name="inHandler">The handler that contains the callback for input events.</param>
 /// <param name="outHandler">The handler that contains the callback for output events.</param>
 public Connection(int id, int inOwnerId, GraphStageLogic inOwner, int outOwnerId, GraphStageLogic outOwner,
                   IInHandler inHandler, IOutHandler outHandler)
 {
     Id         = id;
     InOwnerId  = inOwnerId;
     InOwner    = inOwner;
     OutOwnerId = outOwnerId;
     OutOwner   = outOwner;
     InHandler  = inHandler;
     OutHandler = outHandler;
 }
Esempio n. 5
0
 /// <summary>
 /// TBD
 /// </summary>
 /// <param name="logic">TBD</param>
 /// <param name="enabled">TBD</param>
 internal void SetKeepGoing(GraphStageLogic logic, bool enabled)
 {
     if (enabled)
     {
         _shutdownCounter[logic.StageId] |= KeepGoingFlag;
     }
     else
     {
         _shutdownCounter[logic.StageId] &= KeepGoingMask;
     }
 }
Esempio n. 6
0
 private void FinalizeStage(GraphStageLogic logic)
 {
     try
     {
         logic.PostStop();
         logic.AfterPostStop();
     }
     catch (Exception err)
     {
         if (Log.IsErrorEnabled)
         {
             Log.Error(err, "Error during PostStop in [{0}]", Assembly.Stages[logic.StageId]);
         }
     }
 }
Esempio n. 7
0
 internal void SetKeepGoing(GraphStageLogic logic, bool enabled)
 {
     if (enabled)
         _shutdownCounter[logic.StageId] |= KeepGoingFlag;
     else
         _shutdownCounter[logic.StageId] &= KeepGoingMask;
 }
Esempio n. 8
0
 /// <summary>
 /// Returns true if the given stage is alredy completed
 /// </summary>
 internal bool IsStageCompleted(GraphStageLogic stage) => stage != null && _shutdownCounter[stage.StageId] == 0;
Esempio n. 9
0
 internal void AfterStageHasRun(GraphStageLogic logic)
 {
     if (IsStageCompleted(logic))
     {
         RunningStagesCount--;
         FinalizeStage(logic);
     }
 }
Esempio n. 10
0
 public void RunAsyncInput(GraphStageLogic logic, object evt, Action<object> handler)
 {
     if (!IsStageCompleted(logic))
     {
         if (IsDebug) Console.WriteLine($"{Name} ASYNC {evt} ({handler}) [{logic}]");
         var currentInterpreterHolder = CurrentInterpreter.Value;
         var previousInterpreter = currentInterpreterHolder[0];
         currentInterpreterHolder[0] = this;
         try
         {
             ActiveStage = logic;
             try
             {
                 handler(evt);
             }
             catch (Exception e)
             {
                 logic.FailStage(e);
             }
             AfterStageHasRun(logic);
         }
         finally
         {
             currentInterpreterHolder[0] = previousInterpreter;
         }
     }
 }
Esempio n. 11
0
        public GraphInterpreter(
            GraphAssembly assembly,
            IMaterializer materializer,
            ILoggingAdapter log,
            IInHandler[] inHandlers,
            IOutHandler[] outHandlers,
            GraphStageLogic[] logics,
            Action<GraphStageLogic, object, Action<object>> onAsyncInput,
            bool fuzzingMode)
        {
            Logics = logics;
            Assembly = assembly;
            Materializer = materializer;
            Log = log;
            InHandlers = inHandlers;
            OutHandlers = outHandlers;
            OnAsyncInput = onAsyncInput;
            FuzzingMode = fuzzingMode;

            ConnectionSlots = new object[assembly.ConnectionCount];
            for (var i = 0; i < ConnectionSlots.Length; i++)
                ConnectionSlots[i] = Empty.Instance;

            PortStates = new int[assembly.ConnectionCount];
            for (var i = 0; i < PortStates.Length; i++)
                PortStates[i] = InReady;

            RunningStagesCount = Assembly.Stages.Length;

            _shutdownCounter = new int[assembly.Stages.Length];
            for (var i = 0; i < _shutdownCounter.Length; i++)
            {
                var shape = assembly.Stages[i].Shape;
                _shutdownCounter[i] = shape.Inlets.Count() + shape.Outlets.Count();
            }

            _eventQueue = new int[1 << (32 - (assembly.ConnectionCount - 1).NumberOfLeadingZeros())];
            _mask = _eventQueue.Length - 1;
        }
 public PostStop(GraphStageLogic source)
 {
     Source = source;
 }
Esempio n. 13
0
 /// <summary>
 /// Returns true if the given stage is already completed
 /// </summary>
 /// <param name="stage">TBD</param>
 /// <returns>TBD</returns>
 internal bool IsStageCompleted(GraphStageLogic stage) => stage != null && _shutdownCounter[stage.StageId] == 0;
Esempio n. 14
0
 public AbruptStageTerminationException(GraphStageLogic logic)
     : base($"GraphStage {logic} terminated abruptly, caused by for example materializer or actor system termination.")
 {
 }
 public OnNext(GraphStageLogic source, object element)
 {
     Source  = source;
     Element = element;
 }
 public OnError(GraphStageLogic source, Exception cause)
 {
     Source = source;
     Cause  = cause;
 }
 public Cancel(GraphStageLogic source)
 {
     Source = source;
 }
 public OnComplete(GraphStageLogic source)
 {
     Source = source;
 }
Esempio n. 19
0
 private void FinalizeStage(GraphStageLogic logic)
 {
     try
     {
         logic.PostStop();
         logic.AfterPostStop();
     }
     catch (Exception err)
     {
         if (Log.IsErrorEnabled)
             Log.Error(err, "Error during PostStop in [{0}]", Assembly.Stages[logic.StageId]);
     }
 }
 public RequestOne(GraphStageLogic source)
 {
     Source = source;
 }
 public PreStart(GraphStageLogic source)
 {
     Source = source;
 }
Esempio n. 22
0
        /// <summary>
        /// Takes an interpreter and returns three arrays required by the interpreter containing the input, output port
        /// handlers and the stage logic instances.
        ///
        /// <para>Returns a tuple of</para>
        /// <para/> - lookup table for InHandlers
        /// <para/> - lookup table for OutHandlers
        /// <para/> - array of the logics
        /// <para/> - materialized value
        /// </summary>
        /// <param name="inheritedAttributes">TBD</param>
        /// <param name="copiedModules">TBD</param>
        /// <param name="materializedValues">TBD</param>
        /// <param name="register">TBD</param>
        /// <exception cref="ArgumentException">TBD</exception>
        /// <returns>TBD</returns>
        public Tuple <Connection[], GraphStageLogic[]> Materialize(
            Attributes inheritedAttributes,
            IModule[] copiedModules,
            IDictionary <IModule, object> materializedValues,
            Action <IMaterializedValueSource> register)
        {
            var logics = new GraphStageLogic[Stages.Length];

            for (var i = 0; i < Stages.Length; i++)
            {
                // Port initialization loops, these must come first
                var shape = Stages[i].Shape;

                var idx             = 0;
                var inletEnumerator = shape.Inlets.GetEnumerator();
                while (inletEnumerator.MoveNext())
                {
                    var inlet = inletEnumerator.Current;
                    if (inlet.Id != -1 && inlet.Id != idx)
                    {
                        throw new ArgumentException($"Inlet {inlet} was shared among multiple stages. That is illegal.");
                    }
                    inlet.Id = idx;
                    idx++;
                }

                idx = 0;
                var outletEnumerator = shape.Outlets.GetEnumerator();
                while (outletEnumerator.MoveNext())
                {
                    var outlet = outletEnumerator.Current;
                    if (outlet.Id != -1 && outlet.Id != idx)
                    {
                        throw new ArgumentException($"Outlet {outlet} was shared among multiple stages. That is illegal.");
                    }
                    outlet.Id = idx;
                    idx++;
                }

                var stage = Stages[i];
                if (stage is IMaterializedValueSource)
                {
                    var copy = ((IMaterializedValueSource)stage).CopySource();
                    register(copy);
                    stage = (IGraphStageWithMaterializedValue <Shape, object>)copy;
                }

                var logicAndMaterialized = stage.CreateLogicAndMaterializedValue(inheritedAttributes.And(OriginalAttributes[i]));
                materializedValues[copiedModules[i]] = logicAndMaterialized.MaterializedValue;
                logics[i] = logicAndMaterialized.Logic;
            }

            var connections = new Connection[ConnectionCount];

            for (var i = 0; i < ConnectionCount; i++)
            {
                var connection = new Connection(i, InletOwners[i],
                                                InletOwners[i] == Boundary ? null : logics[InletOwners[i]], OutletOwners[i],
                                                OutletOwners[i] == Boundary ? null : logics[OutletOwners[i]], null, null);
                connections[i] = connection;

                var inlet = Inlets[i];
                if (inlet != null)
                {
                    var owner = InletOwners[i];
                    var logic = logics[owner];
                    var h     = logic.Handlers[inlet.Id] as IInHandler;

                    if (h == null)
                    {
                        throw new IllegalStateException($"No handler defined in stage {logic} for port {inlet}");
                    }
                    connection.InHandler = h;

                    logic.PortToConn[inlet.Id] = connection;
                }

                var outlet = Outlets[i];
                if (outlet != null)
                {
                    var owner   = OutletOwners[i];
                    var logic   = logics[owner];
                    var inCount = logic.InCount;
                    var h       = logic.Handlers[outlet.Id + inCount] as IOutHandler;

                    if (h == null)
                    {
                        throw new IllegalStateException($"No handler defined in stage {logic} for port {outlet}");
                    }
                    connection.OutHandler = h;

                    logic.PortToConn[outlet.Id + inCount] = connection;
                }
            }

            return(Tuple.Create(connections, logics));
        }
 public RequestAnother(GraphStageLogic source)
 {
     Source = source;
 }