예제 #1
0
        public void HandleMessage(Message message, RouterSocket serverSocket, PublisherSocket ioPub)
        {
            this.logger.Debug(string.Format("Message Content {0}", message.Content));
            ExecuteRequest executeRequest = JsonSerializer.Deserialize <ExecuteRequest>(message.Content);

            this.logger.Info(string.Format("Execute Request received with code {0}", executeRequest.Code));

            // 1: Send Busy status on IOPub
            this.SendMessageToIOPub(message, ioPub, StatusValues.Busy);

            // 2: Send execute input on IOPub
            this.SendInputMessageToIOPub(message, ioPub, executeRequest.Code);

            // 3: Evaluate the C# code
            IOPubConsole ioPubConsole = new IOPubConsole(message, ioPub, this.messageSender, this.executionCount, this.logger);

            ioPubConsole.RedirectConsole();
            string          code    = executeRequest.Code;
            ExecutionResult results = this.replEngine.Execute(code);

            ioPubConsole.CancelRedirect();

            string codeOutput     = this.GetCodeOutput(results);
            string codeHtmlOutput = this.GetCodeHtmlOutput(results);

            Dictionary <string, object> data = new Dictionary <string, object>()
            {
                { "text/plain", codeOutput },
                { "text/html", codeHtmlOutput }
            };

            DisplayData displayData = new DisplayData()
            {
                Data = data,
            };

            // 4: Send execute reply to shell socket
            this.SendExecuteReplyMessage(message, serverSocket);

            // 5: Send execute result message to IOPub
            if (results.OutputResultWithColorInformation.Any())
            {
                this.SendOutputMessageToIOPub(message, ioPub, displayData);
            }

            // 6: Send IDLE status message to IOPub
            this.SendMessageToIOPub(message, ioPub, StatusValues.Idle);

            this.executionCount += 1;
        }
예제 #2
0
        public void HandleMessage(Message message, RouterSocket serverSocket, PublisherSocket ioPub)
        {
            this.logger.Debug(string.Format("Message Content {0}", message.Content));
            ExecuteRequest executeRequest = message.Content.ToObject <ExecuteRequest>();

            this.logger.Info(string.Format("Execute Request received with code \n{0}", executeRequest.Code));

            // 1: Send Busy status on IOPub
            this.messageSender.SendStatus(message, ioPub, StatusValues.Busy);

            // 2: Send execute input on IOPub
            if (!executeRequest.Silent)
            {
                this.executionCount += 1;
                this.SendExecuteInputMessageToIOPub(message, ioPub, executeRequest.Code);
            }

            // 3: Evaluate the C# code
            IOPubConsole ioPubConsole = new IOPubConsole(message, ioPub, this.messageSender, this.executionCount, this.logger);

            ioPubConsole.RedirectConsole();
            string          code    = executeRequest.Code;
            ExecutionResult results = this.replEngine.Execute(code);

            ioPubConsole.CancelRedirect();

            if (!results.IsError)
            {
                // 4: Send execute result message to IOPub
                if (results.OutputResultWithColorInformation.Any())
                {
                    string codeOutput     = this.GetCodeOutput(results);
                    string codeHtmlOutput = this.GetCodeHtmlOutput(results);

                    JObject data = new JObject()
                    {
                        { "text/plain", codeOutput },
                        { "text/html", codeHtmlOutput }
                    };

                    DisplayData displayData = new DisplayData()
                    {
                        Data = data,
                    };
                    this.SendDisplayDataMessageToIOPub(message, ioPub, displayData);
                }

                // 5: Send execute reply to shell socket
                this.SendExecuteReplyOkMessage(message, serverSocket);
            }
            else
            {
                var     ex           = results.CompileError != null ? results.CompileError : results.ExecuteError;
                dynamic errorContent = new JObject();
                errorContent.execution_count = this.executionCount;
                errorContent.ename           = ex.GetType().ToString();
                errorContent.evalue          = ex.Message;
                var trace = new JArray(ex.StackTrace.Split('\n'));
                trace.AddFirst(ex.Message);
                errorContent.traceback = trace;

                // 6: Send error message to IOPub
                this.SendErrorMessageToIOPub(message, ioPub, errorContent);

                // 7: Send execute reply message to shell socket
                this.SendExecuteReplyErrorMessage(message, serverSocket, errorContent);
            }

            // 8: Send IDLE status message to IOPub
            this.messageSender.SendStatus(message, ioPub, StatusValues.Idle);
        }