aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Common/Console/DefaultConsoleEngineService.cs
blob: 4a9ee468a9e0e53a770d54e020f032d6c980f9aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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.DI;
using Tango.Integration.ExternalBridge;
using Tango.PPC.Common.ExternalBridge;
using Tango.Transport;

namespace Tango.PPC.Common.Console
{
    [TangoCreateWhenRegistered]
    public class DefaultConsoleEngineService : IConsoleEngineService, IExternalBridgeRequestHandler
    {
        public bool Enabled { get; set; } = true;

        public DefaultConsoleEngineService(IPPCExternalBridgeService externalBridge)
        {
            externalBridge.RegisterRequestHandler(this);
        }

        [ExternalBridgeRequestHandlerMethod(typeof(GetCurrentDirectoryRequest))]
        public async void OnGetCurrentDirectoryRequest(GetCurrentDirectoryRequest request, String token, ITransporter transporter)
        {
            await transporter.SendGenericResponse(new GetCurrentDirectoryResponse()
            {
                CurrentDirectory = Environment.CurrentDirectory,
                Suggestions = ConsoleExecutionEngine.GetSuggestions(Environment.CurrentDirectory)
            }, token);
        }

        [ExternalBridgeRequestHandlerMethod(typeof(ConsoleCommandRequest))]
        public async void OnConsoleCommandRequest(ConsoleCommandRequest request, String token, ITransporter transporter)
        {
            if (Enabled)
            {
                try
                {
                    ConsoleExecutionEngine engine = new ConsoleExecutionEngine();
                    var result = await engine.Execute(request);
                    await transporter.SendGenericResponse<ConsoleCommandResponse>(new ConsoleCommandResponse()
                    {
                        Output = result.Output,
                        Suggestions = result.Suggestions,
                        WorkingFolder = result.WorkingFolder
                    }, token, new TransportResponseConfig()
                    {
                        Immediate = true,
                    });
                }
                catch (Exception ex)
                {
                    await transporter.SendErrorResponse(ex, token);
                }
            }
        }

        public void OnReceiverDisconnected(ExternalBridgeReceiver receiver)
        {

        }
    }
}