Beispiel #1
0
        public void RegisterComponent(EmulatorComponent component)
        {
            if (!_components.Contains(component))
            {
                if (FindComponentById(component.ComponentId) != null)
                {
                    throw new Exception("Another emulator component with component ID \"" + component.ComponentId + "\" already exists.");
                }

                component.SetEmulator(this);
                _components.Add(component);

                if (_emulatorState >= EmulatorState.Setup)
                {
                    component.SetupComponent();
                }

                if (_emulatorState >= EmulatorState.Initialize)
                {
                    component.InitializeComponent();

                    EmulatorComponentCollection ecc;
                    if ((ecc = FindCollection(component)) != null)
                    {
                        ecc.RegisterInternal(component);
                    }
                }

                if (_emulatorState >= EmulatorState.ShutDown)
                {
                    throw new ApplicationException("Cannot add new Emulator component during Shutdown");
                }
            }
        }
Beispiel #2
0
        public void UnregisterComponent(EmulatorComponent component)
        {
            if (_components.Contains(component))
            {
                foreach (EmulatorComponent ec in component.LinkedComponents)
                {
                    UnregisterComponent(ec);
                }

                _components.Remove(component);

                if (_emulatorState > EmulatorState.Setup)
                {
                    EmulatorComponentCollection ecc;
                    if ((ecc = FindCollection(component)) != null)
                    {
                        ecc.UnregisterInternal(component);
                    }
                }

                if (_emulatorState > EmulatorState.Initialize)
                {
                    component.UninitializeComponent();
                }
            }
        }
        private EmulatorComponent UpdateEmulatorComponent(Type type, String componentId, String updatedId, XmlReader config)
        {
            EmulatorComponent updated = _emulator.FindComponentById(updatedId);

            if (updated == null)
            {
                throw new Exception("Cannot find component " + updatedId + ".");
            }

            if (type.IsAssignableFrom(updated.GetType()) == false)
            {
                throw new Exception("Cannot reconfigure a " + updated.GetType().ToString() + " into a " + type.ToString());
            }

            EmulatorComponent original = updated.Clone();

            updated.Configure(config);

            if (updated.IsReplaceableBy(original) == false)
            {
                // the crucial ID info has changed
                throw new Exception("The identification information of " + updated.GetType().ToString() + " cannot be updated in " + updatedId + ".");
            }

            if (componentId != null)
            {
                updated.ComponentId = componentId;
            }

            return(updated);
        }
 public EmulatorComponent()
 {
     _componentId = GetType().ToString() + " (" + Guid.NewGuid().ToString() + ")";
     _linkedComponents = new List<EmulatorComponent>();
     _linkedBy = null;
     _createdByConfigurationFile = false;
 }
 public EmulatorComponent()
 {
     _componentId                = GetType().ToString() + " (" + Guid.NewGuid().ToString() + ")";
     _linkedComponents           = new List <EmulatorComponent>();
     _linkedBy                   = null;
     _createdByConfigurationFile = false;
 }
Beispiel #6
0
        private EmulatorComponentCollection FindCollection(EmulatorComponent ec)
        {
            List <EmulatorComponent> collections = _components.FindAll(IsDerivedFrom <EmulatorComponentCollection>);

            return((EmulatorComponentCollection)collections.Find(
                       delegate(EmulatorComponent ecc)
            {
                return ((EmulatorComponentCollection)ecc).CollectionType.IsAssignableFrom(ec.GetType());
            }));
        }
 public override bool IsReplaceableBy( EmulatorComponent ec )
 {
     if (ec is EmulatorComponentCollection)
     {
         return CollectionType == ((EmulatorComponentCollection)ec).CollectionType;
     }
     else
     {
         return false;
     }
 }
Beispiel #8
0
        private void CallComponents(ComponentCallback callback)
        {
            List <EmulatorComponent> components = new List <EmulatorComponent>(_components);

            for (int i = 0; i < components.Count; i++)
            {
                EmulatorComponent component = components[i];

                callback(component);
            }
        }
 public override bool IsReplaceableBy(EmulatorComponent ec)
 {
     if (ec is EmulatorComponentCollection)
     {
         return(CollectionType == ((EmulatorComponentCollection)ec).CollectionType);
     }
     else
     {
         return(false);
     }
 }
        internal virtual void UnregisterInternal(EmulatorComponent ec)
        {
            CollectionChangeEventHandler evt  = _evtChanged;
            CollectionChangeEventArgs    args = new CollectionChangeEventArgs(CollectionChangeAction.Remove, ec);

            if (evt != null)
            {
                evt(this, args);
            }

            this.Emulator.UnregisterComponent(ec);
        }
        internal virtual void UnregisterInternal(EmulatorComponent ec)
        {
            CollectionChangeEventHandler evt = _evtChanged;
            CollectionChangeEventArgs args = new CollectionChangeEventArgs(CollectionChangeAction.Remove, ec);

            if (evt != null)
            {
                evt(this, args);
            }

            this.Emulator.UnregisterComponent(ec);
        }
        private EmulatorComponent ReplaceEmulatorComponent(Type type, String componentId, String replacedId, XmlReader config)
        {
            EmulatorComponent replaced = _emulator.FindComponentById(replacedId);

            if (replaced == null)
            {
                throw new Exception("Cannot find the component, " + replacedId + ", to be replaced.");
            }

            if (componentId == null)
            {
                componentId = replacedId;
            }

            // Make sure the new componentId isn't taken already
            EmulatorComponent idMatched = _emulator.FindComponentById(componentId);

            if (idMatched != null)
            {
                if (idMatched != replaced)
                {
                    throw new Exception("The Component ID, " + componentId + ", has already been used.");
                }
                else if (idMatched.LinkedBy != null)
                {
                    throw new Exception("Cannot replace the EmulatorComponent " + idMatched.ComponentId + " here because it's linked elsewhere.");
                }
            }

            // Create the component and configure it
            EmulatorComponent component = (EmulatorComponent)Activator.CreateInstance(type);

            component.ComponentId = componentId;

            component.SetEmulator(_emulator);
            component.Configure(config);

            if (replaced.IsReplaceableBy(component) == false)
            {
                throw new Exception("The new component cannot be used to replace the old one.");
            }

            _emulator.UnregisterComponent(replaced);

            Debug.Assert(_emulator.FindReplaceableComponent(component) == null);

            _emulator.RegisterComponent(component);

            return(component);
        }
        private void RemoveEmulatorComponent(Type type, String removedId)
        {
            EmulatorComponent removed = _emulator.FindComponentById(removedId);

            if (removed == null)
            {
                throw new Exception("Cannot find component " + removedId + ".");
            }

            if (type.IsAssignableFrom(removed.GetType()) == false)
            {
                throw new Exception("Type mismatched -- " + removed.GetType().ToString() + " and " + type.ToString() + ".");
            }

            _emulator.UnregisterComponent(removed);
        }
Beispiel #14
0
        public void RegisterComponent(EmulatorComponent component, EmulatorComponent linkedBy)
        {
            RegisterComponent(component);

            if (linkedBy != null)
            {
                if (component.LinkedBy == null)
                {
                    component.LinkedBy = linkedBy;

                    linkedBy.LinkedComponents.Add(component);
                }
                else if (component.LinkedBy != linkedBy)
                {
                    throw new Exception("This EmulatorComponent is already in " + component.LinkedBy.ComponentId + ".");
                }
            }
        }
        private EmulatorComponent CreateEmulatorComponent(Type type, String componentId, XmlReader config)
        {
            if (componentId != null && _emulator.FindComponentById(componentId) != null)
            {
                throw new Exception("Component, " + componentId + ", already existed.");
            }

            // Create the component and configure it
            EmulatorComponent component = (EmulatorComponent)Activator.CreateInstance(type);

            component.SetEmulator(_emulator);

            if (String.IsNullOrEmpty(componentId) == false)
            {
                component.ComponentId = componentId;
            }

            component.Configure(config);

            // Make sure the component is unique (there isn't any existing components that this new component will replace)
            EmulatorComponent replaceable = _emulator.FindReplaceableComponent(component);

            if (replaceable != null)
            {
                if (replaceable.CreatedByConfigurationFile)
                {
                    throw new Exception("Another component, " + replaceable.ComponentId + ", already exists.");
                }
                else
                {
                    // if the component is generated by default, then we won't enforce the replaceId requirement, and replace it.
                    _emulator.UnregisterComponent(replaceable);
                }
            }

            _emulator.RegisterComponent(component);

            return(component);
        }
        // default implementation for EmulatorComponent.Configure()
        public void ConfigureEmulatorComponent( EmulatorComponent component, XmlReader config )
        {
            Type objType = component.GetType();
            config.ReadStartElement(); // bypass the root element

            while (true)
            {
                config.Read();

                if (config.IsStartElement())
                {
                    String propertyName = config.Name;
                    PropertyInfo property = objType.GetProperty( propertyName );

                    if (property == null)
                    {
                        throw new XmlException( "Unrecognize element (Property): " + propertyName );
                    }

                    if (property.CanWrite == false)
                    {
                        throw new Exception( "The element (Property), " + propertyName + ", is read-only." );
                    }

                    Type propertyType = property.PropertyType;
                    Type actualType = propertyType;

                    if (config.MoveToAttribute( "type" ))
                    {
                        actualType = ResolveType( config.Value );
                    }

                    config.MoveToElement();

                    if (typeof( EmulatorComponent ).IsAssignableFrom( propertyType ) ||
                        typeof( EmulatorComponent ).IsAssignableFrom( actualType ))
                    {
                        XmlReader ecConfig = config.ReadSubtree();

                        EmulatorComponent ec = ProcessEmulatorComponent( actualType, ecConfig );

                        ecConfig.Close();

                        if (propertyType.IsAssignableFrom( ec.GetType() ) == false)
                        {
                            throw new Exception( "Type mismatch: " + ec.GetType().ToString() + " is not a " + propertyType.ToString() );
                        }

                        if (ec.LinkedBy == null)
                        {
                            ec.LinkedBy = component;
                        }
                        else if (ec.LinkedBy != component)
                        {
                            throw new Exception( "This EmulatorComponent is already in " + ec.LinkedBy.ComponentId + "." );
                        }

                        if (component.LinkedComponents.Contains( ec ) == false)
                        {
                            component.LinkedComponents.Add( ec );
                        }

                        EmulatorComponent oldEc = (EmulatorComponent)property.GetValue( component, null );
                        if ((oldEc != null) && (oldEc != ec))
                        {
                            _emulator.UnregisterComponent( oldEc );
                        }

                        property.SetValue( component, ec, null );
                    }
                    else
                    {
                        XmlReader objectConfig = config.ReadSubtree();
                        property.SetValue( component, ParseObject( propertyType, objectConfig ), null );
                        objectConfig.Close();
                    }
                }
                else
                {
                    break;
                }
            }
        }
Beispiel #17
0
 private bool IsDerivedFrom <T>(EmulatorComponent ec)
 {
     return(ec is T);
 }
Beispiel #18
0
 internal EmulatorComponent FindReplaceableComponent(EmulatorComponent component)
 {
     return(_components.Find(delegate(EmulatorComponent ec) { return ec.IsReplaceableBy(component); }));
 }
 public void Unregister(EmulatorComponent ec)
 {
     this.Emulator.UnregisterComponent(ec);
 }
 public void Unregister(EmulatorComponent ec)
 {
     this.Emulator.UnregisterComponent(ec);
 }
        private EmulatorComponent ProcessEmulatorComponent(Type type, XmlReader config)
        {
            String       componentId = null, replacesId = null, updatesId = null, removesId = null;
            IdAttributes attr = IdAttributes.None;

            config.MoveToContent();

            if (config.MoveToAttribute("id"))
            {
                componentId = config.Value;

                if (componentId == "")
                {
                    componentId = null;
                }
                else
                {
                    attr |= IdAttributes.ComponentId;
                }
            }
            if (config.MoveToAttribute("replaces"))
            {
                replacesId = config.Value;
                attr      |= IdAttributes.ReplacesId;
            }
            if (config.MoveToAttribute("updates"))
            {
                updatesId = config.Value;
                attr     |= IdAttributes.UpdatesId;
            }
            if (config.MoveToAttribute("removes"))
            {
                removesId = config.Value;
                attr     |= IdAttributes.RemovesId;
            }

            config.MoveToElement();

            EmulatorComponent ec = null;

            switch (attr)
            {
            case IdAttributes.None:
            case IdAttributes.ComponentId:
                ec = CreateEmulatorComponent(type, componentId, config);
                break;

            case IdAttributes.ReplacesId:
            case IdAttributes.ReplacesId | IdAttributes.ComponentId:
                ec = ReplaceEmulatorComponent(type, componentId, replacesId, config);
                break;

            case IdAttributes.UpdatesId:
            case IdAttributes.UpdatesId | IdAttributes.ComponentId:
                ec = UpdateEmulatorComponent(type, componentId, updatesId, config);
                break;

            case IdAttributes.RemovesId:
                RemoveEmulatorComponent(type, removesId);
                break;

            default:
                throw new Exception("Invalid syntax while parsing EmulatorComponent of type" + type.ToString() + ".");
            }

            return(ec);
        }
 public virtual bool IsReplaceableBy( EmulatorComponent ec )
 {
     // by default, if they have the same component base type (for example, if they're both SerialDriver) then they're replaceable.
     return false;
 }
Beispiel #23
0
 public override bool IsReplaceableBy(EmulatorComponent ec)
 {
     return(ec is Hal);
 }
        // default implementation for EmulatorComponent.Configure()
        public void ConfigureEmulatorComponent(EmulatorComponent component, XmlReader config)
        {
            Type objType = component.GetType();

            config.ReadStartElement(); // bypass the root element

            while (true)
            {
                config.Read();

                if (config.IsStartElement())
                {
                    String       propertyName = config.Name;
                    PropertyInfo property     = objType.GetProperty(propertyName);

                    if (property == null)
                    {
                        throw new XmlException("Unrecognize element (Property): " + propertyName);
                    }

                    if (property.CanWrite == false)
                    {
                        throw new Exception("The element (Property), " + propertyName + ", is read-only.");
                    }

                    Type propertyType = property.PropertyType;
                    Type actualType   = propertyType;

                    if (config.MoveToAttribute("type"))
                    {
                        actualType = ResolveType(config.Value);
                    }

                    config.MoveToElement();

                    if (typeof(EmulatorComponent).IsAssignableFrom(propertyType) ||
                        typeof(EmulatorComponent).IsAssignableFrom(actualType))
                    {
                        XmlReader ecConfig = config.ReadSubtree();

                        EmulatorComponent ec = ProcessEmulatorComponent(actualType, ecConfig);

                        ecConfig.Close();

                        if (propertyType.IsAssignableFrom(ec.GetType()) == false)
                        {
                            throw new Exception("Type mismatch: " + ec.GetType().ToString() + " is not a " + propertyType.ToString());
                        }

                        if (ec.LinkedBy == null)
                        {
                            ec.LinkedBy = component;
                        }
                        else if (ec.LinkedBy != component)
                        {
                            throw new Exception("This EmulatorComponent is already in " + ec.LinkedBy.ComponentId + ".");
                        }

                        if (component.LinkedComponents.Contains(ec) == false)
                        {
                            component.LinkedComponents.Add(ec);
                        }

                        EmulatorComponent oldEc = (EmulatorComponent)property.GetValue(component, null);
                        if ((oldEc != null) && (oldEc != ec))
                        {
                            _emulator.UnregisterComponent(oldEc);
                        }

                        property.SetValue(component, ec, null);
                    }
                    else
                    {
                        XmlReader objectConfig = config.ReadSubtree();
                        property.SetValue(component, ParseObject(propertyType, objectConfig), null);
                        objectConfig.Close();
                    }
                }
                else
                {
                    break;
                }
            }
        }
 public virtual bool IsReplaceableBy(EmulatorComponent ec)
 {
     // by default, if they have the same component base type (for example, if they're both SerialDriver) then they're replaceable.
     return(false);
 }
Beispiel #26
0
 public override bool IsReplaceableBy(EmulatorComponent ec)
 {
     return(ec is HalDriver <DriverInterface>);
 }