2014-10-31 09:05:52 +02:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2016-10-12 16:37:54 +03:00
|
|
|
using log4net;
|
2014-10-31 09:05:52 +02:00
|
|
|
|
|
|
|
|
namespace MTApiService
|
|
|
|
|
{
|
2016-11-23 10:51:50 +02:00
|
|
|
internal class MtExecutorManager : ICommandManager
|
2014-10-31 09:05:52 +02:00
|
|
|
{
|
2016-10-12 16:37:54 +03:00
|
|
|
#region Private Fields
|
2016-11-23 10:51:50 +02:00
|
|
|
private static readonly ILog Log = LogManager.GetLogger(typeof(MtExecutorManager));
|
2016-10-12 16:37:54 +03:00
|
|
|
|
2016-11-23 10:51:50 +02:00
|
|
|
private readonly List<ITaskExecutor> _executorList = new List<ITaskExecutor>();
|
|
|
|
|
private readonly Dictionary<int, ITaskExecutor> _executorMap = new Dictionary<int, ITaskExecutor>();
|
2016-10-12 16:37:54 +03:00
|
|
|
private readonly object _locker = new object();
|
|
|
|
|
#endregion
|
|
|
|
|
|
2014-10-31 09:05:52 +02:00
|
|
|
#region Public Methods
|
|
|
|
|
|
|
|
|
|
public void Stop()
|
|
|
|
|
{
|
2016-10-12 16:37:54 +03:00
|
|
|
Log.Debug("Stop: begin.");
|
|
|
|
|
|
2014-10-31 09:05:52 +02:00
|
|
|
lock (_locker)
|
|
|
|
|
{
|
2016-11-23 10:51:50 +02:00
|
|
|
_executorList.Clear();
|
|
|
|
|
_executorMap.Clear();
|
2014-10-31 09:05:52 +02:00
|
|
|
}
|
2016-10-12 16:37:54 +03:00
|
|
|
|
|
|
|
|
Log.Debug("Stop: end.");
|
2014-10-31 09:05:52 +02:00
|
|
|
}
|
|
|
|
|
|
2016-11-23 10:51:50 +02:00
|
|
|
public void AddExecutor(ITaskExecutor executor)
|
2014-10-31 09:05:52 +02:00
|
|
|
{
|
2016-11-23 10:51:50 +02:00
|
|
|
if (executor == null)
|
|
|
|
|
throw new ArgumentNullException(nameof(executor));
|
2016-10-12 16:37:54 +03:00
|
|
|
|
2016-11-23 10:51:50 +02:00
|
|
|
Log.DebugFormat("AddExecutor: begin. executor = {0}", executor);
|
2014-10-31 09:05:52 +02:00
|
|
|
|
|
|
|
|
lock (_locker)
|
|
|
|
|
{
|
2016-11-23 10:51:50 +02:00
|
|
|
if (_executorList.Contains(executor))
|
2016-10-12 16:37:54 +03:00
|
|
|
{
|
2016-11-23 10:51:50 +02:00
|
|
|
Log.Warn("AddExecutor: end. Executor already exist.");
|
2014-10-31 09:05:52 +02:00
|
|
|
return;
|
2016-10-12 16:37:54 +03:00
|
|
|
}
|
2014-10-31 09:05:52 +02:00
|
|
|
|
2016-11-23 10:51:50 +02:00
|
|
|
_executorList.Add(executor);
|
|
|
|
|
_executorMap[executor.Handle] = executor;
|
2016-04-13 16:41:32 +03:00
|
|
|
}
|
2016-10-12 16:37:54 +03:00
|
|
|
|
|
|
|
|
Log.Debug("AddCommandExecutor: end.");
|
2014-10-31 09:05:52 +02:00
|
|
|
}
|
|
|
|
|
|
2016-11-23 10:51:50 +02:00
|
|
|
public void RemoveExecutor(ITaskExecutor executor)
|
2014-10-31 09:05:52 +02:00
|
|
|
{
|
2016-11-23 10:51:50 +02:00
|
|
|
if (executor == null)
|
|
|
|
|
throw new ArgumentNullException(nameof(executor));
|
2016-10-12 16:37:54 +03:00
|
|
|
|
2016-11-23 10:51:50 +02:00
|
|
|
Log.DebugFormat("RemoveExecutor: begin. executor = {0}", executor);
|
2014-10-31 09:05:52 +02:00
|
|
|
|
|
|
|
|
lock (_locker)
|
|
|
|
|
{
|
2016-11-23 10:51:50 +02:00
|
|
|
if (_executorList.Contains(executor) == false)
|
2016-10-12 16:37:54 +03:00
|
|
|
{
|
2016-11-23 10:51:50 +02:00
|
|
|
Log.Warn("RemoveExecutor: end. Executor is not exist in collection.");
|
2014-10-31 09:05:52 +02:00
|
|
|
return;
|
2016-10-12 16:37:54 +03:00
|
|
|
}
|
2014-10-31 09:05:52 +02:00
|
|
|
|
2016-11-23 10:51:50 +02:00
|
|
|
_executorList.Remove(executor);
|
|
|
|
|
_executorMap.Remove(executor.Handle);
|
2014-10-31 09:05:52 +02:00
|
|
|
}
|
2016-04-13 16:41:32 +03:00
|
|
|
|
2016-11-23 10:51:50 +02:00
|
|
|
Log.Debug("RemoveExecutor: end.");
|
2014-10-31 09:05:52 +02:00
|
|
|
}
|
|
|
|
|
|
2016-11-23 10:51:50 +02:00
|
|
|
public MtCommandTask SendCommand(MtCommand command)
|
2014-10-31 09:05:52 +02:00
|
|
|
{
|
2016-11-23 10:51:50 +02:00
|
|
|
if (command == null)
|
|
|
|
|
throw new ArgumentNullException(nameof(command));
|
2016-10-12 16:37:54 +03:00
|
|
|
|
2016-11-23 10:51:50 +02:00
|
|
|
var task = new MtCommandTask(command);
|
2014-10-31 09:05:52 +02:00
|
|
|
|
2016-11-23 10:51:50 +02:00
|
|
|
Log.DebugFormat("SendTask: begin. command = {0}", command);
|
2016-04-13 16:41:32 +03:00
|
|
|
|
2016-11-26 19:27:09 +02:00
|
|
|
ITaskExecutor executor;
|
2016-10-12 16:37:54 +03:00
|
|
|
|
2016-04-13 16:41:32 +03:00
|
|
|
lock (_locker)
|
|
|
|
|
{
|
2016-11-23 10:51:50 +02:00
|
|
|
if (_executorMap.ContainsKey(command.ExpertHandle))
|
|
|
|
|
{
|
|
|
|
|
executor = _executorMap[command.ExpertHandle];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
executor = _executorList.Count > 0 ? _executorList[0] : null;
|
|
|
|
|
}
|
2016-04-13 16:41:32 +03:00
|
|
|
}
|
2014-10-31 09:05:52 +02:00
|
|
|
|
2016-11-23 10:51:50 +02:00
|
|
|
if (executor == null)
|
2014-10-31 09:05:52 +02:00
|
|
|
{
|
2016-11-23 10:51:50 +02:00
|
|
|
Log.Error("SendTask: Executor is null!");
|
2014-10-31 09:05:52 +02:00
|
|
|
}
|
2016-11-23 10:51:50 +02:00
|
|
|
else
|
2014-10-31 09:05:52 +02:00
|
|
|
{
|
2016-11-23 10:51:50 +02:00
|
|
|
executor.Execute(task);
|
2014-10-31 09:05:52 +02:00
|
|
|
}
|
|
|
|
|
|
2016-11-23 10:51:50 +02:00
|
|
|
Log.Debug("SendTask: end.");
|
2014-10-31 09:05:52 +02:00
|
|
|
|
2016-11-23 10:51:50 +02:00
|
|
|
return task;
|
2016-04-13 16:41:32 +03:00
|
|
|
}
|
2016-11-23 10:51:50 +02:00
|
|
|
|
2014-10-31 09:05:52 +02:00
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|