VirtualRoot.partials.Hub.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using NTMiner.Hub;
  2. using NTMiner.Timing;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text.RegularExpressions;
  6. namespace NTMiner {
  7. public static partial class VirtualRoot {
  8. // 视图层有个界面提供给开发者观察系统的消息路径情况所以是public的。
  9. // 系统根上的一些状态集的构造时最好都放在MessageHub初始化之后,因为状态集的构造
  10. // 函数中可能会建造消息路径,所以这里保证在访问MessageHub之前一定完成了构造。
  11. public static readonly IMessagePathHub MessageHub = new MessagePathHub();
  12. private static ITimingEventProducer _timingEventProducer = null;
  13. public static void StartTimer(ITimingEventProducer timingEventProducer = null) {
  14. if (_timingEventProducer != null) {
  15. throw new InvalidProgramException("秒表已经启动,不能重复启动");
  16. }
  17. if (timingEventProducer == null) {
  18. timingEventProducer = new DefaultTimingEventProducer(MessageHub);
  19. }
  20. _timingEventProducer = timingEventProducer;
  21. timingEventProducer.Start();
  22. }
  23. public static void RaiseEvent<TEvent>(TEvent evnt) where TEvent : class, IEvent {
  24. MessageHub.Route(evnt);
  25. }
  26. public static void Execute<TCmd>(TCmd command) where TCmd : class, ICmd {
  27. MessageHub.Route(command);
  28. }
  29. /// <summary>
  30. /// 修建消息的运动路径
  31. /// </summary>
  32. public static IMessagePathId BuildMessagePath<TMessage>(string description, LogEnum logType, Type location, PathPriority priority, Action<TMessage> path) {
  33. return MessageHub.AddPath(location.FullName, description, logType, pathId: PathId.Empty, priority, path);
  34. }
  35. /// <summary>
  36. /// 消息通过路径一次后路径即消失。
  37. /// 注意该路径具有特定的路径标识pathId,pathId可以看作是路径的形状,只有和该路径的形状相同的消息才能通过路径。
  38. /// </summary>
  39. public static IMessagePathId BuildOnecePath<TMessage>(string description, LogEnum logType, PathId pathId, Type location, PathPriority priority, Action<TMessage> path) {
  40. return MessageHub.AddPath(location.FullName, description, logType, pathId, priority, path, viaTimesLimit: 1);
  41. }
  42. /// <summary>
  43. /// 消息通过路径指定的次数后路径即消失
  44. /// </summary>
  45. public static IMessagePathId BuildViaTimesLimitPath<TMessage>(string description, LogEnum logType, int viaTimesLimit, Type location, PathPriority priority, Action<TMessage> path) {
  46. return MessageHub.AddPath(location.FullName, description, logType, pathId: PathId.Empty, priority, path, viaTimesLimit);
  47. }
  48. /// <summary>
  49. /// 消息通过路径指定的次数后路径即消失
  50. /// </summary>
  51. public static IMessagePathId BuildViaTimesLimitPath<TMessage>(string description, LogEnum logType, int viaTimesLimit, string location, PathPriority priority, Action<TMessage> path) {
  52. return MessageHub.AddPath(location, description, logType, pathId: PathId.Empty, priority, path, viaTimesLimit);
  53. }
  54. public static IMessagePathId BuildCmdPath<TCmd>(Type location, LogEnum logType, Action<TCmd> path)
  55. where TCmd : ICmd {
  56. MessageTypeAttribute messageTypeDescription = MessageTypeAttribute.GetMessageTypeAttribute(typeof(TCmd));
  57. return BuildMessagePath($"处理{messageTypeDescription.Description}命令", logType, location, PathPriority.Normal, path);
  58. }
  59. public static IMessagePathId BuildEventPath<TEvent>(string description, LogEnum logType, Type location, PathPriority priority, Action<TEvent> path)
  60. where TEvent : IEvent {
  61. return BuildMessagePath(description, logType, location, priority, path);
  62. }
  63. public static void RemoveMessagePath(IMessagePathId pathId) {
  64. if (pathId == null) {
  65. return;
  66. }
  67. MessageHub.RemovePath(pathId);
  68. }
  69. private static readonly Dictionary<string, Regex> _regexDic = new Dictionary<string, Regex>();
  70. // 【性能】缓存构建的正则对象
  71. public static Regex GetRegex(string pattern) {
  72. if (string.IsNullOrEmpty(pattern)) {
  73. return null;
  74. }
  75. if (_regexDic.TryGetValue(pattern, out Regex regex)) {
  76. return regex;
  77. }
  78. lock (_locker) {
  79. if (!_regexDic.TryGetValue(pattern, out regex)) {
  80. regex = new Regex(pattern, RegexOptions.Compiled);
  81. _regexDic.Add(pattern, regex);
  82. }
  83. return regex;
  84. }
  85. }
  86. }
  87. }