/* Copyright (c) 2019 Ahmed Kh. Zamil Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ import 'package:collection/collection.dart'; import '../Data/RepresentationType.dart'; import 'IEnum.dart'; import 'IntType.dart'; import '../Core/AsyncException.dart'; import '../Core/ErrorType.dart'; import '../Core/ExceptionCode.dart'; import '../Resource/Template/TemplateType.dart'; import 'DataDeserializer.dart'; import 'DataSerializer.dart'; import 'Guid.dart'; import 'IRecord.dart'; import 'Record.dart'; import 'ResourceArrayType.dart'; import 'dart:typed_data'; import '../Core/AsyncBag.dart'; import '../Core/AsyncReply.dart'; import 'DC.dart'; import 'BinaryList.dart'; import 'SizeObject.dart'; import 'NotModified.dart'; import 'PropertyValue.dart'; import 'KeyList.dart'; import '../Net/IIP/DistributedConnection.dart'; import '../Net/IIP/DistributedResource.dart'; import '../Resource/Warehouse.dart'; import '../Resource/IResource.dart'; import '../Resource/Template/PropertyTemplate.dart'; import '../Net/IIP/DistributedPropertyContext.dart'; import 'TransmissionType.dart'; // extension ListTyping on List { // Type get genericType => T; // } // extension MapTyping on Map { // Type get keyType => KT; // Type get valueType => VT; // } typedef Parser = AsyncReply Function(DC data, int offset, int length, DistributedConnection? connection, List? requestSequence); typedef Composer = DataSerializerComposeResults Function( dynamic value, DistributedConnection? connection); class CodecComposeResults { final int transmissionTypeIdentifier; final DC data; CodecComposeResults(this.transmissionTypeIdentifier, this.data); } class CodecParseResults { final AsyncReply reply; final int size; CodecParseResults(this.size, this.reply); } class Codec { //AsyncReply Parser(byte[] data, uint offset, uint length, DistributedConnection connection); static List> fixedParsers = [ [ DataDeserializer.nullParser, DataDeserializer.booleanFalseParser, DataDeserializer.booleanTrueParser, DataDeserializer.notModifiedParser, ], [ DataDeserializer.byteParser, DataDeserializer.sByteParser, DataDeserializer.char8Parser, ], [ DataDeserializer.int16Parser, DataDeserializer.uInt16Parser, DataDeserializer.char16Parser, ], [ DataDeserializer.int32Parser, DataDeserializer.uInt32Parser, DataDeserializer.float32Parser, DataDeserializer.resourceParser, DataDeserializer.localResourceParser, ], [ DataDeserializer.int64Parser, DataDeserializer.uInt64Parser, DataDeserializer.float64Parser, DataDeserializer.dateTimeParser, ], [ DataDeserializer.int128Parser, // int 128 DataDeserializer.uInt128Parser, // uint 128 DataDeserializer.float128Parser, ] ]; static List dynamicParsers = [ DataDeserializer.rawDataParser, DataDeserializer.stringParser, DataDeserializer.listParser, DataDeserializer.resourceListParser, DataDeserializer.recordListParser, ]; static List typedParsers = [ DataDeserializer.recordParser, DataDeserializer.typedListParser, DataDeserializer.typedMapParser, DataDeserializer.tupleParser, DataDeserializer.enumParser, DataDeserializer.constantParser, ]; /// /// Parse a value /// /// Bytes array /// Zero-indexed offset. /// Output the number of bytes parsed /// DistributedConnection is required in case a structure in the array holds items at the other end. /// DataType, in case the data is not prepended with DataType /// Value static CodecParseResults parse(DC data, int offset, DistributedConnection? connection, List? requestSequence, [TransmissionType? dataType = null]) { int len = 0; //print("Parse ${offset} ${dataType} ${requestSequence}"); if (dataType == null) { var parsedDataTyped = TransmissionType.parse(data, offset, data.length); len = parsedDataTyped.size; dataType = parsedDataTyped.type; offset = dataType?.offset ?? 0; //print("Parse TT ${len} ${parsedDataTyped.type} ${dataType?.offset}"); } else len = dataType.contentLength; if (dataType != null) { if (dataType.classType == TransmissionTypeClass.Fixed) { return CodecParseResults( len, fixedParsers[dataType.exponent][dataType.index]( data, dataType.offset, dataType.contentLength, connection, requestSequence)); } else if (dataType.classType == TransmissionTypeClass.Dynamic) { return CodecParseResults( len, dynamicParsers[dataType.index](data, dataType.offset, dataType.contentLength, connection, requestSequence)); } else //if (tt.Class == TransmissionTypeClass.Typed) { return CodecParseResults( len, typedParsers[dataType.index](data, dataType.offset, dataType.contentLength, connection, requestSequence)); } } throw Exception("Can't parse transmission type."); } static Map composers = { // Fixed bool: DataSerializer.boolComposer, NotModified: DataSerializer.notModifiedComposer, //byte = DataSerializer.ByteComposer, //[typeof(byte?)] = DataSerializer.ByteComposer, //[typeof(sbyte)] = DataSerializer.SByteComposer, //[typeof(sbyte?)] = DataSerializer.SByteComposer, //[typeof(char)] = DataSerializer.Char16Composer, //[typeof(char?)] = DataSerializer.Char16Composer, //[typeof(short)] = DataSerializer.Int16Composer, //[typeof(short?)] = DataSerializer.Int16Composer, //[typeof(ushort)] = DataSerializer.UInt16Composer, //[typeof(ushort?)] = DataSerializer.UInt16Composer, Int32: DataSerializer.int32Composer, UInt32: DataSerializer.uInt32Composer, Int8: DataSerializer.int8Composer, UInt8: DataSerializer.uInt8Composer, Int16: DataSerializer.int16Composer, UInt16: DataSerializer.uInt16Composer, Float32: DataSerializer.float128Composer, int: DataSerializer.int64Composer, //[typeof(long?)] = DataSerializer.Int64Composer, //[typeof(ulong)] = DataSerializer.UIn64Composer, //[typeof(ulong?)] = DataSerializer.UIn64Composer, double: DataSerializer.float64Composer, //[typeof(double?)] = DataSerializer.Float64Composer, DateTime: DataSerializer.dateTimeComposer, //[typeof(DateTime?)] = DataSerializer.DateTimeComposer, //[typeof(decimal)] = DataSerializer.Float128Composer, //[typeof(decimal?)] = DataSerializer.Float128Composer, DC: DataSerializer.rawDataComposer, //[typeof(byte?[])] = DataSerializer.RawDataComposerFromArray, //[typeof(List)] = DataSerializer.RawDataComposerFromList, //[typeof(List)] = DataSerializer.RawDataComposerFromList, String: DataSerializer.stringComposer, // Special List: DataSerializer.listComposer, // DataSerializer.ListComposerFromArray, //[typeof(List)] = DataSerializer.ListComposer,// DataSerializer.ListComposerFromList, List: DataSerializer .resourceListComposer, // (value, con) => (TransmissionTypeIdentifier.ResourceList, DC.ToBytes((decimal)value)), List: DataSerializer .resourceListComposer, // (value, con) => (TransmissionTypeIdentifier.ResourceList, DC.ToBytes((decimal)value)), //[typeof(List)] = DataSerializer.ResourceListComposer, //(value, con) => (TransmissionTypeIdentifier.ResourceList, DC.ToBytes((decimal)value)), //[typeof(List)] = DataSerializer.ResourceListComposer, //(value, con) => (TransmissionTypeIdentifier.ResourceList, DC.ToBytes((decimal)value)), //[typeof(IRecord[])] = DataSerializer.RecordListComposer,// (value, con) => (TransmissionTypeIdentifier.RecordList, DC.ToBytes((decimal)value)), //[typeof(IRecord?[])] = DataSerializer.RecordListComposer,// (value, con) => (TransmissionTypeIdentifier.RecordList, DC.ToBytes((decimal)value)), List: DataSerializer .recordListComposer, //(value, con) => (TransmissionTypeIdentifier.RecordList, DC.ToBytes((decimal)value)), List: DataSerializer .recordListComposer, //(value, con) => (TransmissionTypeIdentifier.RecordList, DC.ToBytes((decimal)value)), Map: DataSerializer.mapComposer, List: DataSerializer.propertyValueArrayComposer // Typed // [typeof(bool[])] = (value, con) => DataSerializer.TypedListComposer((IEnumerable)value, typeof(bool), con), // [typeof(bool?[])] = (value, con) => (TransmissionTypeIdentifier.TypedList, new byte[] { (byte)value }), // [typeof(List)] = (value, con) => (TransmissionTypeIdentifier.TypedList, new byte[] { (byte)value }), // [typeof(List)] = (value, con) => (TransmissionTypeIdentifier.TypedList, new byte[] { (byte)value }), // [typeof(byte?[])] = (value, con) => (TransmissionTypeIdentifier.TypedList, new byte[] { (byte)value }), // [typeof(List)] = (value, con) => (TransmissionTypeIdentifier.TypedList, new byte[] { (byte)value }), }; //static Type getListType(List list) => DumClass().type; //static Type getListType2(List list) => T; //static List getMapTypes(Map map) => [KT, VT]; static Type getListType(List list) { return Warehouse.typesFactory.values .firstWhereOrNull((x) => x.isListSubType(list)) ?.type ?? dynamic; } static List getMapTypes(Map map) { var kt = Warehouse.typesFactory.values .firstWhereOrNull((x) => x.isMapKeySubType(map)) ?.type; var vt = Warehouse.typesFactory.values .firstWhereOrNull((x) => x.isMapValueSubType(map)) ?.type; return [kt ?? dynamic, vt ?? dynamic]; } /// /// Compose a variable /// /// Value to compose. /// DistributedConnection is required to check locality. /// If True, prepend the DataType at the beginning of the output. /// Array of bytes in the network byte order. static DC compose(dynamic valueOrSource, DistributedConnection? connection) { if (valueOrSource == null) return TransmissionType.compose(TransmissionTypeIdentifier.Null, DC(0)); var type = valueOrSource.runtimeType; // if (type.) // { // var genericType = type.GetGenericTypeDefinition(); // if (genericType == typeof(DistributedPropertyContext<>)) // { // valueOrSource = ((IDistributedPropertyContext)valueOrSource).GetValue(connection); // } // else if (genericType == typeof(Func<>)) // { // var args = genericType.GetGenericArguments(); // if (args.Length == 2 && args[0] == typeof(DistributedConnection)) // { // //Func a; // //a.Invoke() // } // } // } // if (valueOrSource is IUserType) // valueOrSource = (valueOrSource as IUserType).Get(); //if (valueOrSource is Func) // valueOrSource = (valueOrSource as Func)(connection); // if (valueOrSource == null) // return TransmissionType.Compose(TransmissionTypeIdentifier.Null, null); // type = valueOrSource.GetType(); if (composers.containsKey(type)) { var results = composers[type]!(valueOrSource, connection); return TransmissionType.compose(results.identifier, results.data); } else { if (valueOrSource is List) { var genericType = getListType(valueOrSource); var results = DataSerializer.typedListComposer( valueOrSource, genericType, connection); return TransmissionType.compose(results.identifier, results.data); } else if (valueOrSource is Map) { var genericTypes = getMapTypes(valueOrSource); var results = DataSerializer.typedMapComposer( valueOrSource, genericTypes[0], genericTypes[1], connection); return TransmissionType.compose(results.identifier, results.data); } else if (valueOrSource is IResource) { var results = DataSerializer.resourceComposer(valueOrSource, connection); return TransmissionType.compose(results.identifier, results.data); } else if (valueOrSource is IRecord) { var results = DataSerializer.recordComposer(valueOrSource, connection); return TransmissionType.compose(results.identifier, results.data); } else if (valueOrSource is IEnum) { var results = DataSerializer.enumComposer(valueOrSource, connection); return TransmissionType.compose(results.identifier, results.data); } } return TransmissionType.compose(TransmissionTypeIdentifier.Null, DC(0)); } /// /// Check if a resource is local to a given connection. /// /// Resource to check. /// DistributedConnection to check if the resource is local to it. /// True, if the resource owner is the given connection, otherwise False. static bool isLocalResource( IResource resource, DistributedConnection? connection) { if (connection == null) return false; if (resource is DistributedResource) { if (resource.distributedResourceConnection == connection) return true; } return false; } /// /// Check if a type implements an interface /// /// Sub-class type. /// Super-interface type. /// True, if implements . static bool implementsInterface() => _DummyClass() is _DummyClass; } // related to implementsInterface class _DummyClass {}