public void SwitchToRemoteBroker(Uri uri) { var oldConnector = Interlocked.Exchange(ref _hostConnector, new RemoteRHostConnector(uri, _coreShell)); oldConnector.Dispose(); BrokerChanged?.Invoke(this, new EventArgs()); }
public void SwitchToLocalBroker(string name, string rBasePath = null, string rHostDirectory = null) { var installPath = new RInstallation().GetRInstallPath(rBasePath, new SupportedRVersionRange()); var newConnector = new LocalRHostConnector(name, installPath, rHostDirectory); var oldConnector = Interlocked.Exchange(ref _hostConnector, newConnector); oldConnector.Dispose(); BrokerChanged?.Invoke(this, new EventArgs()); }
private void OnBrokerChanged() { Task.Run(() => BrokerChanged?.Invoke(this, new EventArgs())).DoNotWait(); }
public async Task <bool> TrySwitchBrokerAsync(string name, string path = null, CancellationToken cancellationToken = default(CancellationToken)) { await TaskUtilities.SwitchToBackgroundThread(); var brokerClient = CreateBrokerClient(name, path); if (brokerClient == null) { return(false); } // Broker switching shouldn't be concurrent IAsyncReaderWriterLockToken lockToken = null; try { lockToken = await _connectArwl.WriterLockAsync(cancellationToken); } catch (OperationCanceledException) { lockToken?.Dispose(); brokerClient.Dispose(); return(false); } if (brokerClient.Name.EqualsOrdinal(_brokerProxy.Name) && brokerClient.Uri.AbsoluteUri.PathEquals(_brokerProxy.Uri.AbsoluteUri)) { brokerClient.Dispose(); try { // Switching to the broker that is currently running and connected is always successful if (IsConnected) { return(true); } await ReconnectAsync(cancellationToken, lockToken.Reentrancy); } catch (Exception) { return(false); } finally { lockToken.Dispose(); } OnBrokerConnected(); return(true); } // First switch broker proxy so that all new sessions are created for the new broker var oldBroker = _brokerProxy.Set(brokerClient); try { BrokerChanging?.Invoke(this, EventArgs.Empty); await SwitchBrokerAsync(oldBroker, cancellationToken, lockToken.Reentrancy); oldBroker.Dispose(); if (brokerClient.IsRemote) { _callback.Write(Environment.NewLine + Resources.Connected + Environment.NewLine); PrintBrokerInformation(); } } catch (Exception ex) { _brokerProxy.Set(oldBroker); brokerClient.Dispose(); BrokerChangeFailed?.Invoke(this, EventArgs.Empty); if (ex is OperationCanceledException || ex is RHostBrokerBinaryMissingException) // RHostDisconnectedException is derived from OperationCanceledException { return(false); } throw; } finally { lockToken.Dispose(); } OnBrokerConnected(); BrokerChanged?.Invoke(this, new EventArgs()); return(true); }
public void SwitchToRemoteBroker(Uri uri) { BrokerUri = uri; IsRemote = true; BrokerChanged?.Invoke(this, new EventArgs()); }
public void SwitchToLocalBroker(string name, string rBasePath = null, string rHostDirectory = null) { BrokerUri = rBasePath != null ? new Uri(rBasePath) : new Uri(@"C:\"); IsRemote = false; BrokerChanged?.Invoke(this, new EventArgs()); }