aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Emulations/ExternalBridge/EmulatorExternalBridge.cs
blob: d8c988436798f5fa8ce41af356223ead2aa5d623 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL.Entities;
using Tango.Emulations.Emulators;
using Tango.Integration.ExternalBridge;
using Tango.Integration.Operation;
using Tango.Transport.Adapters;
using Tango.Transport.Transporters;

namespace Tango.Emulations.ExternalBridge
{
    /// <summary>
    /// Represents an in-memory external bridge client emulator.
    /// </summary>
    /// <seealso cref="Tango.Integration.Operation.MachineOperator" />
    /// <seealso cref="Tango.Integration.ExternalBridge.IExternalBridgeClient" />
    public class EmulatorExternalBridge : MachineOperator, IExternalBridgeClient
    {
        /// <summary>
        /// Gets a value indicating whether this client requires authentication.
        /// </summary>
        public bool RequiresAuthentication { get; }

        /// <summary>
        /// Gets or sets the machine serial number.
        /// </summary>
        public string SerialNumber { get; set; }

        /// <summary>
        /// Gets the emulator.
        /// </summary>
        public MachineEmulator Emulator { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="EmulatorExternalBridge"/> class.
        /// </summary>
        public EmulatorExternalBridge()
        {
            ComponentName = $"External Bridge Emulator Client {_component_counter++}";

            EnableDiagnostics = true;
            EnableEmbeddedDebugging = true;
            EnableEventsNotification = true;
            EnableAutomaticThreadLoading = true;

            String address = new string(Guid.NewGuid().ToString().Replace("-", "").TakeLast(4).ToArray());

            Adapter = new MemoryTransportAdapter(address);
            Emulator = new MachineEmulator(new BasicTransporter(new MemoryTransportAdapter(address)));
        }

        /// <summary>
        /// Connects the transport component.
        /// </summary>
        /// <returns></returns>
        public override async Task Connect()
        {
            await Emulator.Start();
            await base.Connect();
        }

        /// <summary>
        /// Disconnects the machine operator and the underlying transporter.
        /// </summary>
        /// <returns></returns>
        public override async Task Disconnect()
        {
            await base.Disconnect();
            await Emulator.Stop();
        }

        /// <summary>
        /// Gets the database machine associated with this client.
        /// </summary>
        public Machine Machine { get; private set; }

        /// <summary>
        /// Sets the database machine.
        /// </summary>
        /// <param name="machine">The machine.</param>
        public void SetMachine(Machine machine)
        {
            Machine = machine;
        }

        public bool CompressionEnabled { get; set; }
    }
}