Exemplo n.º 1
0
        /// <summary>
        /// This constructor is responsible for instantiating the vlanMapInfo.
        /// </summary>
        /// <param name="txnMng">Transaction manager object.</param>
        /// <param name="VtnHostName">Parent Folder name.</param>
        /// <param name="mode">Write or Read mode in which file to be opened.</param>
        public VLANIDMap(TransactionManager txnMng,
                         string VtnHostName,
                         TransactionManager.OpenMode mode)
        {
            var JavaScriptSerializer = new JavaScriptSerializer();

            JavaScriptSerializer.MaxJsonLength = int.MaxValue;
            StringBuilder json = new StringBuilder("\"vMNetworkName\":\"" + VtnHostName + "\"");

            json.Append(" \"TransactionManager\":" + JavaScriptSerializer.Serialize(txnMng));
            json.Append(" \"OpenMode\":\"" + mode + "\"");
            ODLVSEMETW.EventWriteStartLibrary(
                MethodBase.GetCurrentMethod().Name,
                json.ToString());
            if (txnMng == null)
            {
                throw new ArgumentException(
                          "Parameter 'txnMng' is null or invalid.");
            }
            if (string.IsNullOrWhiteSpace(VtnHostName))
            {
                VSEMConfig vsemConfig = new VSEMConfig();
                txnMng.SetConfigManager(vsemConfig, TransactionManager.OpenMode.ReadMode);
                VtnHostName = vsemConfig.Info.ServerName;
            }
            if (string.IsNullOrWhiteSpace(VtnHostName))
            {
                throw new ArgumentException(
                          "Parameter 'VTNCoordinatorHostName' is null or invalid.");
            }
            try {
                VLANIDMappingConfig vLANIDMappingConfig = new VLANIDMappingConfig(VtnHostName);
                txnMng.SetConfigManager(vLANIDMappingConfig, mode);
                this.vlanMapInfo = vLANIDMappingConfig.VLANIDMapping;
            } catch (System.IO.IOException) {
                ODLVSEMETW.EventWriteConfigManagerFileIOError(MethodBase.GetCurrentMethod().Name,
                                                              configFileIOErrorValidationMessage);
                throw new InvalidOperationException(
                          configFileIOErrorValidationMessage);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initialize of ConfigManager.
        /// </summary>
        /// <param name="mode">Open mode of config file.</param>
        public void Initialize(TransactionManager.OpenMode mode)
        {
            ConfigManagerBase config = null;

            if (mode == TransactionManager.OpenMode.WriteMode)
            {
                FileInfo info = new FileInfo(this.Path);
                if (info.Directory.Exists == false)
                {
                    info.Directory.Create();
                }
            }

            // Open the config file.
            // (Retry max RETRY_COUNT times at RETRY_INTERVAL msec intervals)
            for (var cnt = 1; cnt <= RETRY_COUNT; cnt++)
            {
                try {
                    if (mode == TransactionManager.OpenMode.WriteMode)
                    {
                        // Open the config file by WriteMode.
                        this.FileStreamInfo = new FileStream(this.Path,
                                                             FileMode.OpenOrCreate,
                                                             FileAccess.ReadWrite,
                                                             FileShare.None);
                    }
                    else if (mode == TransactionManager.OpenMode.ReadMode)
                    {
                        // Open the config file by ReadMode.
                        this.FileStreamInfo = new FileStream(this.Path,
                                                             FileMode.Open,
                                                             FileAccess.Read,
                                                             FileShare.Read);
                    }

                    ODLVSEMETW.EventWriteConfigManagerFileOpen(this.Path, (uint)mode);
                    break;
                } catch (Exception e) {
                    // If the config file is locked, retry the open.
                    if (e.HResult != unchecked ((int)FILE_LOCK_FAILED_CODE) || cnt == RETRY_COUNT)
                    {
                        ODLVSEMETW.EventWriteConfigManagerFileIOError(MethodBase.GetCurrentMethod().Name, e.Message);
                        throw;
                    }
                    Thread.Sleep(RETRY_INTERVAL);
                }
            }

            try {
                // If the config file is not empty, get the config info.
                if (this.FileStreamInfo.Length != 0)
                {
                    var dcs = new DataContractSerializer(this.GetType());
                    config = (ConfigManagerBase)dcs.ReadObject(this.FileStreamInfo);
                }

                this.SetEntity(config);
            } catch (Exception e) {
                // The config file closed because failed to get the config info.
                ODLVSEMETW.EventWriteConfigManagerFileIOError(MethodBase.GetCurrentMethod().Name, e.Message);
                this.FileStreamInfo.Close();
                throw;
            }
        }