Exemplo n.º 1
0
        public void SaveXRefList(bool createBackup, bool force)
        {
            if (force || BlockListMgr.Dirty || IOAddressMgr.Dirty || CompoundBlockMgr.Dirty || BuildListMgr.Dirty)
            {
                string XRefTmpPath = Path.ChangeExtension(XRefListPath, "tmp");
                string XRefBakPath = Path.ChangeExtension(XRefListPath, "bak");

                try
                {
                    XmlDocument doc = new XmlDocument();
                    if (File.Exists(XRefListPath))
                    {
                        if (createBackup)
                        {
                            //create a temporary copy of the existing file version
                            try { File.Delete(XRefTmpPath); }
                            catch (Exception) { }
                            File.Copy(XRefListPath, XRefTmpPath);
                        }

                        doc.Load(XRefListPath);
                    }

                    if (doc.DocumentElement == null || doc.DocumentElement.Name != "xref")
                    {
                        doc.RemoveAll();
                        doc.AppendChild(doc.CreateElement("xref"));
                    }

                    //save block list
                    XmlNode node = doc.SelectSingleNode("xref//Blocks");
                    if (node == null)
                    {
                        node = doc.CreateElement("Blocks");
                        doc.DocumentElement.AppendChild(node);
                    }
                    node.RemoveAll();
                    List <Block> listOfBlocks = BlockListMgr.GetBlockList2();
                    foreach (var item in listOfBlocks)
                    {
                        node.AppendChild(item.GetXmlBlockNode(doc));
                    }

                    //save I/O connections
                    node = doc.SelectSingleNode("xref//IOConnections");
                    if (node == null)
                    {
                        node = doc.CreateElement("IOConnections");
                        doc.DocumentElement.AppendChild(node);
                    }
                    node.RemoveAll();
                    List <IOConnection> listOfIOConnections = IOAddressMgr.GetIOConnectionList2();
                    foreach (var item in listOfIOConnections)
                    {
                        XmlNode newConnection = item.GetXmlConnectionNode(doc);
                        if (newConnection != null)
                        {
                            node.AppendChild(newConnection);
                        }
                    }

#if true
                    //shared memory variables is saved to xshm.data later
                    node = doc.SelectSingleNode("xref//SharedMemory");
                    if (node != null)
                    {
                        node.ParentNode.RemoveChild(node);
                    }
#else
                    //also save shared memory variables into xref.data to make ViGET happy.
                    node = doc.SelectSingleNode("xref//SharedMemory");
                    if (node == null)
                    {
                        node = doc.CreateElement("SharedMemory");
                        doc.DocumentElement.AppendChild(node);
                    }
                    node.RemoveAll();
                    SharedMemoryVariableList listOfShmVars = null;
                    SharedMemoryMgr.GetShmVariableList(ref listOfShmVars);
                    foreach (var item in listOfShmVars)
                    {
                        SharedMemoryConnectionList listOfConns = null;
                        SharedMemoryMgr.GetShmConnectionList(ref listOfConns);
                        XmlNode newVariable = item.GetXmlShmVariableNode(doc, listOfConns);
                        if (newVariable != null)
                        {
                            node.AppendChild(newVariable);
                        }
                    }
#endif

                    //save compound block list
                    node = doc.SelectSingleNode("xref//CompoundBlocks");
                    if (node == null)
                    {
                        node = doc.CreateElement("CompoundBlocks");
                        doc.DocumentElement.AppendChild(node);
                    }
                    node.RemoveAll();
                    List <CompoundBlock> compoundblocklist = CompoundBlockMgr.GetCompBlockList2();
                    foreach (var item in compoundblocklist)
                    {
                        node.AppendChild(item.GetXmlBlockNode(doc));
                    }

                    //lsu 20120509
                    node = doc.SelectSingleNode("xref//BuildManager");
                    if (node == null)
                    {
                        node = doc.CreateElement("BuildManager");
                        doc.DocumentElement.AppendChild(node);
                    }
                    node.RemoveAll();
                    foreach (var item in BuildListMgr.GetPlanList())
                    {
                        node.AppendChild(item.GetXmlBlockNode(doc));
                    }

                    doc.Save(XRefListPath);

                    if (createBackup)
                    {
                        //compare old and new file version; if different, keep the old version as xref.bak
                        if (HasDifferences(XRefListPath, XRefTmpPath))
                        {
                            try { File.Delete(XRefBakPath); }
                            catch (Exception) { }
                            File.Move(XRefTmpPath, XRefBakPath);
                        }
                        else
                        {
                            try { File.Delete(XRefTmpPath); }
                            catch (Exception) { }
                        }
                    }

                    // reset dirty flag
                    BlockListMgr.Dirty     = false;
                    IOAddressMgr.Dirty     = false;
                    CompoundBlockMgr.Dirty = false;
                    BuildListMgr.Dirty     = false;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    //logger.Error(e.Message + " at " + e.StackTrace);
                }
            }

            if (force || SharedMemoryMgr.Dirty)
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    if (File.Exists(XShmListPath))
                    {
                        doc.Load(XShmListPath);
                    }

                    if (doc.DocumentElement == null || doc.DocumentElement.Name != "xref")
                    {
                        doc.RemoveAll();
                        doc.AppendChild(doc.CreateElement("xref"));
                    }

                    //save shared memory variables
                    XmlNode node = doc.SelectSingleNode("xref//SharedMemory");
                    if (node == null)
                    {
                        node = doc.CreateElement("SharedMemory");
                        doc.DocumentElement.AppendChild(node);
                    }
                    node.RemoveAll();
                    SharedMemoryVariableList listOfShmVars = null;
                    SharedMemoryMgr.GetShmVariableList(ref listOfShmVars);
                    foreach (var item in listOfShmVars)
                    {
                        SharedMemoryConnectionList listOfConns = null;
                        SharedMemoryMgr.GetShmConnectionList(ref listOfConns);
                        XmlNode newVariable = item.GetXmlShmVariableNode(doc, listOfConns);
                        if (newVariable != null)
                        {
                            node.AppendChild(newVariable);
                        }
                    }

                    doc.Save(XShmListPath);

                    // reset dirty flag
                    SharedMemoryMgr.Dirty = false;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    //logger.Error(e.Message + " at " + e.StackTrace);
                }
            }
        }