using System; using System.IO; using System.Collections; using BCG.Utilities; using mbts.utils; namespace BCG { /// /// Controls (ie creates dynamically) an IPubsubDeserializer based on the type name which is /// part of the published data. Since its also a IPubsubDeserializer, all subsequent calls /// are passed though to the interface /// public class PubsubDeserializationController : IPubsubDeserializer { private IPubsubDeserializer? myDeserializer; public PubsubDeserializationController() { } public void update(Stream stream) { bts.Log.Gen.info("PubsubDeserializationController.update stream: {stream.Position} / {stream.Length}"); byte[] ops; Array rows; deserialize(new Stream[] { stream }, out ops, out rows); } public void update(byte[] bytes) { bts.Log.Gen.info("PubsubDeserializationController.update bytes: {bytes.Length}"); byte[] ops; Array rows; deserialize(new Stream[] { new MemoryStream(bytes, 0, bytes.Length, false, true) }, out ops, out rows); } #region IPubsubDeserializer Members public void deserialize(Stream[] streams, out byte[] theOps, out Array theRows) { if (myDeserializer != null) { myDeserializer.deserialize(streams, out theOps, out theRows); bts.Log.Gen.info($"PubsubDeserializationController.deserialize myDeserializer: {myDeserializer} ops: {theOps.Length} theRows: {theRows.Length}"); } else { Stream totalStream = UtilityMethods.getAggregatedStream(streams); long initialPos = totalStream.Position; BinaryReader br = new BinaryReader(totalStream); byte opCode = br.ReadByte(); bts.Log.Gen.info($"PubsubDeserializationController.deserialize opCode: {opCode}"); if (opCode == StreamOpCode.create) { string deserName = br.ReadString(); // TODOds if (deserName == "BCG.PubsubUniversalDeserializer, DataStreamGateway") // so we can play old replay files which would not include the assembly name deserName = "BCG.PubsubUniversalDeserializer, DataStreamCore"; ArgumentNullException.ThrowIfNull(myDeserializer); var deserType = Type.GetType(deserName); ArgumentNullException.ThrowIfNull(deserType); myDeserializer = Activator.CreateInstance(deserType) as IPubsubDeserializer; ArgumentNullException.ThrowIfNull(myDeserializer); totalStream.Position = initialPos; myDeserializer.deserialize(new Stream[] { totalStream }, out theOps, out theRows); bts.Log.Gen.info($"PubsubDeserializationController.deserialize myDeserializer: {myDeserializer} ops: {theOps.Length} theRows: {theRows.Length}"); } else { bts.Log.Gen.error($"Received a non create before a create. Bad op code was : {opCode} "); theOps = new byte[] { }; theRows = new object[] { }; } } } public byte[] getState() { return myDeserializer == null ? new byte[] { } : myDeserializer.getState(); } #endregion } }