/// <inheritdoc/>
        public async Task HandleAsync(string deviceId, string moduleId,
                                      byte[] payload, IDictionary <string, string> properties, Func <Task> checkpoint)
        {
            try {
                var context  = new ServiceMessageContext();
                var decoder  = new BinaryDecoder(new MemoryStream(payload), context);
                var messages = decoder.ReadBoolean(null) // is Batch?
                    ? decoder.ReadEncodeableArray(null, typeof(NetworkMessage))
                               as NetworkMessage[]
                     : (decoder.ReadEncodeable(null, typeof(NetworkMessage))
                        as NetworkMessage).YieldReturn();

                foreach (var message in messages)
                {
                    foreach (var dataSetMessage in message.Messages)
                    {
                        var dataset = new DataSetMessageModel {
                            PublisherId     = message.PublisherId,
                            MessageId       = message.MessageId,
                            DataSetClassId  = message.DataSetClassId,
                            DataSetWriterId = dataSetMessage.DataSetWriterId,
                            SequenceNumber  = dataSetMessage.SequenceNumber,
                            Status          = StatusCode.LookupSymbolicId(dataSetMessage.Status.Code),
                            MetaDataVersion = $"{dataSetMessage.MetaDataVersion.MajorVersion}" +
                                              $".{dataSetMessage.MetaDataVersion.MinorVersion}",
                            Timestamp = dataSetMessage.Timestamp,
                            Payload   = new Dictionary <string, DataValueModel>()
                        };
                        foreach (var datapoint in dataSetMessage.Payload)
                        {
                            var codec = _encoder.Create(context);
                            var type  = BuiltInType.Null;
                            dataset.Payload[datapoint.Key] = new DataValueModel {
                                Value = datapoint.Value == null
                                    ? null : codec.Encode(datapoint.Value.WrappedValue, out type),
                                DataType = type == BuiltInType.Null
                                    ? null : type.ToString(),
                                Status = (datapoint.Value?.StatusCode.Code == StatusCodes.Good)
                                    ? null : StatusCode.LookupSymbolicId(datapoint.Value.StatusCode.Code),
                                SourceTimestamp = (datapoint.Value?.SourceTimestamp == DateTime.MinValue)
                                    ? null : datapoint.Value?.SourceTimestamp,
                                SourcePicoseconds = (datapoint.Value?.SourcePicoseconds == 0)
                                    ? null : datapoint.Value?.SourcePicoseconds,
                                ServerTimestamp = (datapoint.Value?.ServerTimestamp == DateTime.MinValue)
                                    ? null : datapoint.Value?.ServerTimestamp,
                                ServerPicoseconds = (datapoint.Value?.ServerPicoseconds == 0)
                                    ? null : datapoint.Value?.ServerPicoseconds
                            };
                        }
                        await Task.WhenAll(_handlers.Select(h => h.HandleMessageAsync(dataset)));
                    }
                }
            }
            catch (Exception ex) {
                _logger.Error(ex, "Subscriber binary network message handling failed - skip");
            }
        }
示例#2
0
 public void TestBoolean(bool expected)
 {
     using (var stream = new MemoryStream())
         using (var encoder = new BinaryEncoder(stream))
             using (var decoder = new BinaryDecoder(stream))
             {
                 encoder.WriteBoolean(expected);
                 stream.Seek(0, SeekOrigin.Begin);
                 var actual = decoder.ReadBoolean();
                 Assert.AreEqual(expected, actual);
             }
 }
        public void DecodeBoolean(bool expectedValue, int expectedLength, byte[] value)
        {
            using (var stream = new MemoryStream(value))
                using (var decoder = new BinaryDecoder(stream))
                {
                    var decode = decoder.ReadBoolean();
                    Assert.AreEqual(expectedLength, stream.Position, "Decode offset error");
                    Assert.AreEqual(expectedValue, decode);

                    stream.Seek(0, SeekOrigin.Begin);
                    decoder.SkipBoolean();
                    Assert.AreEqual(expectedLength, stream.Position, "Skip offset error");
                }
        }
示例#4
0
            public Object getResponse()
            {
                Message lm = request.GetMessage(requestor.Local);
                Message rm;

                if (!requestor.remoteProtocol.Messages.TryGetValue(request.GetMessage(requestor.Local).Name, out rm))
                {
                    throw new AvroRuntimeException
                              ("Not a remote message: " + request.GetMessage(requestor.Local).Name);
                }

                Transceiver t = requestor.Transceiver;

                if ((lm.Oneway.GetValueOrDefault() != rm.Oneway.GetValueOrDefault()) && t.IsConnected)
                {
                    throw new AvroRuntimeException
                              ("Not both one-way messages: " + request.GetMessage(requestor.Local));
                }

                if (lm.Oneway.GetValueOrDefault() && t.IsConnected)
                {
                    return(null);                                                // one-way w/ handshake
                }
                RpcContext context = request.Context;

                context.ResponseCallMeta = MetaReader.Read(null, input);

                if (!input.ReadBoolean())
                {
                    // no error
                    Object response = requestor.ReadResponse(rm.Response, lm.Response, input);
                    context.Response = response;

                    return(response);
                }

                Exception error = requestor.ReadError(rm.SupportedErrors, lm.SupportedErrors, input);

                context.Error = error;

                throw error;
            }
示例#5
0
        /// <summary>
        /// Decode a scalar type
        /// </summary>
        /// <param name="binaryDecoder"></param>
        /// <param name="builtInType"></param>
        /// <returns>The decoded object</returns>
        private object DecodeRawScalar(BinaryDecoder binaryDecoder, byte builtInType)
        {
            switch ((BuiltInType)builtInType)
            {
            case BuiltInType.Boolean:
                return(binaryDecoder.ReadBoolean(null));

            case BuiltInType.SByte:
                return(binaryDecoder.ReadSByte(null));

            case BuiltInType.Byte:
                return(binaryDecoder.ReadByte(null));

            case BuiltInType.Int16:
                return(binaryDecoder.ReadInt16(null));

            case BuiltInType.UInt16:
                return(binaryDecoder.ReadUInt16(null));

            case BuiltInType.Int32:
                return(binaryDecoder.ReadInt32(null));

            case BuiltInType.UInt32:
                return(binaryDecoder.ReadUInt32(null));

            case BuiltInType.Int64:
                return(binaryDecoder.ReadInt64(null));

            case BuiltInType.UInt64:
                return(binaryDecoder.ReadUInt64(null));

            case BuiltInType.Float:
                return(binaryDecoder.ReadFloat(null));

            case BuiltInType.Double:
                return(binaryDecoder.ReadDouble(null));

            case BuiltInType.String:
                return(binaryDecoder.ReadString(null));

            case BuiltInType.DateTime:
                return(binaryDecoder.ReadDateTime(null));

            case BuiltInType.Guid:
                return(binaryDecoder.ReadGuid(null));

            case BuiltInType.ByteString:
                return(binaryDecoder.ReadByteString(null));

            case BuiltInType.XmlElement:
                return(binaryDecoder.ReadXmlElement(null));

            case BuiltInType.NodeId:
                return(binaryDecoder.ReadNodeId(null));

            case BuiltInType.ExpandedNodeId:
                return(binaryDecoder.ReadExpandedNodeId(null));

            case BuiltInType.StatusCode:
                return(binaryDecoder.ReadStatusCode(null));

            case BuiltInType.QualifiedName:
                return(binaryDecoder.ReadQualifiedName(null));

            case BuiltInType.LocalizedText:
                return(binaryDecoder.ReadLocalizedText(null));

            case BuiltInType.DataValue:
                return(binaryDecoder.ReadDataValue(null));

            case BuiltInType.Enumeration:
                return(binaryDecoder.ReadInt32(null));

            case BuiltInType.Variant:
                return(binaryDecoder.ReadVariant(null));

            case BuiltInType.ExtensionObject:
                return(binaryDecoder.ReadExtensionObject(null));

            default:
                return(null);
            }
        }
示例#6
0
        public static async Task Run(
            [EventHubTrigger("table-update", Connection = "eventHubConnection")] EventData[] events,
            [CosmosDB(
                 databaseName: "Temenos",
                 collectionName: "Events",
                 ConnectionStringSetting = "CosmosDBConnection")]
            IAsyncCollector <JObject> eventsOut,
            ILogger log)
        {
            log.LogInformation($"ProcessAvroEvent triggered with {events.Count()} events");

            var exceptions = new List <Exception>();

            foreach (EventData eventData in events)
            {
                try
                {
                    // convert messageBody of this event to a stream
                    MemoryStream stream = new MemoryStream(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);

                    // skip the first 2 bytes
                    stream.Position = 3;

                    // decode the stream and get the schema number
                    BinaryDecoder decoder      = new BinaryDecoder(stream);
                    var           magicCode    = (decoder.ReadBoolean() == false);
                    var           schemaNumber = decoder.ReadLong();

                    // get the appropriate schema
                    Schema schema = null;
                    switch (schemaNumber)
                    {
                    case 23:
                        schema = Schema.Parse(File.ReadAllText(@"SerializationID-46-CUSTOMER.avsc"));
                        break;

                    case -21:
                        schema = Schema.Parse(File.ReadAllText(@"SerializationID-41-DE_ADDRESS.avsc"));
                        break;

                    default:
                        throw new Exception("Unknown schema nuumber: " + schemaNumber);
                    }

                    // read the avro message using the identified schema
                    var           reader = new DefaultReader(schema, schema);
                    GenericRecord record = reader.Read(null, schema, schema, decoder) as GenericRecord;

                    // convert to JSON and return
                    JObject outputData = ConvertToJson(record);
                    eventsOut.AddAsync(outputData).Wait();

                    // Replace these two lines with your processing logic.
                    await Task.Yield();
                }
                catch (Exception e)
                {
                    // We need to keep processing the rest of the batch - capture this exception and continue.
                    // TODO: consider capturing details of the message that failed processing so it can be processed again later.
                    exceptions.Add(e);
                }
            }

            // Once processing of the batch is complete, if any messages in the batch failed processing throw an exception so that there is a record of the failure.

            if (exceptions.Count > 1)
            {
                throw new AggregateException(exceptions);
            }

            if (exceptions.Count == 1)
            {
                throw exceptions.Single();
            }
        }