aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Common/Console/DefaultConsoleEngineService.cs
blob: 94b677b1821d1b4d8eaf069fe8e0350d76bffea9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Console;
using Tango.Console.Network;
using Tango.Core;
using Tango.Core.DI;
using Tango.Integration.ExternalBridge;
using Tango.PPC.Common.ExternalBridge;
using Tango.Transport;

namespace Tango.PPC.Common.Console
{
    /// <summary>
    /// Represents the <see cref="IConsoleEngineService"/> default implementation 
    /// which listens to incoming console request by registering as a external bridge request handler.
    /// </summary>
    /// <seealso cref="Tango.PPC.Common.Console.IConsoleEngineService" />
    /// <seealso cref="Tango.Integration.ExternalBridge.IExternalBridgeRequestHandler" />
    [TangoCreateWhenRegistered]
    public class DefaultConsoleEngineService : ExtendedObject, IConsoleEngineService, IExternalBridgeRequestHandler
    {
        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="IConsoleEngineService" /> is enabled.
        /// </summary>
        public bool Enabled { get; set; } = true;

        /// <summary>
        /// Initializes a new instance of the <see cref="DefaultConsoleEngineService"/> class.
        /// </summary>
        /// <param name="externalBridge">The external bridge service instance.</param>
        public DefaultConsoleEngineService(IPPCExternalBridgeService externalBridge)
        {
            externalBridge.RegisterRequestHandler(this);
        }

        /// <summary>
        /// Handles <see cref="GetCurrentDirectoryRequest"/> requests.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="token">The token.</param>
        /// <param name="transporter">The transporter.</param>
        [ExternalBridgeRequestHandlerMethod(typeof(GetCurrentDirectoryRequest), RequestHandlerLoggingMode.LogRequestName)]
        public async Task OnGetCurrentDirectoryRequest(GetCurrentDirectoryRequest request, String token, ITransporter transporter)
        {
            this.ThrowIfDisabled();

            await transporter.SendGenericResponse(new GetCurrentDirectoryResponse()
            {
                CurrentDirectory = Environment.CurrentDirectory,
                Suggestions = ConsoleExecutionEngine.GetSuggestions(Environment.CurrentDirectory)
            }, token);
        }

        /// <summary>
        /// Handles <see cref="ConsoleCommandRequest"/> requests.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="token">The token.</param>
        /// <param name="transporter">The transporter.</param>
        [ExternalBridgeRequestHandlerMethod(typeof(ConsoleCommandRequest), RequestHandlerLoggingMode.LogRequestNameAndContent)]
        public async Task OnConsoleCommandRequest(ConsoleCommandRequest request, String token, ITransporter transporter)
        {
            this.ThrowIfDisabled();

            LogManager.Log($"{nameof(ConsoleCommandRequest)} received with command '{request.Command}'. Executing...");

            ConsoleExecutionEngine engine = new ConsoleExecutionEngine();
            var result = await engine.Execute(request);

            LogManager.Log("Console command executed successfully.");

            await transporter.SendGenericResponse<ConsoleCommandResponse>(new ConsoleCommandResponse()
            {
                Output = result.Output,
                Suggestions = result.Suggestions,
                WorkingFolder = result.WorkingFolder
            }, token);
        }

        /// <summary>
        /// Called when any of the external bridge clients (receivers) has disconnected.
        /// </summary>
        /// <param name="receiver">The receiver.</param>
        public void OnReceiverDisconnected(ExternalBridgeReceiver receiver)
        {
            //Do nothing.
        }
    }
}