StorageConvertionExtensions.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using Abc.Zebus.Routing;
  6. using ProtoBuf;
  7. namespace Abc.Zebus.Directory.Cassandra.Storage
  8. {
  9. public static class StorageConvertionExtensions
  10. {
  11. public static StoragePeer ToStoragePeer(this PeerDescriptor peerDescriptor)
  12. {
  13. var timestamp = peerDescriptor.TimestampUtc.HasValue ? new DateTime(peerDescriptor.TimestampUtc.Value.Ticks, DateTimeKind.Utc) : DateTime.UtcNow;
  14. return new StoragePeer
  15. {
  16. PeerId = peerDescriptor.PeerId.ToString(),
  17. EndPoint = peerDescriptor.Peer.EndPoint,
  18. HasDebuggerAttached = peerDescriptor.HasDebuggerAttached,
  19. IsPersistent = peerDescriptor.IsPersistent,
  20. IsUp = peerDescriptor.Peer.IsUp,
  21. IsResponding = peerDescriptor.Peer.IsResponding,
  22. TimestampUtc = timestamp,
  23. StaticSubscriptionsBytes = SerializeSubscriptions(peerDescriptor.Subscriptions)
  24. };
  25. }
  26. public static StorageSubscription ToStorageSubscription(this SubscriptionsForType subscriptionFortype, PeerId peerId)
  27. {
  28. return new StorageSubscription
  29. {
  30. PeerId = peerId.ToString(),
  31. MessageTypeId = subscriptionFortype.MessageTypeId.FullName,
  32. SubscriptionBindings = SerializeBindingKeys(subscriptionFortype.BindingKeys)
  33. };
  34. }
  35. public static SubscriptionsForType ToSubscriptionsForType(this StorageSubscription storageSubscription)
  36. {
  37. return new SubscriptionsForType(new MessageTypeId(storageSubscription.MessageTypeId), DeserializeBindingKeys(storageSubscription.SubscriptionBindings));
  38. }
  39. public static PeerDescriptor ToPeerDescriptor(this StoragePeer storagePeer, IEnumerable<Subscription> peerDynamicSubscriptions)
  40. {
  41. if (storagePeer == null)
  42. return null;
  43. var staticSubscriptions = DeserializeSubscriptions(storagePeer.StaticSubscriptionsBytes);
  44. var allSubscriptions = staticSubscriptions.Concat(peerDynamicSubscriptions).Distinct().ToArray();
  45. return new PeerDescriptor(new PeerId(storagePeer.PeerId), storagePeer.EndPoint, storagePeer.IsPersistent, storagePeer.IsUp,
  46. storagePeer.IsResponding, new DateTime(storagePeer.TimestampUtc.Ticks, DateTimeKind.Utc), allSubscriptions) { HasDebuggerAttached = storagePeer.HasDebuggerAttached };
  47. }
  48. public static PeerDescriptor ToPeerDescriptor(this StoragePeer storagePeer)
  49. {
  50. if (storagePeer == null)
  51. return null;
  52. var staticSubscriptions = DeserializeSubscriptions(storagePeer.StaticSubscriptionsBytes);
  53. return new PeerDescriptor(new PeerId(storagePeer.PeerId), storagePeer.EndPoint, storagePeer.IsPersistent, storagePeer.IsUp,
  54. storagePeer.IsResponding, new DateTime(storagePeer.TimestampUtc.Ticks, DateTimeKind.Utc), staticSubscriptions) { HasDebuggerAttached = storagePeer.HasDebuggerAttached };
  55. }
  56. private static byte[] SerializeSubscriptions(Subscription[] subscriptions)
  57. {
  58. using (var stream = new MemoryStream())
  59. {
  60. Serializer.Serialize(stream, subscriptions);
  61. return stream.ToArray();
  62. }
  63. }
  64. private static Subscription[] DeserializeSubscriptions(byte[] subscriptionsBytes)
  65. {
  66. return Serializer.Deserialize<Subscription[]>(new MemoryStream(subscriptionsBytes));
  67. }
  68. private static byte[] SerializeBindingKeys(BindingKey[] bindingKeys)
  69. {
  70. using (var memoryStream = new MemoryStream())
  71. using (var binaryWriter = new BinaryWriter(memoryStream))
  72. {
  73. binaryWriter.Write(bindingKeys.Length);
  74. for (var keyIndex = 0; keyIndex < bindingKeys.Length; keyIndex++)
  75. {
  76. var bindingKey = bindingKeys[keyIndex];
  77. binaryWriter.Write(bindingKey.PartCount);
  78. for (var partIndex = 0; partIndex < bindingKey.PartCount; partIndex++)
  79. binaryWriter.Write(bindingKey.GetPart(partIndex));
  80. }
  81. return memoryStream.ToArray();
  82. }
  83. }
  84. private static BindingKey[] DeserializeBindingKeys(byte[] bindingKeysBytes)
  85. {
  86. using (var memoryStream = new MemoryStream(bindingKeysBytes))
  87. using (var binaryReader = new BinaryReader(memoryStream))
  88. {
  89. var bindingKeyCount = binaryReader.ReadInt32();
  90. var bindingKeys = new BindingKey[bindingKeyCount];
  91. for (var keyIndex = 0; keyIndex < bindingKeyCount; keyIndex++)
  92. {
  93. var partsCount = binaryReader.ReadInt32();
  94. var parts = new string[partsCount];
  95. for (var partIndex = 0; partIndex < partsCount; partIndex++)
  96. parts[partIndex] = binaryReader.ReadString();
  97. bindingKeys[keyIndex] = new BindingKey(parts);
  98. }
  99. return bindingKeys;
  100. }
  101. }
  102. }
  103. }