Ejemplo n.º 1
0
 /// <summary>
 /// Adds one to the count of the key, if they are present in the dictionary, else initialize the key with 1.
 ///  Raises the event OnAddStatus if the key was not present and OnUpdateStatus if the key was already in the dictionary.
 /// </summary>
 /// <param name="key"></param>
 public void AddOne(string key)
 {
     //Console.WriteLine(key);
     try
     {
         if (Keys.ContainsKey(key))
         {
             Keys[key]++;
             OnUpdateStatus?.Invoke(this, new DictionaryEventArgs(key, false));
         }
         else
         {
             this.Add(key, 1);
         }
     }
     catch (Exception e)
     {
         if (e is ChainingException exception)
         {
             exception.AddErrorToChain("While trying to increment or create entry in count dictionary");
         }
         else
         {
             exception = new ChainingException(e.Message);
             exception.AddErrorToChain("While trying to increment or create entry in count dictionary");
         }
         throw exception;
     }
 }
Ejemplo n.º 2
0
        static private ManagementObject GetVolume(string PartitionID)
        {
            ManagementScope scope = new ManagementScope("\\\\.\\ROOT\\Microsoft\\Windows\\Storage");
            ObjectQuery     query = new ObjectQuery("SELECT * FROM MSFT_PartitionToVolume");

            ManagementObjectSearcher   searcher        = new ManagementObjectSearcher(scope, query);
            ManagementObjectCollection queryCollection = searcher.Get();
            ManagementObject           Volume          = null;

            foreach (ManagementObject m in queryCollection)
            {
                ManagementObject Partition = new ManagementObject(Convert.ToString(m["Partition"]));
                if (Convert.ToString(Partition["UniqueId"]) == PartitionID)
                {
                    Volume = new ManagementObject(Convert.ToString(m["Volume"]));
                    break;
                }
            }

            if (Volume == null)
            {
                OnUpdateStatus?.Invoke("Cannot find Volume in MSFT_PartitionToVolume");
                return(null);
            }

            return(Volume);
        }
Ejemplo n.º 3
0
        public void GetChangedStatus()
        {
            List <User> users = xmlInterpreter.GetChangedStatus();

            if (users.Count == 0)
            {
                return;
            }
            OnUpdateStatus?.Invoke(this, users);
        }
Ejemplo n.º 4
0
        public async Task StopAsync()
        {
            // update context
            await _executeContext.StopAsync();

            // todo: teardown running dframe?

            // update status
            OnUpdateStatus?.Invoke(_executeContext.Status);
        }
Ejemplo n.º 5
0
        static private Int64 CreateMBRPartition(ManagementObject DiskToMess, UInt64?Size, MBRPartType Type, FileSystem FS, bool Active, string Label, out string Letter)
        {
            Letter = "";

            ManagementBaseObject CreParMethod = DiskToMess.GetMethodParameters("CreatePartition");

            CreParMethod["Alignment"]         = 4096;
            CreParMethod["AssignDriveLetter"] = true;
            CreParMethod["IsActive"]          = Active;

            CreParMethod["MbrType"] = (UInt16)Type;
            if (Size == null)
            {
                CreParMethod["UseMaximumSize"] = true;
            }
            else
            {
                CreParMethod["Size"] = Size.Value;
            }

            ManagementBaseObject Output = DiskToMess.InvokeMethod("CreatePartition", CreParMethod, null);

            if (Convert.ToInt64(Output["ReturnValue"]) != 0)
            {
                return(Convert.ToInt64(Output["ReturnValue"]));
            }

            ManagementBaseObject Partition = Output["CreatedPartition"] as ManagementBaseObject;
            string PartitionID             = Partition["UniqueId"].ToString();

            ManagementObject Volume = GetVolume(PartitionID);

            if (Volume == null)
            {
                return(3);
            }

            Letter = Convert.ToString(Volume["DriveLetter"]);

            if (FS != FileSystem.NoFormat)
            {
                OnUpdateStatus?.Invoke("Creating filesystem (" + FS.ToString() + ")");

                Int64 res = FormatVolume(Volume, Label, FS);
                if (res != 0)
                {
                    return(res);
                }
            }

            return(0);
        }
Ejemplo n.º 6
0
        public async Task ExecuteAsync()
        {
            _errorNotifier.Clear();

            // update status
            await _executeContext.ExecuteAsync();

            OnUpdateStatus?.Invoke(_executeContext.Status);

            var sw = ValueStopwatch.StartNew();

            // todo: specify IScalingProvider
            // run dframe
            await Host.CreateDefaultBuilder(_executeContext.Argument.Arguments)
            .ConfigureLogging(logging =>
            {
                logging.ClearProviders();
                logging.SetMinimumLevel(_loggingService.ExecuteLogProcessor.LogLevel);
                logging.AddZLoggerLogProcessor(_loggingService.ExecuteLogProcessor);
                logging.AddZLoggerLogProcessor(_errorNotifier);
                // todo: remove console logger?
                logging.AddZLoggerConsole();
            })
            .RunDFrameLoadTestingAsync(_executeContext.Argument.Arguments !, new DFrameOptions(_executeContext.Argument.HostAddress, 12345));

            var duration = sw.Elapsed;

            // update status
            if (_errorNotifier.GetExceptions().Length == 0)
            {
                await _executeContext.StopAsync();
            }
            else
            {
                await _executeContext.ErrorAsync();
            }
            OnUpdateStatus?.Invoke(_executeContext.Status);

            // record
            using (var scope = _serviceProvider.CreateScope())
            {
                // todo: requests and errors will obtain after progress implementation
                var profiler = scope.ServiceProvider.GetService <IDFrameProfiler>();
                await profiler.InsertAsync(_executeContext.ExecuteId, _executeContext.Argument.WorkerName, _executeContext.Argument.Arguments, 0, 0, duration, default);
            }
        }
Ejemplo n.º 7
0
 /// <summary>
 /// stop the proxy and free up resources
 /// </summary>
 /// <returns></returns>
 public async Task <bool> Stop()
 {
     return(await Task.Run(() =>
     {
         try
         {
             Proxy.Stop();
             OnUpdateStatus?.Invoke(this, new ProxyStatusEventsArguments()
             {
                 Status = ProxyStatus.Stopped
             });
             return true;
         }
         catch (Exception e)
         {
             Helpers.YTrackLogger.Log("Could not stop Titanium : " + e.Message + "\n\n" + e.StackTrace);
             return false;
         }
     }));
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Start the proxy and free up resources
 /// </summary>
 /// <returns></returns>
 public async Task <bool> Start(int runningPort)
 {
     return(await Task.Run(() =>
     {
         try
         {
             Proxy.Start();
             Proxy.SetAsSystemHttpProxy(_instance.explicitProxyEndPoint);
             Proxy.SetAsSystemHttpsProxy(_instance.explicitProxyEndPoint);
             OnUpdateStatus?.Invoke(this, new ProxyStatusEventsArguments()
             {
                 Status = ProxyStatus.Started
             });
             return true;
         }
         catch (Exception e)
         {
             Helpers.YTrackLogger.Log("Could not start Titanium : " + e.Message + "\n\n" + e.StackTrace);
             return false;
         }
     }));
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Remove the key from the dictionary
 /// Raises the event OnUpdateStatus
 /// </summary>
 /// <param name="key"></param>
 /// <returns>true if the operation was successful, false otherwise</returns>
 public bool Remove(string key)
 {
     try
     {
         bool result = Keys.TryRemove(key, out _);
         OnUpdateStatus?.Invoke(this, new DictionaryEventArgs(key, true));
         return(result);
     }
     catch (Exception e)
     {
         if (e is ChainingException exception)
         {
             exception.AddErrorToChain("While trying to remove key from count dictionary");
         }
         else
         {
             exception = new ChainingException(e.Message);
             exception.AddErrorToChain("While trying to remove key from count dictionary");
         }
         throw exception;
     }
 }
Ejemplo n.º 10
0
        public void rewriteTags(string[] images, string outputFolder, List <ColumnData> tags)
        {
            ReTagError err;
            //https://dejanstojanovic.net/aspnet/2014/november/adding-extra-info-to-an-image-file/
            //For each provided file, find matching row in the ColumnData, the read,modify,write
            //Do not update image if dest == source
            var fileNameColumn = tags.Where(c => c.ColumnTag == EXIFTag.FileName).ToArray();

            if (fileNameColumn.Length != 1)
            {
                err.errorMessage = "No file name mapping column selected";
                err.failingFile  = "";
                OnFinish?.Invoke(this, false, err);
                return;
            }
            var tagsToUpdate = tags.Where(c => c.ColumnTag != EXIFTag.Ignored && c.ColumnTag != EXIFTag.FileName).ToArray();

            for (int index = 0; index < images.Length; index++)
            {
                var fileName = "";
                {
                    System.IO.FileInfo fi = new System.IO.FileInfo(images[index]);
                    fileName = fi.Name;
                }
                var itemRow = fileNameColumn[0].cells.FindIndex(fName => fName == fileName);
                if (itemRow == -1)
                {
                    // no matches

                    err.errorMessage = "Provided Image could not be matched";
                    err.failingFile  = fileName;
                    OnFinish?.Invoke(this, false, err);
                    return;
                }
                //Update file
                List <UpdateMetaPair> updatedTags = new List <UpdateMetaPair> {
                };
                foreach (ColumnData c in tagsToUpdate)
                {
                    updatedTags.Add(new UpdateMetaPair
                    {
                        tag   = c.ColumnTag,
                        value = c.cells[itemRow]
                    });
                }
                //calculate output filename as rebasing base onto the output folder

                err = FileRetag.ReTagImage(images[index], System.IO.Path.Combine(outputFolder, fileName), updatedTags);
                if (err.errorMessage.Length > 0)
                {
                    OnFinish?.Invoke(this, false, err);
                    return;
                }

                OnUpdateStatus?.Invoke(this, ((float)index / (float)images.Length) * 100);
                //Every so often, run a GC pass over the data
                if (index % 3 == 0)
                {
                    GC.Collect();
                }
            }
            err.errorMessage = "";
            err.failingFile  = "";
            OnFinish?.Invoke(this, true, err);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Run the GA for a specific number of generations.
        /// </summary>
        /// <param name="_generations">The number of generations to run for</param>
        public Chromosome Run(int _generations)
        {
            // Build a pool of random chromos
            m_fullPool    = new List <Chromosome>();
            m_workingPool = new List <Chromosome>();
            MaxGeneration = _generations;

            // Generate the two pools of chromosomes
            for (var i = 0; i < PoolSize; i++)
            {
                var chromo = new Chromosome(m_template);
                chromo.Randomize();
                m_workingPool.Add(chromo);   // first operation in loop is to swap pools!

                chromo = new Chromosome(m_template);
                chromo.Randomize();
                m_fullPool.Add(chromo);   // Don't care about these counts right now
            }


            // Loop through the generations
            var updateTime = DateTime.Now;

            for (Generation = 0; Generation < MaxGeneration; Generation++)
            {
                m_workingIndex = 0;

                // Switch pools new pool by turning the current one into a working pool
                m_fullPool = m_fullPool.Swap(ref m_workingPool);

                // Sort the new pool (generated from previous generation's working pool)
                m_fullPool.Sort();

                // Let the application have its chance at the new working pool. This can be used
                // to implement special kinds of elitism or other operations which add
                // chromosomes to the working pool.
                OnGenerationStart?.Invoke(this);   // Assumes Application will sort pool properly if it changes sort order

                // If its time to end this, then do so now. If the application calls StopNow(),
                // this will be how we know to stop
                if (DateTime.Now >= StopTime)
                {
                    break;
                }

                // Update the Application if its time to do so. Assume that the application
                // doesn't modify anything in the GA during this event.
                if (DateTime.Now >= updateTime)
                {
                    GenerationsSinceLastUpdate = Generation - m_lastUpdateCount;
                    m_lastUpdateCount          = Generation;
                    OnUpdateStatus?.Invoke(this);
                    updateTime += new TimeSpan(0, 0, 0, 0, UpdateInterval);
                }

                Chromosome working;

                // Handle Elitism
                for (var i = 0; i < ElitismCount; i++)
                {
                    working = GetWorkingChromosome();
                    if (working != null) // may have run out already... that would be lame.
                    {
                        working.FromOther(m_fullPool[i]);
                    }
                    else
                    {
                        break;
                    }
                }

                // Handle Xover and Mutation
                while ((working = GetWorkingChromosome()) != null) // while there are working chromosomes to fill
                {
                    // Assumes that "value" is an error value, so use the "inverse" sampling to
                    // favor small values.
                    var first = Sample();

                    if (Rng.Default.NextDouble() < MutationRate) // mutation only
                    {
                        working.FromOther(first);
                        working.Mutate(MutationStrength);
                    }
                    else // crossover
                    {
                        var second = Sample();

                        if (Rng.Default.NextBool())
                        {
                            working.CrossoverSplice(first, second);
                        }
                        else
                        {
                            working.CrossoverBits(first, second);
                        }

                        if (Rng.Default.NextDouble() < MutationRate) // mutation
                        {
                            working.Mutate(MutationStrength);
                        }
                    }
                }
            }
            GenerationsSinceLastUpdate = Generation - m_lastUpdateCount;
            OnUpdateStatus?.Invoke(this);


            m_fullPool.Sort();
            // Return the "best" chromosome in the pool
            return(m_fullPool[0]);
        }
Ejemplo n.º 12
0
        static public Int64 CreatePartitions(string UID, int Schema, out string BootLetter, out string SystemLetter)
        {
            BootLetter   = "";
            SystemLetter = "";

            OnUpdateStatus?.Invoke("Querying disk");
            ManagementScope scope = new ManagementScope("\\\\.\\ROOT\\Microsoft\\Windows\\Storage");
            ObjectQuery     query = new ObjectQuery("SELECT * FROM MSFT_Disk WHERE UniqueId = '" + EscapeString(UID) + "'");

            ManagementObjectSearcher   searcher        = new ManagementObjectSearcher(scope, query);
            ManagementObjectCollection queryCollection = searcher.Get();
            ManagementObject           DiskToMess      = null;

            foreach (ManagementObject m in queryCollection)
            {
                DiskToMess = m;
                break;
            }

            if (DiskToMess == null)
            {
                return(2);
            }

            Int64 res;

            switch (Schema)
            {
            case 0:     //Legacy
            case 1:     //EFI
                break;

            default:
                return(13);
            }

            OnUpdateStatus?.Invoke("Clearing disk");
            res = ClearDisk(DiskToMess);
            if (res != 0)
            {
                return(res);
            }

            OnUpdateStatus?.Invoke("Converting Schema");
            res = InitializeDisk(DiskToMess, Schema);
            if (res != 0)
            {
                return(res);
            }

            string MSRLetter = ""; //dummy

            switch (Schema)
            {
            case 0:
                OnUpdateStatus?.Invoke("Creating Boot Partition");
                res = CreateMBRPartition(DiskToMess, 134217728, MBRPartType.IFS, FileSystem.NTFS, true, "Boot", out BootLetter);
                if (res != 0)
                {
                    return(res);
                }
                OnUpdateStatus?.Invoke("Creating Data Partition");
                res = CreateMBRPartition(DiskToMess, null, MBRPartType.IFS, FileSystem.NTFS, false, "SYSTEM", out SystemLetter);
                if (res != 0)
                {
                    return(res);
                }
                break;

            case 1:
                OnUpdateStatus?.Invoke("Creating EFI Partition");
                res = CreateEFIPartition(DiskToMess, 134217728, EFIPartType.EFI, FileSystem.FAT32, "EFI", out BootLetter);
                if (res != 0)
                {
                    return(res);
                }
                OnUpdateStatus?.Invoke("Creating MSR Partition");
                res = CreateEFIPartition(DiskToMess, 134217728, EFIPartType.MSR, FileSystem.NoFormat, "MSR", out MSRLetter);
                if (res != 0)
                {
                    return(res);
                }
                OnUpdateStatus?.Invoke("Creating Data Partition");
                res = CreateEFIPartition(DiskToMess, null, EFIPartType.Pri, FileSystem.NTFS, "SYSTEM", out SystemLetter);
                if (res != 0)
                {
                    return(res);
                }
                break;
            }

            return(0);
        }
Ejemplo n.º 13
0
        static private Int64 CreateEFIPartition(ManagementObject DiskToMess, UInt64?Size, EFIPartType Type, FileSystem FS, string Label, out string Letter)
        {
            Letter = "";

            ManagementBaseObject CreParMethod = DiskToMess.GetMethodParameters("CreatePartition");

            CreParMethod["Alignment"] = 4096;
            if (Type != EFIPartType.MSR)
            {
                CreParMethod["AssignDriveLetter"] = true;
            }
            string ParID = "";

            switch (Type)
            {
            case EFIPartType.EFI: ParID = "{c12a7328-f81f-11d2-ba4b-00a0c93ec93b}"; break;

            case EFIPartType.MSR: ParID = "{e3c9e316-0b5c-4db8-817d-f92df00215ae}"; break;

            case EFIPartType.Pri: ParID = "{ebd0a0a2-b9e5-4433-87c0-68b6b72699c7}"; break;

            case EFIPartType.Recovery: ParID = "{de94bba4-06d1-4d40-a16a-bfd50179d6ac}"; break;

            default: ParID = ""; break;
            }

            CreParMethod["GptType"] = ParID;
            if (Size == null)
            {
                CreParMethod["UseMaximumSize"] = true;
            }
            else
            {
                CreParMethod["Size"] = Size.Value;
            }

            ManagementBaseObject Output = DiskToMess.InvokeMethod("CreatePartition", CreParMethod, null);

            if (Convert.ToInt64(Output["ReturnValue"]) != 0)
            {
                return(Convert.ToInt64(Output["ReturnValue"]));
            }

            if (Type == EFIPartType.MSR)
            {
                return(0);
            }

            ManagementBaseObject Partition = Output["CreatedPartition"] as ManagementBaseObject;
            string PartitionID             = Partition["UniqueId"].ToString();

            ManagementObject Volume = GetVolume(PartitionID);

            if (Volume == null)
            {
                return(3);
            }

            Letter = Convert.ToString(Volume["DriveLetter"]);

            if (FS != FileSystem.NoFormat)
            {
                OnUpdateStatus?.Invoke("Creating filesystem (" + FS.ToString() + ")");

                Int64 res = FormatVolume(Volume, Label, FS);
                if (res != 0)
                {
                    return(res);
                }
            }

            return(0);
        }