Esempio n. 1
0
        private async Task ParseLine(string line)
        {
            StreamKernelCommand streamKernelCommand = null;

            try
            {
                streamKernelCommand = StreamKernelCommand.Deserialize(line);

                if (!_cancellationSource.IsCancellationRequested)
                {
                    var command = streamKernelCommand.Command;
                    command.Properties["id"] = streamKernelCommand.Id;
                    await _kernel.SendAsync(command, _cancellationSource.Token);
                }
            }
            catch (JsonReaderException)
            {
                Write(
                    new CommandParseFailure
                {
                    Body = line
                },
                    streamKernelCommand?.Id ?? -1);
            }
        }
        public Task Start()
        {
            return(Task.Run(async() =>
            {
                while (true)
                {
                    var line = await _input.ReadLineAsync();
                    if (line == null)
                    {
                        await Task.Delay(100);
                        continue;
                    }

                    StreamKernelCommand streamKernelCommand = null;
                    JObject obj = null;
                    try
                    {
                        obj = JObject.Parse(line);
                        streamKernelCommand = obj.ToObject <StreamKernelCommand>();
                        IKernelCommand command = null;

                        if (obj.TryGetValue("command", StringComparison.InvariantCultureIgnoreCase, out var commandValue))
                        {
                            command = DeserializeCommand(streamKernelCommand.CommandType, commandValue);
                        }

                        if (streamKernelCommand.CommandType == nameof(Quit))
                        {
                            return;
                        }

                        if (command == null)
                        {
                            Write(new CommandNotRecognized
                            {
                                Body = obj
                            },
                                  streamKernelCommand.Id);
                            continue;
                        }

                        var result = await _kernel.SendAsync(command);
                        result.KernelEvents.Subscribe(e =>
                        {
                            Write(e, streamKernelCommand.Id);
                        });
                    }
                    catch (JsonReaderException)
                    {
                        Write(new CommandParseFailure
                        {
                            Body = line
                        },
                              streamKernelCommand?.Id ?? -1);
                    }
                }
            }));
        }
Esempio n. 3
0
        public static int WriteMessage(this StreamWriter writer, IKernelCommand command, int?correlationId = null)
        {
            var message = new StreamKernelCommand
            {
                Id          = correlationId ?? Interlocked.Increment(ref _id),
                CommandType = command.GetType().Name,
                Command     = command
            };

            writer.WriteLine(JsonConvert.SerializeObject(message, _jsonSerializerSettings));
            writer.Flush();
            return(message.Id);
        }
Esempio n. 4
0
        public static int WriteMessage(this StreamWriter writer, IKernelCommand command)
        {
            var message = new StreamKernelCommand()
            {
                Id          = Interlocked.Increment(ref id),
                CommandType = command.GetType().Name,
                Command     = JsonConvert.SerializeObject(command)
            };

            writer.WriteLine(JsonConvert.SerializeObject(message));
            writer.Flush();
            return(message.Id);
        }