OperationManager.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. if (!CanDoOperation(operation, out reason, args))
  83. {
  84. EV.PostWarningLog("OP", $"Can not execute {operation}, {reason}");
  85. return false;
  86. }
  87. if(_timeOperationMap.ContainsKey(operation))
  88. {
  89. if (!_timeOperationMap[operation].Invoke(out reason, time, args))
  90. {
  91. LOG.Error($"Do operation {operation} failed, {reason}");
  92. return false;
  93. }
  94. return true;
  95. }
  96. else if(_operationMap.ContainsKey(operation))
  97. {
  98. if (!_operationMap[operation].Invoke(operation, args))
  99. {
  100. LOG.Error($"Do operation {operation} failed, {reason}");
  101. return false;
  102. }
  103. return true;
  104. }
  105. return false;
  106. }
  107. public bool ContainsOperation(string operation)
  108. {
  109. return _timeOperationMap.ContainsKey(operation) || _operationMap.ContainsKey(operation);
  110. }
  111. public bool AddCheck(string operation, IInterlockChecker check)
  112. {
  113. if (!_operationInterlockCheck.ContainsKey(operation))
  114. _operationInterlockCheck[operation] = new List<IInterlockChecker>();
  115. _operationInterlockCheck[operation].Add(check);
  116. return true;
  117. }
  118. public bool CanDoOperation(string operation, out string reason, params object[] args)
  119. {
  120. if (!ContainsOperation(operation))
  121. {
  122. reason = $"Call unregistered function, {operation}";
  123. return false;
  124. }
  125. if (_operationInterlockCheck.ContainsKey(operation))
  126. {
  127. foreach (var interlockChecker in _operationInterlockCheck[operation])
  128. {
  129. if (!interlockChecker.CanDo(out reason, args))
  130. {
  131. return false;
  132. }
  133. }
  134. }
  135. reason = string.Empty;
  136. return true;
  137. }
  138. public bool DoOperation(string operation, params object[] args)
  139. {
  140. if (OnDoOperation != null && OnDoOperation(operation, args))
  141. return true;
  142. if (!ContainsOperation(operation))
  143. {
  144. throw new ApplicationException("调用了未发布的接口" + operation);
  145. }
  146. if (!CanDoOperation(operation, out string reason, args))
  147. {
  148. EV.PostWarningLog("OP", $"Can not execute {operation}, {reason}");
  149. return false;
  150. }
  151. string parameter = "";
  152. string roleName = "";
  153. List<object> _argList = new List<object>();//not contain role name
  154. if (args == null || args.Length == 0 )
  155. parameter = "()";
  156. else
  157. {
  158. parameter += "(";
  159. foreach (object o in args)
  160. {
  161. if (o != null && !string.IsNullOrEmpty(o.ToString()) && o.ToString().ToLower().Contains("rolename="))
  162. {
  163. roleName = o.ToString().Substring(9);//"rolename=" length = 9
  164. continue;
  165. }
  166. _argList.Add(o);
  167. if (o == null)
  168. continue;
  169. var param1 = "";
  170. if (o.GetType() == typeof(Dictionary<string, object>))
  171. {
  172. param1 += "[";
  173. foreach (var item in (Dictionary<string, object>)(o))
  174. {
  175. var value = item.Value.ToString();
  176. if (value.Length > 10)
  177. value = value.Substring(0, 7) + "...";
  178. param1 += item.Value.ToString();
  179. param1 += ", ";
  180. }
  181. if (param1.Length > 2)
  182. param1 = param1.Remove(param1.Length - 2, 2);
  183. param1 += "]";
  184. parameter += param1;
  185. }
  186. else
  187. {
  188. parameter += o.ToString();
  189. }
  190. parameter += ", ";
  191. }
  192. if (parameter.Length > 2)
  193. parameter = parameter.Remove(parameter.Length - 2, 2);
  194. parameter += ")";
  195. }
  196. //if (parameter.Length > 50)
  197. //{
  198. // parameter = parameter.Substring(0, 50) + "...";
  199. // LOG.Write("long parameter: " + parameter);
  200. //}
  201. if(!operation.Contains("Virtual"))
  202. EV.PostInfoLog("OP", $"Invoke operation {operation}{parameter}", roleName);
  203. if (_operationMap.ContainsKey(operation))
  204. {
  205. _operationMap[operation](operation, _argList.ToArray());
  206. }
  207. else
  208. {
  209. if (!_timeOperationMap[operation].Invoke(out string reason1, 0, _argList.ToArray()))
  210. {
  211. EV.PostWarningLog("OP", reason1);
  212. return false;
  213. }
  214. }
  215. return true;
  216. }
  217. public void Traverse(object instance, string keyPrefix)
  218. {
  219. Parallel.ForEach(instance.GetType().GetMethods().Where<MethodInfo>(_hasSubscriptionAttribute), fi =>
  220. {
  221. string key = Parse(fi);
  222. key = string.IsNullOrWhiteSpace(keyPrefix) ? key : string.Format("{0}.{1}", keyPrefix, key);
  223. //Subscribe(key, fi.Invoke(instance, );
  224. });
  225. }
  226. string Parse(MethodInfo member)
  227. {
  228. return _hasSubscriptionAttribute(member) ? (member.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute).Key : null;
  229. }
  230. }
  231. }