Exemple #1
0
        /// <summary>
        /// Performs a clean termination of the Ella system<br />
        /// Includes
        /// <list type="bullet">
        /// <item>
        /// <description>Stopping all publishers </description>
        /// </item>
        /// <item>
        /// <description>Cancelling all subscriptions </description>
        /// </item>
        /// <item>
        /// <description>Notifying other nodes of the termination </description>
        /// </item>
        /// </list>
        /// </summary>
        public static void Ella()
        {
            _log.Debug("Shutting down Ella");

            _log.Debug("Broadcasting shutdown message to all nodes");

            /*
             * Notify other nodes of the termination
             */
            Networking.BroadcastShutdown();

            /*
             * Cancel all subscriptions
             */
            var groupedSubscriptions = EllaModel.Instance.FilterSubscriptions(s => true).GroupBy(s => s.Subscriber).ToArray();

            foreach (var s in groupedSubscriptions)
            {
                Unsubscribe.From(s.Key);
            }
            _log.Debug("Unsubscribed all subscribers");

            /*
             * Stop all publishers
             */
            var activePublishers = EllaModel.Instance.GetActivePublishers().ToArray();

            _log.Debug("Stopping publishers");
            foreach (var activePublisher in activePublishers)
            {
                try
                {
                    Stop.Publisher(activePublisher.Instance);
                }
                catch (Exception ex)
                {
                    _log.ErrorFormat("Could not stop publisher {0}. {1}", activePublisher.Instance, ex.Message);
                }
            }

            //Join and terminate (if necessary) the publisher threads
            var publisherThreads = EllaModel.Instance.PublisherThreads.ToArray();

            foreach (Thread t in publisherThreads)
            {
                if (!t.Join(1000))
                {
                    try
                    {
                        t.Abort();
                        if (!t.Join(1000))
                        {
                            _log.DebugFormat("Could not terminate thread {0}. Did all I could.", t);
                        }
                    }
                    catch (Exception)
                    {
                        _log.DebugFormat("Could not terminate thread {0}. Did all I could.", t);
                    }
                }
            }

            Networking.Stop();
        }