aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/VSIX/Tango.BuildExtensions/RemoteDebugForm.cs
blob: 99961cfcb6bf66786051a74eaa661e2d029aef86 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tango.PMR.Discovery;
using Tango.Transport.Discovery;

namespace Tango.BuildExtensions
{
    public partial class RemoteDebugForm : Form
    {
        private bool closed;

        public class RemoteRunnerService
        {
            public String Address { get; set; }
            public String HostName { get; set; }
            public int Port { get; set; }

            public override string ToString()
            {
                return String.Format("[{0}] - [{1}:{2}]", HostName, Address, Port);
            }
        }

        private UdpDiscoveryClient<BasicDiscoveryMessage> _discoveryClient;

        public RemoteRunnerService SelectedRemoteRunner { get; set; }

        public RemoteDebugForm(String projectName)
        {
            InitializeComponent();

            txtProjectName.Text = projectName;

            btnOK.Click += BtnOK_Click;
            btnCancel.Click += BtnCancel_Click;

            btnOK.Enabled = false;

            _discoveryClient = new UdpDiscoveryClient<BasicDiscoveryMessage>(2018);
            _discoveryClient.ServiceDiscovered += _discoveryClient_ServiceDiscovered;

            listServices.SelectedIndexChanged += ListServices_SelectedIndexChanged;
        }

        private void ListServices_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnOK.Enabled = listServices.SelectedItem != null;
        }

        private void _discoveryClient_ServiceDiscovered(object sender, DiscoveredService<BasicDiscoveryMessage> e)
        {
            if (!closed)
            {
                BeginInvoke(new Action(() =>
                {
                    if (e.Message.ServiceName == "Tango Remote Runner")
                    {
                        if (!listServices.Items.OfType<RemoteRunnerService>().ToList().Exists(x => x.Address == e.Address))
                        {
                            listServices.Items.Add(new RemoteRunnerService()
                            {
                                Address = e.Address,
                                HostName = e.HostName,
                                Port = e.Message.Port,
                            });
                        }
                    }
                }));
            }
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void BtnOK_Click(object sender, EventArgs e)
        {
            if (listServices.SelectedItem != null)
            {
                SelectedRemoteRunner = listServices.SelectedItem as RemoteRunnerService;
                DialogResult = DialogResult.OK;
                Close();
            }
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            _discoveryClient.Start();
        }

        protected override void OnClosed(EventArgs e)
        {
            closed = true;
            _discoveryClient.Stop();
        }
    }
}