Esempio n. 1
0
        /// <summary>
        ///
        /// </summary>
        protected bool DoRegisterComponent(PlatformComponent component, bool isInitial)
        {
            TracerHelper.Trace(component.Name);

            if (CanAcceptComponent(component.GetType()) == false)
            {
                SystemMonitor.Error("Failed to add component instance since only one instance of this type allowed.");
                return(false);
            }

            if (component.SubscriptionClientID.Id.Guid == Guid.Empty)
            {
                SystemMonitor.Error("Component [" + component.GetType().Name + "] has no valid Guid Id assigned.");
                return(false);
            }

            if (InitializeComponent(component) == false)
            {
                return(false);
            }

            lock (this)
            {
                if (_components.ContainsValue(component))
                {
                    SystemMonitor.OperationWarning("Component [" + component.GetType().Name + "] already added.");
                    return(true);
                }

                if (_components.ContainsKey(component.SubscriptionClientID.Id))
                {
                    SystemMonitor.Error("Component with this Id [" + component.SubscriptionClientID.Id.Name + ", " + component.SubscriptionClientID.Id.ToString() + "] already added.");
                    return(false);
                }

                _components.Add(component.SubscriptionClientID.Id, component);
            }

            component.OperationalStateChangedEvent += new OperationalStateChangedDelegate(component_OperationalStatusChangedEvent);

            if (component.IsPersistableToDB && PersistenceHelper.IsPersisted <PlatformComponent>(component) == false)
            {// New component not present in DB, persist.
                if (PersistenceHelper.Insert <PlatformComponent>(component, new KeyValuePair <string, object>("PlatformId", this.Id)) == false)
                {
                    SystemMonitor.Error("Failed to insert component [" + component.Name + "] to DB.");
                }
            }

            if (ActiveComponentAddedEvent != null)
            {
                ActiveComponentAddedEvent(component, isInitial);
            }

            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// The initialization of a platform consumes 2 main dataDelivery sources.
        /// The settings is used for primary, startup information (like where
        /// is the persistence DB file etc.) and the information inside the
        /// persistence is than on used to create components etc.
        /// </summary>
        public bool Initialize(PlatformSettings platformSettings)
        {
            TracerHelper.TraceEntry();

            SystemMonitor.CheckThrow(_settings == null, "Platform already initialized.");

            lock (this)
            {
                _settings = platformSettings;
                if (_persistenceHelper == null)
                {
                    _persistenceHelper = CreatePlatformPersistenceHelper(platformSettings);
                }
            }

            if (_persistenceHelper == null)
            {
                return(false);
            }

            LoadModules(_settings);

            if (PersistenceHelper.Count <Platform>(new MatchExpression("Name", this.Name)) == 0)
            {// This is a new platform.
                lock (this)
                {
                    _guid = Guid.NewGuid();
                }

                if (PersistenceHelper.Insert <Platform>(this, null) == false)
                {
                    SystemMonitor.Error("Failed to persist new platform [" + this.Name + "]");
                    return(false);
                }
            }
            else
            {// This is existing.
                // Now try to load self from persistance storage.
                bool selectionResult = PersistenceHelper.SelectScalar <Platform>(this, new MatchExpression("Name", this.Name));

                if (selectionResult == false)
                {// Failed to load self from DB.
                    return(false);
                }
            }

            lock (this)
            {
                if (_serializationData.ContainsValue("diagnosticsMode"))
                {
                    _settings.DiagnosticsMode = _serializationData.GetBoolean("diagnosticsMode");
                }

                if (_serializationData.ContainsValue("uiSerializationInfo"))
                {
                    // The main serialization dataDelivery stores the UI orderInfo.
                    _uiSerializationInfo = _serializationData.GetValue <SerializationInfoEx>("uiSerializationInfo");
                }

                if (_serializationData.ContainsValue("componentSpecificSerializationInfo"))
                {
                    // The main serialization dataDelivery stores the UI orderInfo.
                    _componentSpecificSerializationInfo = _serializationData.GetValue <SerializationInfoEx>("componentSpecificSerializationInfo");
                }
            }

            //_server = new Arbiter.TransportIntegrationServer(_platformUri);
            //Arbiter.AddClient(_server);

            GeneralHelper.FireAndForget(delegate()
            {// LoadFromFile components.
             // Registering of components is better done outside the lock,
             // since components may launch requests to platform at initializations.

                _isLoading = true;

                // Components are stored in the PlatformComponents database, they are being serialized and the entire object is
                // persisted in the DB, as well as the type information for it and a reference to the platform instance it belongs to.
                List <long> failedSerializationsIds = new List <long>();
                List <PlatformComponent> components = PersistenceHelper.SelectSerializedType <PlatformComponent>(
                    new MatchExpression("PlatformId", this.Id), "Data", null, failedSerializationsIds);

                SortedList <int, List <PlatformComponent> > componentsByLevel = GetComponentsByLevel(components);

                GatherMandatoryComponents(componentsByLevel);

                foreach (int level in componentsByLevel.Keys)
                {// Register lower level components first.
                    foreach (PlatformComponent component in componentsByLevel[level])
                    {
                        if (DoRegisterComponent(component, true) == false &&
                            component.Id.HasValue && ComponentDeserializationFailedEvent != null)
                        {
                            ComponentDeserializationFailedEvent(component.Id.Value, component.GetType().Name);
                        }
                    }
                }

                // Handle failed deserializations.
                foreach (int id in failedSerializationsIds)
                {
                    string typeName = "Unknown";

                    try
                    {// Extract the type of this entry.
                        List <object[]> result = PersistenceHelper.SelectColumns <PlatformComponent>(
                            new MatchExpression("Id", id), new string[] { "Type" }, 1);

                        Type type = Type.GetType(result[0][0].ToString());

                        if (type != null)
                        {
                            typeName = type.Name;
                        }
                    }
                    catch (Exception ex)
                    {
                        SystemMonitor.Error("Failed to extract type information [" + ex.Message + "].");
                    }

                    if (ComponentDeserializationFailedEvent != null)
                    {
                        ComponentDeserializationFailedEvent(id, typeName);
                    }
                }

                _isLoading = false;
            });

            return(true);
        }