예제 #1
0
        public virtual object ReadType(string typeName, Configuration configuration, BinaryContext context)
        {
            var typeDescription = configuration.Descriptions.Find(p => p.Name.Equals(typeName));
            if (typeDescription == null)
                throw new ArgumentException("The complex type " + typeName + " was not found in configuration");
            try
            {
                var type=Type.GetType(typeDescription.ClassName);
                if (type==null)
                {
                    throw new InvalidOperationException("Specified type was not found in classpath");
                }
                var result=Activator.CreateInstance(type);

                foreach (var fieldDescription in typeDescription.Fields)
                {
                    var typeReader = TypeReaderFactory.GetTypeReader(fieldDescription.Type,configuration);
                    var fieldValue = typeReader.ReadType(fieldDescription.Type, configuration, context);
                    type.GetProperty(fieldDescription.Name).GetSetMethod().Invoke(result,new[] {fieldValue});
                }
                return result;

            }
            catch (Exception exception)
            {
                throw new InvalidOperationException("Cannot read type " + typeDescription.ClassName + " from given context", exception);
            }
        }
예제 #2
0
 public void WriteType(object obj, Configuration configuration, BinaryWritingContext context)
 {
     try
     {
         var givenType = obj.GetType();
         var typeDescription = configuration.Descriptions.Find(p => p.ClassName == givenType.AssemblyQualifiedName);
         if (typeDescription == null)
         {
             throw new ArgumentException("Cannot find description for given object of class " +
                                         givenType.AssemblyQualifiedName);
         }
         foreach (var fieldDescription in typeDescription.Fields)
         {
             var typeWriter = TypeWriterFactory.GetTypeWriter(fieldDescription.Type, configuration);
             var fieldObject = givenType.GetProperty(fieldDescription.Name).GetGetMethod().Invoke(obj,
                                                                                                  new object[] {});
             typeWriter.WriteType(fieldObject, configuration, context);
         }
     }
     catch (Exception exception)
     {
         throw new InvalidOperationException(
             "Cannot write type to given context", exception);
     }
 }
예제 #3
0
        public static ITypeReader GetTypeReader(string typeName, Configuration configuration)
        {
            ITypeReader typeReader = GetReaderFor(typeName);

            if(typeReader == null)
                throw new ArgumentException("Cannot determine reader for type name: "+typeName);

            return typeReader;
        }
예제 #4
0
        public static ITypeWriter GetTypeWriter(object obj, Configuration configuration)
        {
            ITypeWriter typeWriter = GetWriterFor(obj);

            if(typeWriter == null)
                throw new ArgumentException("Cannot determine reader for type name: "+obj.ToString());

            return typeWriter;
        }
        public void InitTest()
        {
            var configurationSerializerBase = new ConfigurationSerializerBase();
            _context = new BinaryContext();
            _configuration = configurationSerializerBase.Deserialize("TCPDescription.xml");
            _context.InitializeFromFile("HttpPackage.dat");

            CreateResultPackage();
        }
예제 #6
0
 private bool CheckRecordSize(Configuration configuration)
 {
     var metadata = configuration.Metadata;
     var found = metadata.Find(p => p.MetadataType == MetadataTypes.RecordSize);
     if (found == null)
     {
         return false;
     }
     return true;
 }
 protected static void CheckConfigurationAndContext(Configuration configuration, BinaryContext context)
 {
     if(configuration==null)
     {
         throw new ArgumentException("Configuration is null");
     }
     if (context==null || !context.IsInitialized())
     {
         throw new ArgumentException("Context is null or not initialized");
     }
 }
 protected override string DetermineNextTypeName(Configuration configuration, BinaryContext context)
 {
     CreateReaderFactory();
     string typeName = _typeNames[_currentTypeName];
     _currentTypeName++;
     if (_currentTypeName>=_typeNames.Length)
     {
         _repeatCounter++;
         _currentTypeName = 0;
     }
     return typeName;
 }
 public List<Object> ReadObjects(Configuration configuration, BinaryContext context)
 {
     CheckConfigurationAndContext(configuration,context);
     var objects = new List<object>();
     while (!EndReading(configuration, context, objects))
     {
         var typeName = DetermineNextTypeName(configuration, context);
         var typeReader = ReaderFactory.GetTypeReader(typeName, configuration);
         var result = typeReader.ReadType(typeName, configuration, context);
         objects.Add(result);
     }
     return objects;
 }
        protected override string DetermineNextTypeName(Configuration configuration, BinaryContext context)
        {
            CreateTypeReaderFactory(configuration,context);
            long markerSize = GetMarkerSize(configuration, context);
            if (!context.CanReadBytes(markerSize))
            {
                throw new InvalidOperationException("Unable to read next type marker from context");
            }
            var markerBytes = context.PeekBytes(markerSize);
            var nextType = FindTypeForMarker(configuration, markerBytes);

            _typesCounter++;
            return nextType;
        }
예제 #11
0
 private bool CheckTypeMarkers(Configuration configuration)
 {
     var descriptions = configuration.Descriptions;
     var markerFlag = true;
     foreach (var typeDescription in descriptions)
     {
         var typeMarker = typeDescription.Metadata.Find(p => p.MetadataType == MetadataTypes.TypeMarker && p is ValueMetadata);
         if (typeMarker==null)
         {
             markerFlag = false;
         }
     }
     return markerFlag;
 }
예제 #12
0
        public void WriteType(Object obj, Configuration configuration, BinaryWritingContext context)
        {
            Type type = obj.GetType();
            MethodInfo lengthProperty = type.GetProperty("Length").GetGetMethod();
            var arrayLength = (int)lengthProperty.Invoke(obj, new object[] { });
            MethodInfo getValueMethod = type.GetMethod("GetValue", new[] { Type.GetType("System.Int32") });

            for (int i = 0; i < arrayLength; i++)
            {
                var propValue = getValueMethod.Invoke(obj, new object[] { i });
                var typeWriter = TypeWriterFactory.GetTypeWriter(propValue,configuration);
                typeWriter.WriteType(propValue,configuration,context);
            }
        }
예제 #13
0
        public object ReadType(string typeName, Configuration configuration, BinaryContext context)
        {
            string elementTypeName;
            uint arrayLength = GetArrayLength(typeName, out elementTypeName);
            var typeReader = TypeReaderFactory.GetTypeReader(elementTypeName, configuration);
            var result = new object[arrayLength];
            for (var i = 0; i < result.Length; i++)
            {
                result[i] = typeReader.ReadType(elementTypeName, configuration, context);
            }
            var typeCastedResult = CreateTypeCastedArray(result);

            return typeCastedResult;
        }
 public List<Object> ReadObjects(Configuration configuration, BinaryContext context)
 {
     CheckConfigurationAndContext(configuration,context);
     var objects = new List<object>();
     var splitter = GetSplitter();
     var splittedResult = splitter.Split(configuration, context);
     var subContext = new BinaryContext();
     foreach (var splittedData in splittedResult)
     {
         subContext.InitializeFromArray(splittedData);
         var result = ReadObjectsFromSubContext(configuration, subContext, objects);
         objects.Add(result);
     }
     return objects;
 }
예제 #15
0
 /// <summary>
 /// Creates the strategy for given configuration.
 /// </summary>
 /// <param name="configuration"> the configuration to work with.</param>
 /// <returns>the created strategy.</returns>
 public IReadingStrategy CreateStrategy(Configuration configuration)
 {
     if(CheckTypeMarkerSize(configuration) && CheckTypeMarkers(configuration))
     {
         return _typeMarkerFlowReadingStrategy;
     }
     if(CheckRecordSize(configuration))
     {
         return _sizeSplittedReadingStrategy;
     }
     if (CheckRecordSeparatingMarker(configuration))
     {
         return _markerSplittedReadingStrategy;
     }
     throw new InvalidOperationException("Cannot determine strategy for the configuration");
 }
 private byte[] RetrieveMarker(Configuration configuration, BinaryContext context)
 {
     byte[] result = null;
     var marker = configuration.Metadata.Find(p => p.MetadataType == MetadataTypes.RecordSeparatingMarker);
     if (marker is OffsetMetadata)
     {
         var offsetMarker = (OffsetMetadata) marker;
         result = context.PeekBytes(offsetMarker.Offset, offsetMarker.Length);
     }
     if (marker is ValueMetadata)
     {
         var valueMarker = (ValueMetadata) marker;
         result = ConvertToBytes(valueMarker.Value);
     }
     if (result != null) return result;
     throw new InvalidOperationException("Cannot determine the record marker");
 }
 protected long GetMarkerSize(Configuration configuration, BinaryContext context)
 {
     long markerSize;
     MetadataBase markerSizeMetadata =
         configuration.Metadata.Find(p => p.MetadataType == MetadataTypes.TypeMarkerSize);
     if (markerSizeMetadata is ValueMetadata)
     {
         markerSize = Int64.Parse(((ValueMetadata) markerSizeMetadata).Value);
     }
     else
     {
         var offsetMetadata = (OffsetMetadata) markerSizeMetadata;
         byte[] sizeBytes = context.PeekBytes(offsetMetadata.Offset, offsetMetadata.Length);
         markerSize = ConvertSize(sizeBytes);
     }
     return markerSize;
 }
        /// <summary>
        /// Splits the given context to several data arrays using the global RecordSize metadata.
        /// </summary>
        /// <param name="configuration">the configuration to use</param>
        /// <param name="context">the binary context to work with</param>
        /// <returns>the result of splitting.</returns>
        public List<byte[]> Split(Configuration configuration, BinaryContext context)
        {
            var globalSizeMetadata = configuration.Metadata.Single(p => p.MetadataType == MetadataTypes.RecordSize);
            if(globalSizeMetadata is OffsetMetadata)
            {
                var offsetMetadata = globalSizeMetadata as OffsetMetadata;
                var result=_offsetSizeDataSplitter.Split(offsetMetadata.Offset, offsetMetadata.Length, context);
                return result;

            }
            if (globalSizeMetadata is ValueMetadata)
            {
                var valueMetadata = globalSizeMetadata as ValueMetadata;
                var result = _valueSizeDataSplitter.Split(Int64.Parse(valueMetadata.Value), context);
                return result;
            }
            throw new InvalidOperationException("Cannot determine size metadata type:");
        }
예제 #19
0
 public object ReadType(string typeName, Configuration configuration, BinaryContext context)
 {
     object result;
         switch (typeName)
         {
             case "byte":
                 result = ReadByte(context);
                 break;
             case "char":
                 result =  ReadChar(context);
                 break;
             case "double":
                 result =  ReadDouble(context);
                 break;
             case "float":
             case "single":
                 result =  ReadFloat(context);
                 break;
             case "long":
                 result =  ReadLong(context);
                 break;
             case "short":
                 result =  ReadShort(context);
                 break;
             case "string":
                 result =  ReadString(context);
                 break;
             case "int":
                 result =  ReadInt(context);
                 break;
             case "ulong":
                 result = ReadULong(context);
                 break;
             case "ushort":
                 result = ReadUShort(context);
                 break;
             case "uint":
                 result = ReadUInt(context);
                 break;
             default:
                 throw new ArgumentException("Unknown simple type: "+typeName, typeName);
         }
         return result;
 }
        /// <summary>
        /// Splits the given context to several data arrays using the global RecordMarker metadata.
        /// </summary>
        /// <param name="configuration">the configuration to use</param>
        /// <param name="context">the binary context to work with</param>
        /// <returns>the result of splitting.</returns>
        public List<byte[]> Split(Configuration configuration, BinaryContext context)
        {
            var result = new List<byte[]>();
            byte[] marker = RetrieveMarker(configuration, context);

            var positions=context.FindMarkers(marker);
            for (int i = 0; i < positions.Count; i++)
            {
                var index = positions[i];
                var prevIndex = (i-1<0)?0:positions[i - 1];
                var recordSize = index - prevIndex;
                if(context.CanReadBytes(recordSize+marker.Length))
                {

                }

            }
            return result;
        }
예제 #21
0
 public void WriteType(Object obj, Configuration configuration, BinaryWritingContext context)
 {
     switch (obj.GetType().FullName)
         {
             case "System.Byte":
                 WriteByte(obj,context);
                 break;
             case "System.Char":
                 WriteChar(obj, context);
                 break;
             case "System.Double":
                 WriteDouble(obj, context);
                 break;
             case "System.Single":
                 WriteFloat(obj, context);
                 break;
             case "System.Int64":
                 WriteLong(obj, context);
                 break;
             case "System.Int16":
                 WriteShort(obj, context);
                 break;
             case "System.String":
                 WriteString(obj, context);
                 break;
             case "System.Int32":
                 WriteInt(obj, context);
                 break;
             case "System.UInt64":
                 WriteULong(obj, context);
                 break;
             case "System.UInt16":
                 WriteUShort(obj, context);
                 break;
             case "System.UInt32":
                 WriteUInt(obj,context);
                 break;
             default:
                 throw new ArgumentException("Unsupported simple type: "+obj.GetType().FullName);
         }
 }
 protected abstract Boolean EndReading(Configuration configuration, BinaryContext context, List<Object> objects);
 protected abstract string DetermineNextTypeName(Configuration configuration, BinaryContext context);
 public override object ReadType(string typeName, Configuration configuration, BinaryContext context)
 {
     context.MoveForward(_markerSize);
     return base.ReadType(typeName, configuration, context);
 }
 private string FindTypeForMarker(Configuration configuration, byte[] markerBytes)
 {
     string marker = "";
     string typeName = "";
     for (int i = 0; i < markerBytes.Length; i++)
     {
         marker += markerBytes[i].ToString("X2");
     }
     List<TypeDescription> typeDescriptions = configuration.Descriptions;
     foreach (TypeDescription typeDescription in typeDescriptions)
     {
         var typeMarker = (ValueMetadata) typeDescription.Metadata.Find(
             p => p.MetadataType == MetadataTypes.TypeMarker && p is ValueMetadata);
         if (!typeMarker.Value.Equals(marker)) continue;
         typeName = typeDescription.Name;
         break;
     }
     if (typeName.Length == 0) throw new InvalidOperationException("Cannot determine type for marker " + marker);
     return typeName;
 }
 protected abstract object[] ReadObjectsFromSubContext(Configuration configuration, BinaryContext context, List<Object> objects);
 protected override bool EndReading(Configuration configuration, BinaryContext context, List<object> objects)
 {
     return (_currentTypeName >= _typeNames.Length || _repeatCounter>=_repeatSequence) && !context.EndOfContextReached();
 }
        protected override object[] ReadObjectsFromSubContext(Configuration configuration, BinaryContext context, List<object> objects)
        {
            //TODO: how to determine which types must be read from subcontext?

            throw new NotImplementedException();
        }
 public FlowTypeReaderFactory(Configuration configuration, BinaryContext context)
 {
     ComplexReader = new FlowComplexTypeReader(GetMarkerSize(configuration, context));
 }
예제 #30
0
 public bool Equals(Configuration configuration)
 {
     var result = Descriptions.SequenceEqual(configuration.Descriptions);
     return result;
 }