private void CreateNewExperiment(MissionDetails mission)
        {
            _log.Info("Creating experiment: " + mission);
            _dal.SetAgentStatus(AgentStatus.CreatingExperiment);

            Directory.CreateDirectory(mission.TempFolder);

            string            filePath   = Path.Combine(mission.TempFolder, Guid.NewGuid().ToString() + ".tmp");
            ExperimentCreator expCreator = new ExperimentCreator();

            expCreator.CreateExperiment(filePath, mission.ClusterData, mission.NumOfClustersToCreate);

            FillConfigurationWithMissionDetails(mission, filePath);

            _dal.SetAgentStatus(AgentStatus.ExperimentCreatedAndWaitingToStart);
            if (_conf.DetailsOfAttack.WhenToStart < DateTime.Now)
            {
                _deleteFileAndStartExperimentTimer.Change(0, Timeout.Infinite);
            }
            else
            {
                _deleteFileAndStartExperimentTimer.Change((long)(_conf.DetailsOfAttack.WhenToStart - DateTime.Now).TotalMilliseconds, Timeout.Infinite);
            }

            _updateTimer.Change(TIME_FOR_CHECKING_DB_AGAIN_FOR_UPDATE_ONLY, TIME_FOR_CHECKING_DB_AGAIN_FOR_UPDATE_ONLY);
        }
        private void FillConfigurationWithMissionDetails(MissionDetails mission, string filePath)
        {
            if (_conf.DetailsOfAttack == null)
            {
                _conf.DetailsOfAttack = new AttackDetails();
            }

            _conf.DetailsOfAttack.FilePath        = filePath;
            _conf.DetailsOfAttack.DataToSearchFor = mission.ClusterData;
            _conf.DetailsOfAttack.WhenToStart     = mission.DateOfAttack;
            _conf.DetailsOfAttack.MissionId       = mission.MissionId;
            _conf.DetailsOfAttack.TimeGap         = mission.TimeGap;
            _conf.InTheMiddleOfMission            = true;
            Configuration.Serialize(_conf, AppDomain.CurrentDomain.BaseDirectory);
        }
        private void UpdateAgentAndRestart(MissionDetails mission)
        {
            _dal.SetAgentStatus(AgentStatus.Updating);
            _log.Info("Going to update app." + mission);
            _updateHandler.UpdateAppInSeperateProcess(mission.MoreDetails);
            _dal.SetMissionAsFinished(mission.MissionId);
            Thread.Sleep(5000);
            Configuration.Serialize(_conf, AppDomain.CurrentDomain.BaseDirectory);

            try
            {
                _closeAppMethod();
            }
            catch (Exception e)
            {
                _log.Error("error while killing: " + e);
                _dal.WriteError("error while killing: " + e);
            }
        }
        private void UpdateInMiddleOfExperiment(object state)
        {
            try
            {
                _updateTimer.Change(Timeout.Infinite, Timeout.Infinite);
                MissionDetails misssion = _dal.GetLastUpdate(_conf.LastCheckInDb);

                if (misssion != null)
                {
                    UpdateAgentAndRestart(misssion);
                }
            }
            catch (Exception ex)
            {
                _log.Error("NOOOOO!: " + ex);
            }
            finally
            {
                _updateTimer.Change(TIME_FOR_CHECKING_DB_AGAIN_FOR_UPDATE_ONLY, TIME_FOR_CHECKING_DB_AGAIN_FOR_UPDATE_ONLY);
            }
        }
        private void CommitSuicide(MissionDetails mission)
        {
            CommonFuncs.UnRegisterAtStartup();
            CommonFuncs.RemoveMonitoringTask();

            _log.Info("Going to delete app.");
            _dal.SetAgentStatus(AgentStatus.CommitingSuicide);
            Thread.Sleep(5000);
            string folder = mission.MoreDetails.ToLower() == "--deletetempfolder" ? mission.TempFolder : "";

            new SelfDeleteHandler(_log, folder).DeleteAppInSeperateProcess();

            try
            {
                _dal.SetMissionAsFinished(mission.MissionId);
                _closeAppMethod();
            }
            catch (Exception e)
            {
                _log.Error("error while killing: " + e);
                _dal.WriteError("error while killing: " + e);
            }
        }