Пример #1
0
        public ReconfigurationRequest loadRequest(string xml, MetaHashComponent component)
        {
            ReconfigurationRequest request = null;

            Console.WriteLine("[XmlLoader.loadRequest] Iniciando carga de script de reconfiguração... ");

            if (true /*validator.IsValid (REQUEST_XSD, xml)*/)
            {
                clear();
                generator.setInitialCode(component.LastIdCode);

                request = new ReconfigurationRequest();
                XmlDocument document = new XmlDocument();
                XmlNode     nodeRequest, data;

                document.Load(xml);
                nodeRequest = document.SelectSingleNode("reconfigurationRequest");

                data = nodeRequest.SelectSingleNode("targetComponent");
                request.TargetComponent = data.InnerText;

                //Console.WriteLine("[XmlLoader.LoadRequest] Obtendo componente alvo {0}", request.TargetComponent);

                List <MetaParameter> parameterList                 = uLoader.getParameters(nodeRequest.SelectNodes("parameter"));

                if (parameterList != null)
                {
                    Console.WriteLine("[XmlLoader.LoadRequest] Carregando as reconfigurações estruturais...");
                    request.StructuralRequest = new StructuralReconfigurationRequest(parameterList);
                }

                BehavioralReconfigurationRequest behavioralRequest = null;

                List <MetaInnerComponent> innerComponents          = generateInnerComponents(nodeRequest.SelectNodes("innerComponent"));
                //Console.WriteLine("[XmlLoader.LoadRequest] Tem innerComponent para adicionar? {0}", innerComponents != null);
                if (innerComponents != null)
                {
                    behavioralRequest = new BehavioralReconfigurationRequest();

                    Console.WriteLine("[XmlLoader.LoadRequest] Carregando novos componentes aninhados...");
                    //Console.WriteLine("[XmlLoader.LoadRequest] {0} InnerComponent(s) adicionado(s)", innerComponents.Count);
                    behavioralRequest.NewInnerComponents = innerComponents;
                    uLoader.CandidateInnerComponents     = innerComponents;
                }

                uLoader.InnerComponents = innerComponents;
                XmlNodeList changeActionList                       = nodeRequest.SelectNodes("changeAction");

                if (changeActionList != null)
                {
                    Console.WriteLine("[XmlLoader.LoadRequest] Carregando as reconfigurações comportamentais...");
                    IEnumerator      ienum = changeActionList.GetEnumerator();
                    BehavioralChange change;
                    XmlNode          changeNode;

                    if (behavioralRequest == null)
                    {
                        behavioralRequest = new BehavioralReconfigurationRequest();
                    }

                    XmlAttribute attr;
                    int          initialCode;
                    int          aditionalStates, aditionalTrans;
                    aditionalStates = aditionalTrans = 0;
                    while (ienum.MoveNext())
                    {
                        changeNode = (XmlNode)ienum.Current;

                        change = new BehavioralChange();
                        //Console.WriteLine("[XmlLoader.LoadRequest] Criando uma nova BehavioralChange");
                        initialCode = generator.getCurrentCode();

                        attr        = (XmlAttribute)changeNode.Attributes.GetNamedItem("unit");
                        change.Unit = attr.Value;
                        //Console.WriteLine("[XmlLoader.LoadRequest] Unit a sofrer alteração: {0}", change.Unit);

                        MetaUnit u = component.Units[change.Unit];

                        if (u != null)
                        {
                            //Console.WriteLine("[XmlLoader.LoadRequest] Unit {0} encontrada!", change.Unit);

                            attr          = (XmlAttribute)changeNode.Attributes.GetNamedItem("action");
                            change.Action = attr.Value;
                            //Console.WriteLine("[XmlLoader.LoadRequest] Ação a sofrer alteração: {0}", change.Action);
                            MetaAction a = u.Actions[change.Action];

                            if (a != null && !a.IsNative)
                            {
                                //Console.WriteLine("[XmlLoader.LoadRequest] Ação {0} encontrada!", change.Action);
                                attr = (XmlAttribute)changeNode.Attributes.GetNamedItem("type");
                                if (attr.Value.Equals("remove"))
                                {
                                    change.Type = BehavioralChange.BehavioralChangeType.REMOVE;
                                }
                                else
                                {
                                    change.Type = BehavioralChange.BehavioralChangeType.INCLUDE;
                                }

                                attr         = (XmlAttribute)changeNode.Attributes.GetNamedItem("point");
                                change.Point = attr.Value;

                                //Console.WriteLine("[XmlLoader.LoadRequest] Ponto a sofrer alteração: {0}", change.Point);
                                Transition t = a.Protocol.getTransition(change.Point);

                                if (t != null)
                                {
                                    //Console.WriteLine("[XmlLoader.LoadRequest] Ponto {0} encontrado.", change.Point);
                                    change.NewSlices        = uLoader.getSlices(changeNode.SelectNodes("slice"));
                                    uLoader.CandidateSlices = change.NewSlices;

                                    if (change.Type == BehavioralChange.BehavioralChangeType.INCLUDE)
                                    {
                                        //Console.WriteLine("[XmlLoader.LoadRequest] Obtendo novo protocolo");
                                        change.Transitions = uLoader.getTransitions(changeNode.SelectSingleNode("protocol").FirstChild, u, t.InitialState, t.FinalState, a.Protocol.NumStates + aditionalStates, a.Protocol.LastTransationId + aditionalTrans);

                                        aditionalStates = uLoader.NumStates - a.Protocol.NumStates;
                                        aditionalTrans  = uLoader.NumTransations - a.Protocol.LastTransationId;
                                        //TODO remover
                                        //Console.WriteLine("Transações de reconfiguração");
                                        //foreach(Transition tst in change.Transitions) {
                                        //	Console.WriteLine(tst);
                                        //}
                                    }
                                    else
                                    {
                                        change.Transitions = new List <Transition>();
                                        change.Transitions.Add(new Transition(t.InitialState, t.FinalState, Configuration.LAMBDA_TRANSITION, a.Protocol.LastTransationId++));
                                    }
                                }
                                else
                                {
                                    throw new Exception("[XmlLoader.loadRequest] Ponto de reconfiguração não existe: " + change.Point);
                                }
                            }
                            else
                            {
                                throw new Exception("[XmlLoader.loadRequest] Ação a ser alterada não existe ou nãoo é uma configuração: " + change.Action);
                            }
                        }
                        else
                        {
                            throw new Exception("[XmlLoader.loadRequest] Unidade da ação a ser alterada não existe: " + change.Unit);
                        }

                        change.AditionalStates = generator.getCurrentCode() - initialCode;
                        behavioralRequest.AddChange(change);
                    }
                }

                request.BehavioralRequest = behavioralRequest;
                component.LastIdCode      = generator.getCurrentCode();
            }

            uLoader.CandidateSlices          = null;
            uLoader.CandidateInnerComponents = null;

            Console.WriteLine("[XmlLoader.loadRequest] Carga de script realizada com sucesso!");
            return(request);
        }
        /*public bool CommitReconfiguration ()
         * {
         *      Thread commit = new Thread(new ThreadStart(Commit));
         *      commit.Priority = ThreadPriority.Highest;
         *      commit.Start();
         *
         *      return true;
         * }*/

        public bool CommitReconfiguration()
        {
            Configuration protocol;

            Console.WriteLine("[ConfigurationManager.Commit] Aplicando a reconfiguração...");
            lock (thisLock) {
                if (reconfiguring)
                {
                    foreach (ExecutionStateEvaluation es in evaluations)
                    {
                        protocol = unit.Actions [es.ActionName].Protocol;
                        protocol.stopStates(es.StatesToStop);
                        WaitForSafeState(protocol.ReconfigMonitor, es);
                        Console.WriteLine("[ConfigurationManager.Commit] Estado seguro para reconfiguração!");

                        if (request.StructuralRequest != null && request.StructuralRequest.Changes != null)
                        {
                            Console.WriteLine("[ConfigurationManager.Commit] Aplicando reconfigurações estruturais...");
                            foreach (StructuralChange sc in request.StructuralRequest.Changes)
                            {
                                if (SecurityAnalyzer.isChangeConcrete(sc))
                                {
                                    Console.WriteLine("[ConfigurationManager.Commit] Reconfigurando a unidade {0}", sc.Old.Name);
                                    createConcreteUnit(sc.New, ((IUnit)sc.Old.Entity).CID.getInstanceName() /*+ "(new)"*/);
                                    SubstituteUnit(sc.Old, sc.New);
                                }
                            }
                        }
                        BehavioralReconfigurationRequest behavioralRequest = request.BehavioralRequest;

                        if (behavioralRequest != null)
                        {
                            foreach (BehavioralChange bc in behavioralRequest.Changes)
                            {
                                if (bc.NewSlices != null)
                                {
                                    //Console.WriteLine("[ConfigurationManager.Commit] Adicionando novas fatias");
                                    foreach (MetaSlice slice in bc.NewSlices.Values)
                                    {
                                        if (!unit.Slices.ContainsKey(slice.Inner))
                                        {
                                            Console.WriteLine("[ConfigurationManager.Commit] Adicionando fatia '{0}'...", slice.Inner);
                                            createConcreteUnit(slice.Unit, slice.Inner /*+ "(new)"*/);
                                            unit.AddSlice(slice.Inner, slice);
                                        }
                                    }
                                }

                                unit.Actions [bc.Action].Protocol.generateAutomaton(bc.Transitions);
                            }
                        }
                    }

                    foreach (ExecutionStateEvaluation es in evaluations)
                    {
                        foreach (MetaAction action in unit.Actions.Values)
                        {
                            if (!action.IsNative && action.Name.Equals(es.ActionName))
                            {
                                action.Protocol.runStates(es.StatesToStop);
                            }
                        }
                    }

                    this.reconfiguring = false;
                    this.evaluations   = null;
                    this.request       = null;

                    Console.WriteLine("[ConfigurationManager.Commit] Reconfiguração aplicada!");

                    return(true);
                }
            }
            return(false);
        }