aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Integration
diff options
context:
space:
mode:
Diffstat (limited to 'Software/Visual_Studio/Tango.Integration')
-rw-r--r--Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeScanner.cs122
-rw-r--r--Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeService.cs9
-rw-r--r--Software/Visual_Studio/Tango.Integration/IntegrationSettings.cs13
3 files changed, 126 insertions, 18 deletions
diff --git a/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeScanner.cs b/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeScanner.cs
index 694502d2e..da55a13d3 100644
--- a/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeScanner.cs
+++ b/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeScanner.cs
@@ -22,7 +22,10 @@ using Tango.PMR;
using Tango.PMR.Common;
using Tango.PMR.Integration;
using Tango.Settings;
+using Tango.Transport;
using Tango.Transport.Adapters;
+using Tango.Transport.Discovery;
+using Tango.Transport.Helpers;
namespace Tango.Integration.ExternalBridge
{
@@ -35,7 +38,6 @@ namespace Tango.Integration.ExternalBridge
private Thread _tcpDiscoveryThread;
private Thread _usbDiscoveryThread;
private Thread _signalRDiscoveryThread;
- private UdpClient _server;
private IntegrationSettings _settings;
private HubConnection _connection;
private IHubProxy _proxy;
@@ -99,13 +101,6 @@ namespace Tango.Integration.ExternalBridge
{
LogManager.Log("External bridge scanner started...");
- if (_server == null)
- {
- _server = new UdpClient();
- _server.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
- _server.Client.Bind(new IPEndPoint(IPAddress.Any, _settings.ExternalBridgeServiceDiscoveryPort));
- }
-
IsStarted = true;
foreach (var machine in AvailableMachines.OfType<ExternalBridgeTcpClient>().ToList())
@@ -167,6 +162,19 @@ namespace Tango.Integration.ExternalBridge
if (IsStarted)
{
IsStarted = false;
+
+ try
+ {
+ _usbDiscoveryThread.Abort();
+ _tcpDiscoveryThread.Abort();
+ _signalRDiscoveryThread.Abort();
+ AvailableMachines.Clear();
+ }
+ catch (Exception ex)
+ {
+ Debug.WriteLine(ex);
+ }
+
LogManager.Log("External bridge scanner stopped.");
}
}
@@ -209,13 +217,39 @@ namespace Tango.Integration.ExternalBridge
{
try
{
- var ClientEp = new IPEndPoint(IPAddress.Any, _settings.ExternalBridgeServiceDiscoveryPort);
- _server.EnableBroadcast = true;
- var ClientRequestData = _server.Receive(ref ClientEp);
+ ExternalBridgeUdpDiscoveryPacket discoveryPacket = null;
+ String address = null;
- ExternalBridgeUdpDiscoveryPacket packet = ExternalBridgeUdpDiscoveryPacket.Parser.ParseFrom(ClientRequestData);
+ if (_settings.ExternalBridgeDiscoveryMethod == DiscoveryMethod.Multicast)
+ {
+ using (UdpClient udpClient = new UdpClient())
+ {
+ udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
+ udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, _settings.ExternalBridgeServiceDiscoveryPort));
+ udpClient.JoinMulticastGroup(IPAddress.Parse(_settings.ExternalBridgeMulticastGroup), IPAddress.Parse(IPAddressHelper.GetLocalIpAddress()));
- String address = ClientEp.Address.ToString();
+ var endPoint = new IPEndPoint(IPAddress.Any, 0);
+ var discoveryData = udpClient.Receive(ref endPoint);
+
+ discoveryPacket = ExternalBridgeUdpDiscoveryPacket.Parser.ParseFrom(discoveryData);
+ address = endPoint.Address.ToString();
+ }
+ }
+ else
+ {
+ using (UdpClient udpClient = new UdpClient())
+ {
+ udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
+ udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, _settings.ExternalBridgeServiceDiscoveryPort));
+ var endPoint = new IPEndPoint(IPAddress.Any, _settings.ExternalBridgeServiceDiscoveryPort);
+
+ udpClient.EnableBroadcast = true;
+ var discoveryData = udpClient.Receive(ref endPoint);
+
+ discoveryPacket = ExternalBridgeUdpDiscoveryPacket.Parser.ParseFrom(discoveryData);
+ address = endPoint.Address.ToString();
+ }
+ }
//validate service existence using TCP connection.
try
@@ -226,7 +260,7 @@ namespace Tango.Integration.ExternalBridge
}
catch
{
- var disconnected_machine = AvailableMachines.OfType<ExternalBridgeTcpClient>().ToList().FirstOrDefault(x => x.SerialNumber == packet.SerialNumber && x.IPAddress == address);
+ var disconnected_machine = AvailableMachines.OfType<ExternalBridgeTcpClient>().ToList().FirstOrDefault(x => x.SerialNumber == discoveryPacket.SerialNumber && x.IPAddress == address);
if (disconnected_machine != null)
{
@@ -238,14 +272,14 @@ namespace Tango.Integration.ExternalBridge
continue;
}
- if (!AvailableMachines.OfType<ExternalBridgeTcpClient>().ToList().Exists(x => x.SerialNumber == packet.SerialNumber && x.IPAddress == address))
+ if (!AvailableMachines.OfType<ExternalBridgeTcpClient>().ToList().Exists(x => x.SerialNumber == discoveryPacket.SerialNumber && x.IPAddress == address))
{
ExternalBridgeTcpClient newMachine = null;
- var knownMachine = KnownMachines.FirstOrDefault(x => x.SerialNumber == packet.SerialNumber);
+ var knownMachine = KnownMachines.FirstOrDefault(x => x.SerialNumber == discoveryPacket.SerialNumber);
if (knownMachine == null && KnownMachines.Count == 0)
{
- newMachine = new ExternalBridgeTcpClient(packet.SerialNumber, address);
+ newMachine = new ExternalBridgeTcpClient(discoveryPacket.SerialNumber, address);
}
else if (knownMachine != null)
{
@@ -275,6 +309,8 @@ namespace Tango.Integration.ExternalBridge
{
LogManager.Log(ex);
}
+
+ Thread.Sleep(100);
}
}
@@ -363,5 +399,57 @@ namespace Tango.Integration.ExternalBridge
MachineLost?.Invoke(this, sender as IExternalBridgeClient);
}
}
+
+ public void TryGetMachineByIP(String ip)
+ {
+ IPAddress _IP;
+ if (IPAddress.TryParse(ip, out _IP))
+ {
+ try
+ {
+ TcpClient client = new TcpClient();
+ client.Connect(ip, _settings.ExternalBridgeServiceDiscoveryPort);
+ Thread.Sleep(100);
+ byte[] data = new byte[client.Available];
+ client.GetStream().Read(data, 0, data.Length);
+ client.Dispose();
+ TcpValidationInfo info = GenericMessageSerializer.Deserialize<TcpValidationInfo>(data, GenericMessageProtocol.Bson);
+
+ ExternalBridgeTcpClient newMachine = null;
+ var knownMachine = KnownMachines.FirstOrDefault(x => x.SerialNumber == info.SerialNumber);
+
+ if (knownMachine == null && KnownMachines.Count == 0)
+ {
+ newMachine = new ExternalBridgeTcpClient(info.SerialNumber, ip);
+ }
+ else if (knownMachine != null)
+ {
+ newMachine = new ExternalBridgeTcpClient(knownMachine, ip);
+ }
+
+ if (newMachine != null)
+ {
+ LogManager.Log("Found a new machine via IP Address" + newMachine.SerialNumber);
+
+ ThreadsHelper.InvokeUINow(() =>
+ {
+ if (AvailableMachines.Count > 0)
+ {
+ AvailableMachines.Insert(1, newMachine);
+ }
+ else
+ {
+ AvailableMachines.Add(newMachine);
+ }
+ MachineDiscovered?.Invoke(this, newMachine);
+ });
+ }
+ }
+ catch (Exception ex)
+ {
+ LogManager.Log(ex, "Error occurred when trying to find a machine by IP.");
+ }
+ }
+ }
}
}
diff --git a/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeService.cs b/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeService.cs
index 4218f9e9a..ae7cace31 100644
--- a/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeService.cs
+++ b/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeService.cs
@@ -40,6 +40,7 @@ namespace Tango.Integration.ExternalBridge
private TcpServer _tcpServer;
private int _discovery_port = 8888; //Will be overridden by settings in constructor..
private int _external_bridge_port = 1984; //Will be overridden by settings in constructor..
+ private String _multicastAddress = "234.55.66.77"; //Will be overridden by settings in constructor..
private HubConnection _connection;
private IHubProxy _proxy;
private bool _isSignalRConnected;
@@ -199,6 +200,7 @@ namespace Tango.Integration.ExternalBridge
_discovery_port = settings.ExternalBridgeServiceDiscoveryPort;
_external_bridge_port = settings.ExternalBridgeServicePort;
+ _multicastAddress = settings.ExternalBridgeMulticastGroup;
_tcpServer = new TcpServer(_external_bridge_port);
_tcpServer.ClientConnected += _tcpServer_ClientConnected;
@@ -234,7 +236,12 @@ namespace Tango.Integration.ExternalBridge
_discoveryService = new UdpDiscoveryService<ExternalBridgeUdpDiscoveryPacket>(_discovery_port, new ExternalBridgeUdpDiscoveryPacket()
{
SerialNumber = Machine.SerialNumber,
- });
+ Guid = Machine.Guid,
+ })
+ {
+ MulticastGroupAddress = _multicastAddress,
+ TcpValidationInfo = new TcpValidationInfo() { Guid = Machine.Guid, SerialNumber = Machine.SerialNumber }
+ };
_discoveryService.BeforeBroadcasting -= _discoverySevice_BeforeBroadcasting;
_discoveryService.BeforeBroadcasting += _discoverySevice_BeforeBroadcasting;
diff --git a/Software/Visual_Studio/Tango.Integration/IntegrationSettings.cs b/Software/Visual_Studio/Tango.Integration/IntegrationSettings.cs
index ce6ace32a..20d0b76e4 100644
--- a/Software/Visual_Studio/Tango.Integration/IntegrationSettings.cs
+++ b/Software/Visual_Studio/Tango.Integration/IntegrationSettings.cs
@@ -5,6 +5,7 @@ using System.Text;
using System.Threading.Tasks;
using Tango.Settings;
using Tango.Transport.Adapters;
+using Tango.Transport.Discovery;
namespace Tango.Integration
{
@@ -21,6 +22,16 @@ namespace Tango.Integration
public int ExternalBridgeServiceDiscoveryPort { get; set; }
/// <summary>
+ /// Gets or sets the external bridge multi-cast group address.
+ /// </summary>
+ public String ExternalBridgeMulticastGroup { get; set; }
+
+ /// <summary>
+ /// Gets or sets the discovery method.
+ /// </summary>
+ public DiscoveryMethod ExternalBridgeDiscoveryMethod { get; set; }
+
+ /// <summary>
/// Gets or sets the name of the embedded device.
/// </summary>
public String EmbeddedDeviceName { get; set; }
@@ -42,6 +53,8 @@ namespace Tango.Integration
{
ExternalBridgeServicePort = 1984;
ExternalBridgeServiceDiscoveryPort = 8888;
+ ExternalBridgeMulticastGroup = "234.55.66.77";
+ ExternalBridgeDiscoveryMethod = DiscoveryMethod.Multicast;
EmbeddedDeviceName = "Tango USB Serial Port";
FilterExternalBridgeUsbMachines = false;
EmbeddedSerialBaudRate = UsbSerialBaudRates.BR_115200;