Exemplo n.º 1
0
 /// <summary> This method returns the downstream Packet count for a given OutputPort
 /// It is normally only used by Components that do load balancing.
 /// </summary>
 /// <returns>int
 ///
 /// </returns>
 public int DownstreamCount()
 {
     return(_cnxt.Count());
 }
Exemplo n.º 2
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;
                }
                _status = States.Active;

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

                if (!_inputPorts.ContainsKey("*IN"))
                {
                    _autoInput = null;
                }
                else
                {
                    _autoInput = (IInputPort)(_inputPorts["*IN"]);
                }
                if (!_outputPorts.ContainsKey("*OUT"))
                {
                    _autoOutput = null;
                }
                else
                {
                    _autoOutput = (OutputPort)(_outputPorts["*OUT"]);
                }

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

                InputStates ist = null;
                if (SelfStarting)
                {
                    _autoStarting = true;
                }
                else
                {
                    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 (_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)
                        {
                            InitializationConnection icx = port as InitializationConnection;
                            icx.Reopen();
                        }
                    }

                    _mother.Trace("{0}: Activated", Name);
                    try
                    {
                        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)
                        {
                            InitializationConnection icx = port as InitializationConnection;
                            //if (!icx.IsClosed())
                            //    FlowError.Complain("Component deactivated with IIP port not closed: " + icx.Name);
                            icx.Close();
                        }
                    }
                    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)
                    {
                        Connection cx = port as Connection;
                        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);
                        }
                    }
                    if (port is InitializationConnection)
                    {
                        InitializationConnection iip = port as InitializationConnection;
                        if (!(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);
                }
            }
        }