示例#1
0
        //UPGRADE_TODO: The equivalent of method java.lang.Runnable.run is not an override method. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca5065"'
        // internal void Run()
        // implement mainline for thread

        // runs and keeps running as long as there are input ports to read
        void ThreadMain()
        {
            try
            {
                if (IsTerminated() || HasError())
                {
                    try
                    {
                        Monitor.Exit(_lockObject);
                    }
                    catch (SynchronizationLockException e)
                    {
                        // do nothing - this is OK!
                    }
                    return;
                }
                bool componentExecutedAtLeastOnce = false;
                _status = States.Active;

                _mother.Trace("{0}: Started", Name);

                _ = _inputPorts.TryGetValue("*IN", out _autoInput);
                _ = _outputPorts.TryGetValue("*OUT", out _autoOutput);

                if (_autoInput != null)
                {
                    Packet p = _autoInput.Receive();
                    if (p != null)
                    {
                        Drop(p);
                    }
                    _autoInput.Close();
                }

                InputStates ist = null;
                //if (SelfStarting)
                //    _autoStarting = true;
                //else
                //if componentent has connected non IIP inputports, but is not selfstarting
                if (!SelfStarting)
                {
                    try
                    {
                        ist = new InputStates(_inputPorts, this);
                    }
                    catch (ThreadInterruptedException ex)
                    {
                        if (IsTerminated() || HasError())
                        {
                            // if we are in the TERMINATED or ERROR state we terminated intentionally
                            return;
                        }
                        // otherwise there was an error
                        throw ex;
                    }
                }

                while (SelfStarting || _autoStarting || !ist.allDrained || _autoInput != null || ist.allDrained && MustRun || StackSize() > 0)
                {
                    _autoInput = null;
                    if (_network._deadlock || IsTerminated())
                    {
                        break;
                    }
                    _packetCount = 0;

                    foreach (IInputPort port in _inputPorts.Values)
                    {
                        if (port is InitializationConnection icx)
                        {
                            icx.Reopen();
                        }
                    }

                    _mother.Trace("{0}: Activated", Name);
                    try
                    {
                        componentExecutedAtLeastOnce = true;
                        Execute(); // do one activation!
                    }
                    catch (ComponentException e)
                    {
                        _mother.Trace("Component Exception: " + Name + " - " + e.Message);
                        if (e.Message.StartsWith("*"))
                        {
                            string s = e.Message.Substring(1);
                            FlowError.Complain("Component Exception: " + Name + " - " + s);
                        }
                        else
                        {
                            Console.Out.WriteLine("! Component Exception: " + Name + " - " + e.Message);
                        }
                    }

                    _mother.Trace("{0}: Deactivated", Name);

                    if (_packetCount != 0)
                    {
                        _mother.Trace(Name + " deactivated holding " + _packetCount + " packets");

                        FlowError.Complain(_packetCount + " packets not disposed of during Component activation of " + Name);
                    }
                    foreach (IInputPort port in _inputPorts.Values)
                    {
                        if (port is InitializationConnection icx)
                        {
                            if (!icx.IsClosed())
                            {
                                FlowError.Complain("Component deactivated with IIP port not closed: " + icx.Name);
                            }
                        }
                    }
                    MustRun      = false;
                    SelfStarting = false;
                    if (_autoStarting)
                    {
                        break;
                    }
                    // lock ((_inputPorts as ICollection).SyncRoot)
                    //{
                    try
                    {
                        ist = new InputStates(_inputPorts, this);
                    }
                    catch (ThreadInterruptedException ex)
                    {
                        if (IsTerminated() || HasError())
                        {
                            // if we are in the TERMINATED or ERROR state we terminated intentionally
                            return;
                        }
                        // otherwise there was an error
                        throw ex;
                    }
                    if (ist.allDrained)
                    {
                        break;
                    }
                    //if (_network._deadlock)
                    //{
                    //    break;
                    // }
                } //  while (!ist.allDrained);


                //_compLog.Trace("{0}: Terminating", Name);
                //}
                // catch (System.Exception t)
                // {
                //Console.Out.WriteLine("*** Exception detected in " + Name);
                //      System.Diagnostics.Trace.Fail("*** Exception detected in " + Name + ": " + t.Message);
                // }


                //_compLog.Trace("{0}: Terminated", Name);
                if (_autoOutput != null)
                {
                    //Packet p = Create("");
                    //_autoOutput.Send(p);
                    _autoOutput.Close();
                }
                _status = States.Terminated;

                if (_stack.Count > 0)
                {
                    FlowError.Complain("Stack not empty at component termination: " + Name);
                }

                foreach (IInputPort port in _inputPorts.Values)
                {
                    if (port is Connection cx)
                    {
                        if (cx.Count() > 0)
                        {
                            Console.Out.WriteLine("{0}: Component terminated with {1} packets in input connection", cx.Name, cx.Count());
                        }
                        while (cx.Count() > 0)
                        {
                            Packet p = cx._buffer.Take();
                            Console.Out.WriteLine(p);
                        }
                    }
                    else if (port is InitializationConnection iip)
                    {
                        if (componentExecutedAtLeastOnce && !iip.IsClosed())
                        {
                            FlowError.Complain("Component terminated with input port not closed: " + iip.Name);
                        }
                    }
                }

                foreach (OutputPort port in _outputPorts.Values)
                {
                    port.Close();
                }

                //_status = States.Terminated; //will not be set if never activated
                //_network.NotifyTerminated();
                _mother.NotifyTerminated(this);
            }
            catch (Exception e)
            {
                // don't tell the mother if we are already in the ERROR or TERMINATE state
                // because then the mother told us to terminate
                if (!HasError() && !IsTerminated())
                {
                    // an error occurred in this component
                    _status = States.Error;
                    // tell the mother
                    _mother.SignalError(e);
                }
            }
        }
示例#2
0
 /**
  * Issues tracing messages belonging to this input port.
  */
 void Trace(string msg)
 {
     _traceNetwork.Trace(_fullName + ": " + msg);
 }