-
Notifications
You must be signed in to change notification settings - Fork 276
/
MtExecutorManager.cs
executable file
·115 lines (90 loc) · 3.23 KB
/
MtExecutorManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Collections.Generic;
using log4net;
namespace MTApiService
{
internal class MtExecutorManager : ICommandManager
{
#region Private Fields
private static readonly ILog Log = LogManager.GetLogger(typeof(MtExecutorManager));
private readonly List<ITaskExecutor> _executorList = new List<ITaskExecutor>();
private readonly Dictionary<int, ITaskExecutor> _executorMap = new Dictionary<int, ITaskExecutor>();
private readonly object _locker = new object();
#endregion
#region Public Methods
public void Stop()
{
Log.Debug("Stop: begin.");
lock (_locker)
{
_executorList.Clear();
_executorMap.Clear();
}
Log.Debug("Stop: end.");
}
public void AddExecutor(ITaskExecutor executor)
{
if (executor == null)
throw new ArgumentNullException(nameof(executor));
Log.DebugFormat("AddExecutor: begin. executor = {0}", executor);
lock (_locker)
{
if (_executorList.Contains(executor))
{
Log.Warn("AddExecutor: end. Executor already exist.");
return;
}
_executorList.Add(executor);
_executorMap[executor.Handle] = executor;
}
Log.Debug("AddCommandExecutor: end.");
}
public void RemoveExecutor(ITaskExecutor executor)
{
if (executor == null)
throw new ArgumentNullException(nameof(executor));
Log.DebugFormat("RemoveExecutor: begin. executor = {0}", executor);
lock (_locker)
{
if (_executorList.Contains(executor) == false)
{
Log.Warn("RemoveExecutor: end. Executor is not exist in collection.");
return;
}
_executorList.Remove(executor);
_executorMap.Remove(executor.Handle);
}
Log.Debug("RemoveExecutor: end.");
}
public MtCommandTask SendCommand(MtCommand command)
{
if (command == null)
throw new ArgumentNullException(nameof(command));
var task = new MtCommandTask(command);
Log.DebugFormat("SendTask: begin. command = {0}", command);
ITaskExecutor executor;
lock (_locker)
{
if (_executorMap.ContainsKey(command.ExpertHandle))
{
executor = _executorMap[command.ExpertHandle];
}
else
{
executor = _executorList.Count > 0 ? _executorList[0] : null;
}
}
if (executor == null)
{
Log.Error("SendTask: Executor is null!");
}
else
{
executor.Execute(task);
}
Log.Debug("SendTask: end.");
return task;
}
#endregion
}
}