public async Task SendAsync(KernelCommand command, string token = null)
        {
            if (!string.IsNullOrWhiteSpace(token))
            {
                command.SetToken(token);
            }

            var completionSource = new TaskCompletionSource <bool>();

            token = command.GetToken();
            var sub = _kernelEvents
                      .Where(e => e.Command.GetToken() == token)
                      .Subscribe(kernelEvent =>
            {
                switch (kernelEvent)
                {
                case CommandFailed _:
                case CommandSucceeded _:
                    completionSource.SetResult(true);
                    break;
                }
            });

            var envelope = KernelCommandEnvelope.Create(command);

            var serialized = KernelCommandEnvelope.Serialize(envelope);

            _output.Write(serialized);

            await completionSource.Task;

            sub.Dispose();
        }
Esempio n. 2
0
        public static string Serialize(IKernelEventEnvelope eventEnvelope)
        {
            KernelCommandEnvelope.SerializationModel commandSerializationModel = null;

            if (eventEnvelope.Event.Command is not null && eventEnvelope.Event.Command is not NoCommand)
            {
                var commandEnvelope = KernelCommandEnvelope.Create(eventEnvelope.Event.Command);

                commandSerializationModel = new KernelCommandEnvelope.SerializationModel
                {
                    command     = commandEnvelope.Command,
                    commandType = commandEnvelope.CommandType,
                    token       = eventEnvelope.Event.Command.GetToken()
                };
            }

            var serializationModel = new SerializationModel
            {
                @event    = eventEnvelope.Event,
                eventType = eventEnvelope.EventType,
                command   = commandSerializationModel
            };

            return(JsonSerializer.Serialize(
                       serializationModel,
                       Serializer.JsonSerializerOptions));
        }
Esempio n. 3
0
        public static IKernelEventEnvelope Deserialize(string json)
        {
            var jsonObject = JObject.Parse(json);

            var commandJson = jsonObject[nameof(SerializationModel.command)];

            var commandEnvelope = KernelCommandEnvelope.Deserialize(commandJson);

            var eventJson = jsonObject[nameof(SerializationModel.@event)];

            var eventTypeName = jsonObject[nameof(SerializationModel.eventType)].Value <string>();

            var eventType = EventTypeByName(eventTypeName);

            var @event = (KernelEvent)eventJson.ToObject(eventType, Serializer.JsonSerializer);

            if (@event is {} &&
Esempio n. 4
0
        private async Task DeserializeAndSendCommand(string line)
        {
            IKernelCommandEnvelope streamKernelCommand;

            try
            {
                streamKernelCommand = KernelCommandEnvelope.Deserialize(line);
            }
            catch (JsonReaderException ex)
            {
                WriteEventToOutput(
                    new DiagnosticLogEntryProduced(
                        $"Error while parsing command: {ex.Message}\n{line}"));

                return;
            }

            await _kernel.SendAsync(streamKernelCommand.Command);
        }
Esempio n. 5
0
        public static IKernelEventEnvelope Deserialize(JsonElement jsonObject)
        {
            var commandJson = jsonObject.GetProperty(nameof(SerializationModel.command));

            var commandEnvelope = commandJson.ValueKind == JsonValueKind.Null ? null : KernelCommandEnvelope.Deserialize(commandJson);

            var command = commandEnvelope?.Command;

            return(DeserializeWithCommand(jsonObject, command));
        }
        public static IKernelEventEnvelope Deserialize(string json)
        {
            var jsonObject = JsonDocument.Parse(json).RootElement;

            var commandJson = jsonObject.GetProperty(nameof(SerializationModel.command));

            var commandEnvelope = commandJson.ValueKind == JsonValueKind.Null ? null : KernelCommandEnvelope.Deserialize(commandJson);

            var eventJson = jsonObject.GetProperty(nameof(SerializationModel.@event));

            var eventTypeName = jsonObject.GetProperty(nameof(SerializationModel.eventType)).GetString();

            var eventType = EventTypeByName(eventTypeName);

            var ctor = eventType.GetConstructors(BindingFlags.IgnoreCase
                                                 | BindingFlags.Public
                                                 | BindingFlags.Instance)[0];

            var ctorParams = new List <object>();

            foreach (var parameterInfo in ctor.GetParameters())
            {
                if (typeof(KernelCommand).IsAssignableFrom(parameterInfo.ParameterType))
                {
                    ctorParams.Add(commandEnvelope == null ? KernelCommand.None: commandEnvelope.Command);
                }
                else
                {
                    ctorParams.Add(eventJson.TryGetProperty(parameterInfo.Name, out var property)
                        ? JsonSerializer.Deserialize(property.GetRawText(), parameterInfo.ParameterType,
                                                     Serializer.JsonSerializerOptions)
                        : GetDefaultValueForType(parameterInfo.ParameterType));
                }
            }

            var @event = (KernelEvent)ctor.Invoke(ctorParams.ToArray());

            return(Create(@event));
        }
Esempio n. 7
0
        public static IKernelEventEnvelope Deserialize(string json)
        {
            var jsonObject = JsonDocument.Parse(json).RootElement;

            var commandJson = jsonObject.GetProperty(nameof(SerializationModel.command));

            var commandEnvelope = commandJson.ValueKind == JsonValueKind.Null ? null : KernelCommandEnvelope.Deserialize(commandJson);

            var command = commandEnvelope?.Command;

            return(DeserializeWithCommand(json, command));
        }