Ejemplo n.º 1
0
 internal virtual void Put(Packet x)
 {
     _array[_sendPtr] = x;
     if (++_sendPtr >= _array.Length)
         _sendPtr = 0;
     IncUsedSlots();
 }
Ejemplo n.º 2
0
        public override void Execute() /* throws Throwable */ {


            Packet p;
            int i = 0, j, k, n;
            Packet[] array = new Packet[9999];
            while ((p = _inport.Receive()) != null)
            {
                array[i] = p;
                // Console.WriteLine("in: " + p.GetContent());
                ++i;
            }

            Console.Out.WriteLine("No. of elements:" + i);
            j = 0;
            k = i;
            n = k;

            string t = null;

            while (n > 0)
            {
                t = null;
                for (i = 0; i < k; ++i)
                {
                    if (array[i] != null)
                    {
                        string s = (string)array[i].Content;
                        if (t == null || (String.Compare(s, t) < 0))
                        {
                            j = i;
                            t = (string)array[j].Content;
                        }
                    }
                }

                _outport.Send(array[j]);
                array[j] = null;

                --n;

            }

        }
Ejemplo n.º 3
0
        /// <summary>Detach Packet from named chain
        /// </summary>

        internal virtual bool Detach(string name, Packet subordinate)
        {
            if (_chains == null)
                return false;
            Chain chain = (Chain)(_chains[name]);
            if (chain == null)
                return false;
            if (!(chain.Members.Contains(subordinate)))
                return false;
            chain.Members.Remove(subordinate);
            //Packet root = Root;
            //subordinate.Owner = root._owner;
            return true;
        }
Ejemplo n.º 4
0
        /// <summary>Maintains a Chain of Packets attached to this Packet.
        /// A Packet may have multiple named Chains attached to it, accessed via a Hashtable.
        /// Since Packets are attached to Chains, as well as Chains to Packets,
        /// this results in an alternation of Packets and Chains, creating a tree structure.
        /// </summary>

        internal virtual void Attach(string name, Packet subordinate)
        {
            if (subordinate == null)
            {
                FlowError.Complain("Null packet reference in 'Attach' method call");
            }
            Packet p = this;
    while (p._owner is Packet) {
      if (p == subordinate) {
        FlowError.Complain("Loop in tree structure");
      }
      p = (Packet) p._owner;
    }
    if (p == subordinate) {
      FlowError.Complain("Loop in tree structure");
    }
    if (p._owner != Thread.CurrentThread)
    {
      FlowError.Complain("Packet not owned (directly or indirectly) by current component");
    }
    if (subordinate._owner != Thread.CurrentThread)
    {
      FlowError.Complain("Subordinate packet not owned by current component");
    }
            if (_chains == null)
                _chains = new Dictionary<string, Chain>();
            Chain chain = (Chain)(_chains[name]);
            if (chain == null)
            {
                chain = new Chain(name);
                _chains.Add(name, chain);
            }
            
            subordinate.Owner = this;
            chain.Members.Add(subordinate);
        }
Ejemplo n.º 5
0
 /// <summary>The receive function of an InitializationConection.
 /// Returns null after the packet has been delivered (because packet is set to null).
 /// You get one copy per activation
 /// *
 /// Warning: the object contained in this packet must not be modified.
 /// *
 /// See IInputPort.receive.
 /// </summary>
 public Packet Receive()
 {
     if (IsClosed())
         return null;
     else
     {
         Interlocked.Increment(ref Network.receives);
         Packet p = new Packet(_receiver);
         p._content = _content;
         _receiver._mother.Trace("{0}: IIP received: {1}", Name, p);
         Close();    // is needed!
         return p;
     }
 }
Ejemplo n.º 6
0
        /// <summary> Send a packet to this Port.
        /// The thread is suspended if no capacity is currently available.
        /// If the port or connection has been closed, <code>false</code> is
        /// returned; otherwise, <code>true</code> is returned.
        /// <p> Do not reference the packet after sending - someone else may be
        /// modifying it!
        /// </summary>
        /// <param name="packet">packet to send
        /// </param>
        /// <returns>true if successful
        /// *
        /// </returns>

        // The send function.
        public virtual void Send(Packet packet)
        {
            bool res = true;
            Trace("Sending: " + packet.ToString());
            _sender.CheckOwner(packet);

            if (isClosed)
            {

                Trace("Sending - port closed");
                res = false;
            }
            res &= _cnxt.Send(packet, this); // fire up send method on connection

            if (!res)
                FlowError.Complain("Could not deliver packet to " + Name);
           Trace("Sent OK");

            return;
        }
Ejemplo n.º 7
0
        /// <summary>The send function.  See OutputPort.send.
        /// </summary>
        //UPGRADE_NOTE: Synchronized keyword was removed from method 'send'. Lock expression was added. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1027"'
        internal bool Send(Packet packet, OutputPort op)
        {
            lock (this)
            {
                if (packet == null)
                    throw new System.ArgumentException();
                _sender = op._sender;

                if (IsClosed())
                {
                    return false;
                }
                //_sender._mother.Trace(Name + ": Sending: " + packet);

                if (IsEmpty())
                    System.Threading.Monitor.PulseAll(this);

                while (IsFull())
                {
                    if (_dropOldest)
                    {
                        Packet p = _buffer.Take();
                        Interlocked.Increment(ref Network.dropOlds);
                        _sender._mother.Trace("{0}: DropOldest", _sender.Name);
                    }
                    else
                    {
                        _sender.Status = Component.States.SuspSend;

                        _sender._mother.Trace("{0}: Send/susp", _sender.Name);
                        try
                        {
                            System.Threading.Monitor.Wait(this);
                        }
                        catch (System.Threading.ThreadInterruptedException e)
                        {
                            //UPGRADE_NOTE: Exception 'java.lang.ThreadDeath' was converted to ' ' which has different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1100"'

                            //      throw new System.ApplicationException();
                            IndicateOneSenderClosed();
                            FlowError.Complain(_sender._name + ": interrupted");
                            // unreachable code
                            return false;
                        }
                        // if (Component._network._deadlock)
                        //{
                        //    _sender._thread.Interrupt();
                        //    _sender._mother.Trace(_sender._name + ": Send interrupted because of deadlock");
                        //    // unreachable code
                        //    return false;
                        // }

                        _sender = op._sender;
                        _sender.Status = Component.States.Active;

                        _sender._mother.Trace("{0}: Send/resume", _sender.Name);
                    }
                }
                if (IsClosed())
                {
                    _sender._mother.Trace("{0}: Send/close", _sender.Name);
                    //Interlocked.Increment(ref Network.sends);
                    return false;
                }

                //lock ((_receiver._inputPorts as ICollection).SyncRoot)
                try
                {
                    Monitor.Enter(_receiver._lockObject);
                    packet.ClearOwner();
                    _buffer.Put(packet);

                    if (_receiver.Status == Component.States.Dormant ||
                        _receiver.Status == Component.States.NotStarted)
                        _receiver.Activate();
                    else
                        System.Threading.Monitor.PulseAll(this);

                    _sender._network._active = true;
                    _sender = null;

                }
                catch (ThreadInterruptedException e)
                {
                    return false;
                }
                finally
                {
                    Monitor.Exit(_receiver._lockObject);
                }
                Interlocked.Increment(ref Network.sends);
                return true;
            }
        }
Ejemplo n.º 8
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>
        //internal int downstreamCount()
        // {
        //    return _cnxt.Count();
        // }
        /// <summary> Send a packet to this Port.
        /// The thread is suspended if no capacity is currently available.
        /// If the port or connection has been closed, <code>false</code> is
        /// returned; otherwise, <code>true</code> is returned.
        /// <p> Do not reference the packet after sending - someone else may be
        /// modifying it!
        /// </summary>
        /// <param name="packet">packet to send
        /// </param>
        /// <returns>true if successful
        /// *
        /// </returns>

        // The send function.
        public override void Send(Packet packet)
        {
            //_sender.Drop(packet);  -- do nothing - changed Aug. 5, 2014
        }