From 8d1933f305e8fb4aa4cc7633fa02c419d77766ff Mon Sep 17 00:00:00 2001 From: Roy Ben Shabat Date: Wed, 15 Jan 2020 21:13:45 +0200 Subject: External bridge improvements. --- .../Tango.MachineService/Hubs/ExternalBridgeHub.cs | 65 ++++++++++++++++------ 1 file changed, 47 insertions(+), 18 deletions(-) (limited to 'Software/Visual_Studio/Web/Tango.MachineService') diff --git a/Software/Visual_Studio/Web/Tango.MachineService/Hubs/ExternalBridgeHub.cs b/Software/Visual_Studio/Web/Tango.MachineService/Hubs/ExternalBridgeHub.cs index d5ce0d145..a09cd7954 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService/Hubs/ExternalBridgeHub.cs +++ b/Software/Visual_Studio/Web/Tango.MachineService/Hubs/ExternalBridgeHub.cs @@ -41,6 +41,8 @@ namespace Tango.MachineService.Hubs public void RegisterMachine(MachineInfo machineInfo) { _externalBridges.RemoveAll(x => x.MachineInfo.SerialNumber == machineInfo.SerialNumber); + _sessions.RemoveAll(x => x.ExternalBridge.ConnectionID == Context.ConnectionId); + _externalBridges.Add(new ExternalBridgeInfo() { MachineInfo = machineInfo, @@ -51,6 +53,7 @@ namespace Tango.MachineService.Hubs public void UnregisterMachine() { _externalBridges.RemoveAll(x => x.ConnectionID == Context.ConnectionId); + _sessions.RemoveAll(x => x.ExternalBridge.ConnectionID == Context.ConnectionId); } public List GetAvailableMachines() @@ -64,6 +67,13 @@ namespace Tango.MachineService.Hubs if (externalBridge != null) { + var existingSession = GetSession(); + + if (existingSession != null) + { + _sessions.Remove(existingSession); + } + MachineSession session = new MachineSession(); session.ControllerConnectionID = Context.ConnectionId; session.ExternalBridge = externalBridge; @@ -75,16 +85,6 @@ namespace Tango.MachineService.Hubs return null; } - //public void NotifySessionCreated() - //{ - // var session = _sessions.SingleOrDefault(x => x.ReceiverConnectionID == Context.ConnectionId); - - // if (session != null) - // { - // Clients.Client(session.ControllerConnectionID).SessionCreated(); - // } - //} - public void JoinSession(String sessionID) { var session = _sessions.SingleOrDefault(x => x.SessionID == sessionID); @@ -98,7 +98,41 @@ namespace Tango.MachineService.Hubs public void Write(List dataCollection) { - var session = _sessions.SingleOrDefault(x => x.ReceiverConnectionID == Context.ConnectionId || x.ControllerConnectionID == Context.ConnectionId); + var connectionID = GetOtherSideConnectionID(); + + if (connectionID != null) + { + Clients.Client(connectionID).DataAvailable(dataCollection); + } + } + + public void LeaveSession() + { + var connectionID = GetOtherSideConnectionID(); + ClearSession(); + Clients.Client(connectionID).OnSessionClosed(); + } + + public override Task OnDisconnected(bool stopCalled) + { + _externalBridges.RemoveAll(x => x.ConnectionID == Context.ConnectionId); + _sessions.RemoveAll(x => x.ControllerConnectionID == Context.ConnectionId || x.ReceiverConnectionID == Context.ConnectionId || x.ExternalBridge.ConnectionID == Context.ConnectionId); + return base.OnDisconnected(stopCalled); + } + + private void ClearSession() + { + _sessions.RemoveAll(x => x.ControllerConnectionID == Context.ConnectionId || x.ReceiverConnectionID == Context.ConnectionId); + } + + private MachineSession GetSession() + { + return _sessions.SingleOrDefault(x => x.ReceiverConnectionID == Context.ConnectionId || x.ControllerConnectionID == Context.ConnectionId); + } + + private String GetOtherSideConnectionID() + { + var session = GetSession(); if (session != null) { @@ -113,15 +147,10 @@ namespace Tango.MachineService.Hubs connectionID = session.ReceiverConnectionID; } - Clients.Client(connectionID).DataAvailable(dataCollection); + return connectionID; } - } - public override Task OnDisconnected(bool stopCalled) - { - _externalBridges.RemoveAll(x => x.ConnectionID == Context.ConnectionId); - _sessions.RemoveAll(x => x.ControllerConnectionID == Context.ConnectionId || x.ReceiverConnectionID == Context.ConnectionId); - return base.OnDisconnected(stopCalled); + return null; } } } \ No newline at end of file -- cgit v1.3.1