using System; using System.Collections; using System.IO; using BCG.Utilities; //using BCG.Utilities.BlueConfiguration; namespace BCG { /// /// Pairs up with the UniversalSerializer to deserialize the corresponding data. Everythign anologous /// to UniversalSerializer. /// public abstract class ClientDeserializer : IClientDeserializer { protected IBlueTableSchema mySchema; protected IBlueTableSchema myFirstSchema; protected IClientRowDeserializer myRowDeserializer; public ClientDeserializer() { myFirstSchema = null; mySchema = null; } #region IClientDeserializer Members void BCG.IClientDeserializer.deserialize(Stream[] streams, out byte[] theOps, out Array theRows) { BinaryReader br = new BinaryReader(UtilityMethods.getAggregatedStream(streams)); // BlueTrace.WriteDeserializeStart(streams.Length, br.BaseStream.Length); // emit # of streams and total length ArrayList ops = new ArrayList(), rows = new ArrayList(); IBlueRowCollection[] result; while (true) { byte op; try { int value = br.BaseStream.ReadByte(); if (value == -1) break; op = (byte)value; // BlueTrace.WriteDeserializeOperation(op); } catch (EndOfStreamException) { break; } if (op == StreamOpCode.create) { br.ReadString(); //pubsubDeserializer br.ReadString(); //clientDeserializer mySchema = new BlueTableSchema(); mySchema.deserialze(br); //keep track of the first schema you've ever seen, so you know how to reorder columns if (myFirstSchema == null) myFirstSchema = mySchema; result = new IBlueRowCollection[] { myGetRows(br, op, br.ReadInt32()) }; myEncorporateRows(result[0], op); ops.Add(op); rows.Add(result); } else { if (mySchema != null) { if (op == StreamOpCode.add || op == StreamOpCode.change || op == StreamOpCode.remove) { result = new IBlueRowCollection[] { myGetRows(br, op, 1) }; myEncorporateRows(result[0], op); ops.Add(op); rows.Add(result); } else if (op == StreamOpCode.multipleChange) { result = new IBlueRowCollection[] { myGetRows(br, op, br.ReadInt32()) }; myEncorporateRows(result[0], op); ops.Add(op); rows.Add(result); } else if (op == StreamOpCode.multipleRemoveAdd) { result = new IBlueRowCollection[] { myGetRows (br, StreamOpCode.remove, br.ReadInt32 ()), myGetRows (br, StreamOpCode.add, br.ReadInt32 ()) }; myEncorporateRows(result[0], StreamOpCode.remove); myEncorporateRows(result[1], StreamOpCode.add); ops.Add(op); rows.Add(result); } else { // BlueEventLog.logEvent("Illegal op-code received", BlueEventLevel.Error); } } else { // BlueEventLog.logEvent("Received a non create operation even though the schema" + // "has never been created", BlueEventLevel.Warning); } } } theOps = ops.ToArray(typeof(byte)) as byte[]; theRows = rows.ToArray(); // BlueTrace.WriteDeserializeStop(rows.Count); } #endregion protected abstract IBlueRowCollection myGetRows(BinaryReader br, byte op, int nRows); protected abstract void myEncorporateRows(IBlueRowCollection rows, byte op); } }