Example #1
0
        /// <summary>
        /// Start the service
        /// </summary>
        public bool Start()
        {
            this.Starting?.Invoke(this, EventArgs.Empty);

            this.m_subscriptionDefinitions = new List <SubscriptionDefinition>();
            // Subscribe to the applet manager
            EventHandler loaderFn = (o, e) =>
            {
                var retVal = new List <SubscriptionDefinition>(this.m_subscriptionDefinitions?.Count ?? 10);
                var slns   = ApplicationServiceContext.Current.GetService <IAppletSolutionManagerService>().Solutions.ToList();
                slns.Add(new AppletSolution()
                {
                    Meta = new AppletInfo()
                    {
                        Id = String.Empty
                    }
                });

                foreach (var s in slns)
                {
                    var slnMgr = ApplicationServiceContext.Current.GetService <IAppletSolutionManagerService>().GetApplets(s.Meta.Id);
                    // Find and load all sub defn's
                    foreach (var am in slnMgr)
                    {
                        var definitions = am.Assets.Where(a => a.Name.StartsWith("subscription/")).Select <AppletAsset, SubscriptionDefinition>(a =>
                        {
                            using (var ms = new MemoryStream(slnMgr.RenderAssetContent(a)))
                            {
                                this.m_tracer.TraceInfo("Attempting load of {0}", a.Name);
                                try
                                {
                                    return(SubscriptionDefinition.Load(ms));
                                }
                                catch (Exception ex)
                                {
                                    this.m_tracer.TraceError("Error loading {0} : {1}", a.Name, ex);
                                    return(null);
                                }
                            }
                        }).OfType <SubscriptionDefinition>();

                        retVal.AddRange(definitions.Where(n => !retVal.Any(a => a.Key == n.Key)));
                    }
                }

                this.m_tracer.TraceInfo("Registering applet subscriptions");

                lock (m_lockObject)
                {
                    this.m_subscriptionDefinitions.Clear();
                    this.m_subscriptionDefinitions.AddRange(retVal);
                }
            };


            ApplicationServiceContext.Current.Started += (o, e) =>
            {
                // Collection changed handler
                this.m_tracer.TraceInfo("Binding to change events");
                var appletService = ApplicationServiceContext.Current.GetService <IAppletManagerService>();

                if (appletService == null)
                {
                    throw new InvalidOperationException("No applet manager service has been loaded!!!!");
                }

                ApplicationServiceContext.Current.GetService <IAppletManagerService>().Changed += loaderFn;

                if ((ApplicationServiceContext.Current.GetService <IAppletManagerService>() as IDaemonService)?.IsRunning == false)
                {
                    (ApplicationServiceContext.Current.GetService <IAppletManagerService>() as IDaemonService).Started += loaderFn;
                }
                else
                {
                    loaderFn(this, EventArgs.Empty);
                }
            };
            return(true);
        }