aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Common/Console/DefaultConsoleEngineService.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Software/Visual_Studio/PPC/Tango.PPC.Common/Console/DefaultConsoleEngineService.cs')
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.Common/Console/DefaultConsoleEngineService.cs92
1 files changed, 92 insertions, 0 deletions
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Console/DefaultConsoleEngineService.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/Console/DefaultConsoleEngineService.cs
new file mode 100644
index 000000000..94b677b18
--- /dev/null
+++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Console/DefaultConsoleEngineService.cs
@@ -0,0 +1,92 @@
+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.
+ }
+ }
+}