FileWriterSet.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections.Generic;
  3. namespace NTMiner.Core.Impl {
  4. public class FileWriterSet : IFileWriterSet {
  5. private readonly Dictionary<Guid, FileWriterData> _dicById = new Dictionary<Guid, FileWriterData>();
  6. public FileWriterSet(IServerContext context) {
  7. context.AddCmdPath<AddFileWriterCommand>("添加文件书写器", LogEnum.DevConsole,
  8. action: (message) => {
  9. InitOnece();
  10. if (message == null || message.Input == null || message.Input.GetId() == Guid.Empty) {
  11. throw new ArgumentNullException();
  12. }
  13. if (_dicById.ContainsKey(message.Input.GetId())) {
  14. return;
  15. }
  16. if (string.IsNullOrEmpty(message.Input.FileUrl) || string.IsNullOrEmpty(message.Input.Body)) {
  17. throw new ValidationException("FileWriter name and body can't be null or empty");
  18. }
  19. FileWriterData entity = new FileWriterData().Update(message.Input);
  20. _dicById.Add(entity.Id, entity);
  21. var repository = NTMinerRoot.CreateServerRepository<FileWriterData>();
  22. repository.Add(entity);
  23. VirtualRoot.RaiseEvent(new FileWriterAddedEvent(message.MessageId, entity));
  24. }, location: this.GetType());
  25. context.AddCmdPath<UpdateFileWriterCommand>("更新文件书写器", LogEnum.DevConsole,
  26. action: (message) => {
  27. InitOnece();
  28. if (message == null || message.Input == null || message.Input.GetId() == Guid.Empty) {
  29. throw new ArgumentNullException();
  30. }
  31. if (string.IsNullOrEmpty(message.Input.FileUrl) || string.IsNullOrEmpty(message.Input.Body)) {
  32. throw new ValidationException("FileWriter name and body can't be null or empty");
  33. }
  34. if (!_dicById.ContainsKey(message.Input.GetId())) {
  35. return;
  36. }
  37. FileWriterData entity = _dicById[message.Input.GetId()];
  38. if (ReferenceEquals(entity, message.Input)) {
  39. return;
  40. }
  41. entity.Update(message.Input);
  42. var repository = NTMinerRoot.CreateServerRepository<FileWriterData>();
  43. repository.Update(entity);
  44. VirtualRoot.RaiseEvent(new FileWriterUpdatedEvent(message.MessageId, entity));
  45. }, location: this.GetType());
  46. context.AddCmdPath<RemoveFileWriterCommand>("移除文件书写器", LogEnum.DevConsole,
  47. action: (message) => {
  48. InitOnece();
  49. if (message == null || message.EntityId == Guid.Empty) {
  50. throw new ArgumentNullException();
  51. }
  52. if (!_dicById.ContainsKey(message.EntityId)) {
  53. return;
  54. }
  55. FileWriterData entity = _dicById[message.EntityId];
  56. _dicById.Remove(entity.GetId());
  57. var repository = NTMinerRoot.CreateServerRepository<FileWriterData>();
  58. repository.Remove(message.EntityId);
  59. VirtualRoot.RaiseEvent(new FileWriterRemovedEvent(message.MessageId, entity));
  60. }, location: this.GetType());
  61. }
  62. private bool _isInited = false;
  63. private readonly object _locker = new object();
  64. private void InitOnece() {
  65. if (_isInited) {
  66. return;
  67. }
  68. Init();
  69. }
  70. private void Init() {
  71. lock (_locker) {
  72. if (!_isInited) {
  73. var repository = NTMinerRoot.CreateServerRepository<FileWriterData>();
  74. foreach (var item in repository.GetAll()) {
  75. if (!_dicById.ContainsKey(item.GetId())) {
  76. _dicById.Add(item.GetId(), item);
  77. }
  78. }
  79. _isInited = true;
  80. }
  81. }
  82. }
  83. public bool TryGetFileWriter(Guid writerId, out IFileWriter writer) {
  84. InitOnece();
  85. FileWriterData g;
  86. bool r = _dicById.TryGetValue(writerId, out g);
  87. writer = g;
  88. return r;
  89. }
  90. public IEnumerable<IFileWriter> AsEnumerable() {
  91. InitOnece();
  92. return _dicById.Values;
  93. }
  94. }
  95. }