OperationManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections.Concurrent;
  6. using System.Reflection;
  7. using System.Threading.Tasks;
  8. using Aitex.Core.Util;
  9. using Aitex.Core.RT.DataCenter;
  10. using Aitex.Core.RT.Event;
  11. using Aitex.Core.RT.Log;
  12. using Aitex.Core.WCF;
  13. using MECF.Framework.Common.DataCenter;
  14. using MECF.Framework.Common.OperationCenter;
  15. using MECF.Framework.Common.Utilities;
  16. namespace Aitex.Core.RT.OperationCenter
  17. {
  18. public class OperationManager : ICommonOperation, IOperation
  19. {
  20. private ConcurrentDictionary<string, Func<string, object[], bool>> _operationMap = new ConcurrentDictionary<string, Func<string, object[], bool>>();
  21. private ConcurrentDictionary<string, OperationFunction> _timeOperationMap = new ConcurrentDictionary<string, OperationFunction>();
  22. private Func<object, bool> _isSubscriptionAttribute;
  23. private Func<MemberInfo, bool> _hasSubscriptionAttribute;
  24. private Dictionary<string, List<IInterlockChecker>> _operationInterlockCheck = new Dictionary<string, List<IInterlockChecker>>();
  25. public event Func<string, object[], bool> OnDoOperation;
  26. public OperationManager()
  27. {
  28. }
  29. public void Initialize(bool enableService)
  30. {
  31. _isSubscriptionAttribute = attribute => attribute is SubscriptionAttribute;
  32. _hasSubscriptionAttribute = mi => mi.GetCustomAttributes(false).Any(_isSubscriptionAttribute);
  33. OP.InnerOperationManager = this;
  34. OP.OperationManager = this;
  35. if (enableService)
  36. {
  37. Singleton<WcfServiceManager>.Instance.Initialize(new Type[]
  38. {
  39. typeof(InvokeService)
  40. });
  41. }
  42. }
  43. public void Initialize()
  44. {
  45. Initialize(true);
  46. }
  47. public void Subscribe<T>(T instance, string keyPrefix = null) where T : class
  48. {
  49. if (instance == null)
  50. throw new ArgumentNullException("instance");
  51. Traverse(instance, keyPrefix);
  52. }
  53. public void Subscribe(string key, OperationFunction op)
  54. {
  55. if (string.IsNullOrWhiteSpace(key))
  56. throw new ArgumentNullException("key");
  57. if (_timeOperationMap.ContainsKey(key))
  58. throw new Exception(string.Format("Duplicated Key:{0}", key));
  59. if (op == null)
  60. throw new ArgumentNullException("op");
  61. _timeOperationMap[key] = op;
  62. }
  63. public void Subscribe(string key, Func<string, object[], bool> op)
  64. {
  65. if (string.IsNullOrWhiteSpace(key))
  66. throw new ArgumentNullException("key");
  67. if (_operationMap.ContainsKey(key))
  68. throw new Exception(string.Format("Duplicated Key:{0}", key));
  69. if (op == null)
  70. throw new ArgumentNullException("op");
  71. _operationMap[key] = op;
  72. }
  73. /// <summary>
  74. ///
  75. /// </summary>
  76. /// <param name="operation">操作名</param>
  77. /// <param name="time">毫秒</param>
  78. /// <param name="reason">失败原因</param>
  79. /// <param name="args">参数</param>
  80. /// <returns></returns>
  81. public bool DoOperation(string operation, out string reason, int time, params object[] args)
  82. {
  83. if (!CanDoOperation(operation, out reason, args))
  84. {
  85. EV.PostWarningLog("OP", $"Can not execute {operation}, {reason}");
  86. return false;
  87. }
  88. if (!_timeOperationMap[operation].Invoke(out reason, time, args))
  89. {
  90. LOG.Error($"Do operation {operation} failed, {reason}");
  91. return false;
  92. }
  93. return true;
  94. }
  95. public bool ContainsOperation(string operation)
  96. {
  97. return _timeOperationMap.ContainsKey(operation) || _operationMap.ContainsKey(operation);
  98. }
  99. public bool AddCheck(string operation, IInterlockChecker check)
  100. {
  101. if (!_operationInterlockCheck.ContainsKey(operation))
  102. _operationInterlockCheck[operation] = new List<IInterlockChecker>();
  103. _operationInterlockCheck[operation].Add(check);
  104. return true;
  105. }
  106. public bool CanDoOperation(string operation, out string reason, params object[] args)
  107. {
  108. if (!ContainsOperation(operation))
  109. {
  110. reason = $"Call unregistered function, {operation}";
  111. return false;
  112. }
  113. if (_operationInterlockCheck.ContainsKey(operation))
  114. {
  115. foreach (var interlockChecker in _operationInterlockCheck[operation])
  116. {
  117. if (!interlockChecker.CanDo(out reason, args))
  118. {
  119. return false;
  120. }
  121. }
  122. }
  123. reason = string.Empty;
  124. return true;
  125. }
  126. public bool DoOperation(string operation, params object[] args)
  127. {
  128. if (OnDoOperation != null && OnDoOperation(operation, args))
  129. return true;
  130. if (!ContainsOperation(operation))
  131. {
  132. throw new ApplicationException("调用了未发布的接口" + operation);
  133. }
  134. if (!CanDoOperation(operation, out string reason, args))
  135. {
  136. EV.PostWarningLog("OP", $"Can not execute {operation}, {reason}");
  137. return false;
  138. }
  139. string parameter = "";
  140. if (args == null || args.Length == 0 )
  141. parameter = "()";
  142. else
  143. {
  144. parameter += "(";
  145. foreach (object o in args)
  146. {
  147. if (o == null)
  148. continue;
  149. var param1 = "";
  150. if (o.GetType() == typeof(Dictionary<string, object>))
  151. {
  152. param1 += "[";
  153. foreach (var item in (Dictionary<string, object>)(o))
  154. {
  155. var value = item.Value.ToString();
  156. if (value.Length > 10)
  157. value = value.Substring(0, 7) + "...";
  158. param1 += item.Value.ToString();
  159. param1 += ", ";
  160. }
  161. if (param1.Length > 2)
  162. param1 = param1.Remove(param1.Length - 2, 2);
  163. param1 += "]";
  164. parameter += param1;
  165. }
  166. else
  167. {
  168. parameter += o.ToString();
  169. }
  170. parameter += ", ";
  171. }
  172. if (parameter.Length > 2)
  173. parameter = parameter.Remove(parameter.Length - 2, 2);
  174. parameter += ")";
  175. }
  176. if (parameter.Length > 50)
  177. {
  178. parameter = parameter.Substring(0, 50) + "...";
  179. LOG.Write("long parameter: " + parameter);
  180. }
  181. EV.PostInfoLog("OP", $"Invoke operation {operation}{parameter}");
  182. if (_operationMap.ContainsKey(operation))
  183. {
  184. _operationMap[operation](operation, args);
  185. }
  186. else
  187. {
  188. if (!_timeOperationMap[operation].Invoke(out string reason1, 0, args))
  189. {
  190. EV.PostWarningLog("OP", reason1);
  191. return false;
  192. }
  193. }
  194. return true;
  195. }
  196. public bool DoOperation(string operation,out string reason, params object[] args)
  197. {
  198. reason = "";
  199. if (OnDoOperation != null && OnDoOperation(operation, args))
  200. return true;
  201. if (!CanDoOperation(operation, out reason, args))
  202. {
  203. return false;
  204. }
  205. string parameter = "";
  206. if (args == null || args.Length == 0)
  207. parameter = "()";
  208. else
  209. {
  210. parameter += "(";
  211. foreach (object o in args)
  212. {
  213. if (o == null)
  214. continue;
  215. var param1 = "";
  216. if (o.GetType() == typeof(Dictionary<string, object>))
  217. {
  218. param1 += "[";
  219. foreach (var item in (Dictionary<string, object>)(o))
  220. {
  221. var value = item.Value.ToString();
  222. if (value.Length > 10)
  223. value = value.Substring(0, 7) + "...";
  224. param1 += item.Value.ToString();
  225. param1 += ", ";
  226. }
  227. if (param1.Length > 2)
  228. param1 = param1.Remove(param1.Length - 2, 2);
  229. param1 += "]";
  230. parameter += param1;
  231. }
  232. else
  233. {
  234. parameter += o.ToString();
  235. }
  236. parameter += ", ";
  237. }
  238. if (parameter.Length > 2)
  239. parameter = parameter.Remove(parameter.Length - 2, 2);
  240. parameter += ")";
  241. }
  242. if (parameter.Length > 50)
  243. {
  244. parameter = parameter.Substring(0, 50) + "...";
  245. LOG.Write("long parameter: " + parameter);
  246. }
  247. EV.PostInfoLog("OP", $"Invoke operation {operation}{parameter}");
  248. if (_operationMap.ContainsKey(operation))
  249. {
  250. _operationMap[operation](operation, args);
  251. }
  252. else
  253. {
  254. if (!_timeOperationMap[operation].Invoke(out string reason1, 0, args))
  255. {
  256. EV.PostWarningLog("OP", reason1);
  257. return false;
  258. }
  259. }
  260. return true;
  261. }
  262. public void Traverse(object instance, string keyPrefix)
  263. {
  264. Parallel.ForEach(instance.GetType().GetMethods().Where<MethodInfo>(_hasSubscriptionAttribute), fi =>
  265. {
  266. string key = Parse(fi);
  267. key = string.IsNullOrWhiteSpace(keyPrefix) ? key : string.Format("{0}.{1}", keyPrefix, key);
  268. //Subscribe(key, fi.Invoke(instance, );
  269. });
  270. }
  271. string Parse(MethodInfo member)
  272. {
  273. return _hasSubscriptionAttribute(member) ? (member.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute).Key : null;
  274. }
  275. }
  276. }