OperationManager.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. namespace Aitex.Core.RT.OperationCenter
  16. {
  17. public class OperationManager : ICommonOperation, IOperation
  18. {
  19. private ConcurrentDictionary<string, Func<string, object[], bool>> _operationMap = new ConcurrentDictionary<string, Func<string, object[], bool>>();
  20. private ConcurrentDictionary<string, OperationFunction> _timeOperationMap = new ConcurrentDictionary<string, OperationFunction>();
  21. private Func<object, bool> _isSubscriptionAttribute;
  22. private Func<MemberInfo, bool> _hasSubscriptionAttribute;
  23. private Dictionary<string, List<IInterlockChecker>> _operationInterlockCheck = new Dictionary<string, List<IInterlockChecker>>();
  24. public event Func<string, object[], bool> OnDoOperation;
  25. public OperationManager()
  26. {
  27. }
  28. public void Initialize(bool enableService)
  29. {
  30. _isSubscriptionAttribute = attribute => attribute is SubscriptionAttribute;
  31. _hasSubscriptionAttribute = mi => mi.GetCustomAttributes(false).Any(_isSubscriptionAttribute);
  32. OP.InnerOperationManager = this;
  33. OP.OperationManager = this;
  34. if (enableService)
  35. {
  36. Singleton<WcfServiceManager>.Instance.Initialize(new Type[]
  37. {
  38. typeof(InvokeService)
  39. });
  40. }
  41. }
  42. public void Initialize()
  43. {
  44. Initialize(true);
  45. }
  46. public void Subscribe<T>(T instance, string keyPrefix = null) where T : class
  47. {
  48. if (instance == null)
  49. throw new ArgumentNullException("instance");
  50. Traverse(instance, keyPrefix);
  51. }
  52. public void Subscribe(string key, OperationFunction op)
  53. {
  54. if (string.IsNullOrWhiteSpace(key))
  55. throw new ArgumentNullException("key");
  56. if (_timeOperationMap.ContainsKey(key))
  57. throw new Exception(string.Format("Duplicated Key:{0}", key));
  58. if (op == null)
  59. throw new ArgumentNullException("op");
  60. _timeOperationMap[key] = op;
  61. }
  62. public void Subscribe(string key, Func<string, object[], bool> op)
  63. {
  64. if (string.IsNullOrWhiteSpace(key))
  65. throw new ArgumentNullException("key");
  66. if (_operationMap.ContainsKey(key))
  67. throw new Exception(string.Format("Duplicated Key:{0}", key));
  68. if (op == null)
  69. throw new ArgumentNullException("op");
  70. _operationMap[key] = op;
  71. }
  72. /// <summary>
  73. ///
  74. /// </summary>
  75. /// <param name="operation">操作名</param>
  76. /// <param name="time">毫秒</param>
  77. /// <param name="reason">失败原因</param>
  78. /// <param name="args">参数</param>
  79. /// <returns></returns>
  80. public bool DoOperation(string operation, out string reason, int time, params object[] args)
  81. {
  82. reason = string.Empty;
  83. try
  84. {
  85. if (!CanDoOperation(operation, out reason, args))
  86. {
  87. EV.PostWarningLog("OP", $"Can not execute {operation}, {reason}");
  88. return false;
  89. }
  90. if (!_timeOperationMap[operation].Invoke(out reason, time, args))
  91. {
  92. LOG.Error($"Do operation {operation} failed, {reason}");
  93. return false;
  94. }
  95. }
  96. catch (Exception ex)
  97. {
  98. LOG.Error($"Do operation {operation} failed, {ex.Message}");
  99. LOG.Write(ex);
  100. }
  101. return true;
  102. }
  103. public bool ContainsOperation(string operation)
  104. {
  105. return _timeOperationMap.ContainsKey(operation) || _operationMap.ContainsKey(operation);
  106. }
  107. public bool AddCheck(string operation, IInterlockChecker check)
  108. {
  109. if (!_operationInterlockCheck.ContainsKey(operation))
  110. _operationInterlockCheck[operation] = new List<IInterlockChecker>();
  111. _operationInterlockCheck[operation].Add(check);
  112. return true;
  113. }
  114. public bool CanDoOperation(string operation, out string reason, params object[] args)
  115. {
  116. if (!ContainsOperation(operation))
  117. {
  118. reason = $"Call unregistered function, {operation}";
  119. return false;
  120. }
  121. if (_operationInterlockCheck.ContainsKey(operation))
  122. {
  123. foreach (var interlockChecker in _operationInterlockCheck[operation])
  124. {
  125. if (!interlockChecker.CanDo(out reason, args))
  126. {
  127. return false;
  128. }
  129. }
  130. }
  131. reason = string.Empty;
  132. return true;
  133. }
  134. public bool DoOperation(string operation, params object[] args)
  135. {
  136. if (OnDoOperation != null && OnDoOperation(operation, args))
  137. return true;
  138. if (!ContainsOperation(operation))
  139. {
  140. throw new ApplicationException("调用了未发布的接口" + operation);
  141. }
  142. if (!CanDoOperation(operation, out string reason, args))
  143. {
  144. EV.PostWarningLog("OP", $"Can not execute {operation}, {reason}");
  145. return false;
  146. }
  147. string parameter = "";
  148. if (args == null || args.Length == 0 )
  149. parameter = "()";
  150. else
  151. {
  152. parameter += "(";
  153. foreach (object o in args)
  154. {
  155. if (o == null)
  156. continue;
  157. var param1 = "";
  158. if (o.GetType() == typeof(Dictionary<string, object>))
  159. {
  160. param1 += "[";
  161. foreach (var item in (Dictionary<string, object>)(o))
  162. {
  163. var value = item.Value.ToString();
  164. if (value.Length > 10)
  165. value = value.Substring(0, 7) + "...";
  166. param1 += item.Value.ToString();
  167. param1 += ", ";
  168. }
  169. if (param1.Length > 2)
  170. param1 = param1.Remove(param1.Length - 2, 2);
  171. param1 += "]";
  172. parameter += param1;
  173. }
  174. else
  175. {
  176. parameter += o.ToString();
  177. }
  178. parameter += ", ";
  179. }
  180. if (parameter.Length > 2)
  181. parameter = parameter.Remove(parameter.Length - 2, 2);
  182. parameter += ")";
  183. }
  184. if (parameter.Length > 50)
  185. {
  186. parameter = parameter.Substring(0, 50) + "...";
  187. LOG.Write("long parameter: " + parameter);
  188. }
  189. EV.PostInfoLog("OP", $"Invoke operation {operation}{parameter}");
  190. if (_operationMap.ContainsKey(operation))
  191. {
  192. _operationMap[operation](operation, args);
  193. }
  194. else
  195. {
  196. if (!_timeOperationMap[operation].Invoke(out string reason1, 0, args))
  197. {
  198. EV.PostWarningLog("OP", reason1);
  199. return false;
  200. }
  201. }
  202. return true;
  203. }
  204. public void Traverse(object instance, string keyPrefix)
  205. {
  206. Parallel.ForEach(instance.GetType().GetMethods().Where<MethodInfo>(_hasSubscriptionAttribute), fi =>
  207. {
  208. string key = Parse(fi);
  209. key = string.IsNullOrWhiteSpace(keyPrefix) ? key : string.Format("{0}.{1}", keyPrefix, key);
  210. //Subscribe(key, fi.Invoke(instance, );
  211. });
  212. }
  213. string Parse(MethodInfo member)
  214. {
  215. return _hasSubscriptionAttribute(member) ? (member.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute).Key : null;
  216. }
  217. }
  218. }