Example #1
0
 protected void OnNewReceptor(object sender, ReceptorEventArgs args)
 {
     // Forward to other listeners.
     NewReceptor.Fire(this, args);
     UpdatePermeability();
 }
Example #2
0
        /// <summary>
        /// When all current receptors have been registered (see Register... methods), call this method to
        /// instantiate (if necessary) and register the receptors.  This also generates the
        /// protocol-receptor map for the currently registered receptors.
        /// </summary>
        public void LoadReceptors(Action <IReceptor> afterRegister = null)
        {
            int             processedCount = 0;
            string          receptorName;
            List <Receptor> newReceptors = new List <Receptor>();

            // TODO: Refactor out of this code.
            Say("Loading receptors.");

            foreach (Receptor r in Receptors)
            {
                // TODO: This is lame, to prevent existing receptors from being re-processed.
                if (!r.Instantiated)
                {
                    r.LoadAssembly().Instantiate(this);
                }

                // Get the carriers this receptor is interested in.
                if (!registeredReceptorMap.ContainsKey(r))
                {
                    registeredReceptorMap[r] = true;

                    // Get the protocols that this receptor is receiving, updating the protocolReceptorMap, adding this
                    // receptor to the specified protocols.
                    GatherProtocolReceivers(r);
                    newReceptors.Add(r);
                    ++processedCount;
                    receptorName = r.Name;
                }
            }

            // This order is important.  The visualizer needs to know all the receptors within this membrane AFTER
            // the receptors have been instantiated.  Secondly, the receptors can't be initialized until the visualizer
            // knows where they are.

            // Let interested parties know that we have new receptors and handle how we want to announce the fact.
            newReceptors.ForEach(r =>
            {
                if (afterRegister != null)
                {
                    afterRegister(r);
                }

                NewReceptor.Fire(this, new ReceptorEventArgs(r));
            });

            // Let the receptor instance perform additional initialization, such as creating carriers.
            // We do this only for enabled receptors.
            newReceptors.Where(r => r.Enabled).ForEach(r => r.Instance.Initialize());

            // Any queued carriers are now checked to determine whether receptors now exist to process their protocols.
            ProcessQueuedCarriers();

            // If we've loaded only one receptor...
            // TODO: Refactor this out of here.
            if (processedCount == 1)
            {
                Say("Receptor " + Receptors[0].Name + " online.");
            }
            else
            {
                Say("Receptors online.");
            }
        }