Esempio n. 1
0
        public ShellServer(
            ILogger <ShellServer> logger,
            IOptions <KernelContext> context,
            IServiceProvider provider,
            IShellRouter router
            )
        {
            this.logger   = logger;
            this.context  = context.Value;
            this.provider = provider;
            this.router   = router;

            router.RegisterHandler("kernel_info_request", async message => KernelInfoRequest?.Invoke(message));
            router.RegisterHandler("shutdown_request", async message => ShutdownRequest?.Invoke(message));
        }
Esempio n. 2
0
        public DebugMagic(
            ISymbolResolver resolver, IConfigurationSource configurationSource, IShellRouter router, IShellServer shellServer,
            ILogger <DebugMagic>?logger
            ) : base(
                "debug",
                new Microsoft.Jupyter.Core.Documentation
        {
            Summary     = "Steps through the execution of a given Q# operation or function.",
            Description = $@"
                    This magic command allows for stepping through the execution of a given Q# operation
                    or function using the QuantumSimulator.

                    #### Required parameters

                    - Q# operation or function name. This must be the first parameter, and must be a valid Q# operation
                    or function name that has been defined either in the notebook or in a Q# file in the same folder.
                    - Arguments for the Q# operation or function must also be specified as `key=value` pairs.
                ".Dedent(),
            Examples    = new[]
            {
                @"
                        Step through the execution of a Q# operation defined as `operation MyOperation() : Result`:
                        ```
                        In []: %debug MyOperation
                        Out[]: <interactive HTML for stepping through the operation>
                        ```
                    ".Dedent(),
                @"
                        Step through the execution of a Q# operation defined as `operation MyOperation(a : Int, b : Int) : Result`:
                        ```
                        In []: %debug MyOperation a=5 b=10
                        Out[]: <interactive HTML for stepping through the operation>
                        ```
                    ".Dedent(),
            }
        })
        {
            this.SymbolResolver      = resolver;
            this.ConfigurationSource = configurationSource;
            this.ShellServer         = shellServer;
            this.Logger = logger;
            router.RegisterHandler("iqsharp_debug_advance", this.HandleAdvanceMessage);
        }
Esempio n. 3
0
        public CommsRouter(IShellServer server, IShellRouter router, ILogger <CommsRouter>?logger = null)
        {
            this.Server = server;
            this.Router = router;
            this.logger = logger;

            router.RegisterHandler("comm_open", async(message) =>
            {
                if (message.Content is CommOpenContent openContent)
                {
                    if (sessionHandlers.TryGetValue(openContent.TargetName, out var handler))
                    {
                        var session = new CommSession(this, openContent.Id);
                        openSessions.Add(session.Id, session);
                        await handler.Handle(session, openContent.RawData);
                    }
                    else
                    {
                        // According to the Jupyter messaging protocol, we are
                        // supposed to ignore comm_open messages entirely if
                        // we don't recognizer the target_name property.
                        logger.LogWarning(
                            "Got a comm_open message for target name {TargetName}, but no handler for that target name has been registered.",
                            openContent.TargetName
                            );
                    }
                }
                else
                {
                    logger.LogError(
                        "Expected message content for a comm_open message, but got content of type {Type} instead.",
                        message.Content.GetType()
                        );
                }
            });

            router.RegisterHandler("comm_msg", async(message) =>
            {
                if (message.Content is CommMessageContent msgContent)
                {
                    if (!openSessions.TryGetValue(msgContent.Id, out var session))
                    {
                        logger.LogError(
                            "Got comms message for session {Id}, but no such session is currently open.",
                            session.Id
                            );
                    }
                    await session.HandleMessage(msgContent);
                }
                else
                {
                    logger.LogError(
                        "Expected message content for a comm_msg message, but got content of type {Type} instead.",
                        message.Content.GetType()
                        );
                }
            });

            router.RegisterHandler("comm_close", async(message) =>
            {
                if (message.Content is CommCloseContent closeContent)
                {
                    if (!openSessions.TryGetValue(closeContent.Id, out var session))
                    {
                        logger.LogError(
                            "Asked by client to close comms session with {Id}, but no such session is currently open.",
                            session.Id
                            );
                    }
                    openSessions.Remove(session.Id);
                    await session.HandleClose(CommSessionClosedBy.Client);
                }
                else
                {
                    logger.LogError(
                        "Expected message content for a comm_close message, but got content of type {Type} instead.",
                        message.Content.GetType()
                        );
                }
            });
        }