Ejemplo n.º 1
0
 private void CheckClientExecuteActiveCommand(ActiveCommandInfo activeCommandInfo, Client client)
 {
     if (activeCommandInfo.DynamicCommand.Status == DynamicCommandStatus.Stopped)
     {
         client.StopActiveCommand(activeCommandInfo.DynamicCommand.Id);
     }
 }
        private void ActiveCommandEventManagerOnActiveCommandRemoved(object sender, ActiveCommandInfo activeCommandInfo)
        {
            lock (_listLock)
            {
                for (int i = _activeCommandEvents.Count - 1; i >= 0; i--)
                {
                    var activeCommandEvent = _activeCommandEvents[i];
                    if (activeCommandEvent.ActiveCommandInfo.DynamicCommand.Id == activeCommandInfo.DynamicCommand.Id)
                    {
                        if (activeCommandEvent.ActiveCommandEventType == ActiveCommandEventType.Added)
                        {
                            return;
                        }

                        _activeCommandEvents.Remove(activeCommandEvent);
                    }
                }

                _activeCommandEvents.Add(new ActiveCommandEvent
                {
                    ActiveCommandInfo      = activeCommandInfo,
                    ActiveCommandEventType = ActiveCommandEventType.Removed
                });

                if (!_isTimerRunning)
                {
                    _isTimerRunning = true;
                    _waitTimer.Change(_pushFrequency, Timeout.InfiniteTimeSpan);
                }
            }
        }
Ejemplo n.º 3
0
        public void ReceivedResult(DynamicCommandEvent dynamicCommandEvent, Client client)
        {
            _cacheManager.AddCommandEvent(dynamicCommandEvent);

            if (dynamicCommandEvent.Status == ActivityType.Active || dynamicCommandEvent.Status == ActivityType.Stopped)
            {
                lock (_activeCommandsLock)
                {
                    var activeCommand =
                        ActiveCommands.FirstOrDefault(x => x.DynamicCommand.Id == dynamicCommandEvent.DynamicCommand);
                    if (activeCommand == null)
                    {
                        var dynamicCommand =
                            _databaseManager.GetDynamicCommandById(dynamicCommandEvent.DynamicCommand);
                        if (dynamicCommand == null)
                        {
                            //when there is no command on this server with the id, we stop it because it may be removed by an administrator
                            client.StopActiveCommand(dynamicCommandEvent.DynamicCommand);
                            return;
                        }

                        ActiveCommands.Add(activeCommand = new ActiveCommandInfo(dynamicCommand));
                    }

                    switch (dynamicCommandEvent.Status)
                    {
                    case ActivityType.Active:
                        lock (activeCommand.ClientsLock)
                            activeCommand.Clients.Add(client);
                        ActiveCommandEventManager.AddClient(activeCommand, client);
                        CheckClientExecuteActiveCommand(activeCommand, client);
                        break;

                    case ActivityType.Stopped:
                        lock (activeCommand.ClientsLock)
                            activeCommand.Clients.Remove(client);
                        ActiveCommandEventManager.RemoveClient(activeCommand, client);

                        if (activeCommand.Clients.Count == 0)
                        {
                            ActiveCommands.Remove(activeCommand);
                            if (activeCommand.DynamicCommand.Status == DynamicCommandStatus.Active)
                            {
                                //don't change the status when stopped
                                activeCommand.DynamicCommand.Status = DynamicCommandStatus.Done;
                            }
                            ActiveCommandEventManager.RemoveActiveCommand(activeCommand);
                        }
                        break;
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public void StopActiveCommand(ActiveCommandInfo activeCommandInfo)
        {
            lock (activeCommandInfo.ClientsLock)
            {
                Logger.Info("Stop active command {0} with {1} executing clients", activeCommandInfo.DynamicCommand.Id,
                            activeCommandInfo.Clients.Count);

                foreach (var client in activeCommandInfo.Clients)
                {
                    client.StopActiveCommand(activeCommandInfo.DynamicCommand.Id);
                }

                _databaseManager.SetDynamicCommandStatus(activeCommandInfo.DynamicCommand.Id, DynamicCommandStatus.Stopped);
                activeCommandInfo.DynamicCommand.Status = DynamicCommandStatus.Stopped;
            }
        }
Ejemplo n.º 5
0
 public ActiveCommandClientEventArgs(ActiveCommandInfo activeCommandInfo, Client client)
 {
     ActiveCommandInfo = activeCommandInfo;
     Client            = client;
 }
Ejemplo n.º 6
0
 public void RemoveClient(ActiveCommandInfo activeCommandInfo, Client client)
 {
     ClientRemoved?.Invoke(this, new ActiveCommandClientEventArgs(activeCommandInfo, client));
 }
Ejemplo n.º 7
0
 public void RemoveActiveCommand(ActiveCommandInfo activeCommandInfo)
 {
     ActiveCommandRemoved?.Invoke(this, activeCommandInfo);
 }
Ejemplo n.º 8
0
 public void AddActiveCommand(ActiveCommandInfo activeCommandInfo)
 {
     ActiveCommandAdded?.Invoke(this, activeCommandInfo);
 }
Ejemplo n.º 9
0
        public void OnClientJoin(Client client)
        {
            Logger.Debug("Client CI-{0} joined, check if there are commands to execute", client.Id);

            lock (_dynamicCommandsLock)
            {
                foreach (
                    var command in
                    DynamicCommands.Where(
                        x =>
                        (x.TransmissionEvent.GetType() == typeof(OnJoinTransmissionEvent)) &&
                        IsClientInTargets(client, x.Target) &&
                        CheckConditions(client.GetOnlineClientInformation(), client.ComputerInformation.ClientConfig,
                                        x.Conditions)))
                {
                    if (ExecuteStaticCommand(new[] { client },
                                             DynamicCommandToPotentialCommand(command,
                                                                              _databaseManager.GetDynamicCommandParameter(command.Id)))
                        .Count == 1)
                    {
                        _databaseManager.AddDynamicCommandEvent(command.Id, client.Id, ActivityType.Sent, null);
                    }
                }

                foreach (
                    var command in
                    DynamicCommands.Where(
                        x =>
                        x.TransmissionEvent.GetType() == typeof(EveryClientOnceTransmissionEvent) &&
                        IsClientInTargets(client, x.Target) &&
                        CheckConditions(client.GetOnlineClientInformation(), client.ComputerInformation.ClientConfig,
                                        x.Conditions)))
                {
                    if (!_databaseManager.ClientCommandExecuted(client.Id, command.Id))
                    {
                        if (ExecuteStaticCommand(new[] { client },
                                                 DynamicCommandToPotentialCommand(command,
                                                                                  _databaseManager.GetDynamicCommandParameter(command.Id))).Count == 1)
                        {
                            _databaseManager.AddDynamicCommandEvent(command.Id, client.Id, ActivityType.Sent, null);
                        }
                    }
                }
            }

            if (client.ComputerInformation.ActiveCommands?.Count > 0)
            {
                lock (_activeCommandsLock)
                    foreach (var activeCommandId in client.ComputerInformation.ActiveCommands)
                    {
                        var activeCommand = ActiveCommands.FirstOrDefault(x => x.DynamicCommand.Id == activeCommandId);
                        if (activeCommand == null)
                        {
                            var dynamicCommand =
                                _databaseManager.GetDynamicCommandById(activeCommandId);
                            if (dynamicCommand == null)
                            {
                                //when there is no command on this server with the id, we stop it because it may be removed by an administrator
                                client.StopActiveCommand(activeCommandId);
                                return;
                            }

                            ActiveCommands.Add(activeCommand = new ActiveCommandInfo(dynamicCommand));
                            ActiveCommandEventManager.AddActiveCommand(activeCommand);
                        }

                        lock (activeCommand.ClientsLock)
                            activeCommand.Clients.Add(client);

                        CheckClientExecuteActiveCommand(activeCommand, client);
                        ActiveCommandEventManager.AddClient(activeCommand, client);
                    }
            }
        }