예제 #1
0
        private Dictionary <string, IConnection> CreateConnectionList()
        {
            var connections         = GetConnectionsFromSettings();
            var localEngines        = _installationService.GetCompatibleEngines().ToList();
            var containers          = _containers.GetRunningContainers();
            var connectionsToRemove = new List <string>();

            // Remove missing engines and add engines missing from saved connections
            // Set 'is used created' to false if path points to locally found interpreter

            foreach (var connection in connections.Values)
            {
                var name = connection.Name;

                // Remove connections for missing engines
                if (!connection.IsRemote)
                {
                    var isValid = connection.Uri.IsFile
                        ? IsValidLocalConnection(name, connection.Path)
                        : containers.Any(c => c.Name.EqualsOrdinal(name));

                    if (!isValid)
                    {
                        connectionsToRemove.Add(name);
                    }
                }

                // For MRS and MRC always use generated name. These upgrade in place
                // so keeping old name with old version doesn't make sense.
                // TODO: handle this better in the future by storing version.
                if (connection.Path.ContainsIgnoreCase("R_SERVER"))
                {
                    connectionsToRemove.Add(name);
                }
            }

            foreach (var connectionName in connectionsToRemove)
            {
                connections.Remove(connectionName);
            }

            // Add newly installed engines
            foreach (var e in localEngines)
            {
                if (!connections.Values.Any(x => x.Path.PathEquals(e.InstallPath)))
                {
                    connections[e.Name] = Connection.Create(_securityService, e.Name, e.InstallPath, string.Empty, false, _settings.ShowHostLoadMeter);
                }
            }

            // Add running containers
            foreach (var container in containers)
            {
                if (!connections.ContainsKey(container.Name))
                {
                    connections[container.Name] = Connection.Create(_securityService, container, string.Empty, false, _settings.ShowHostLoadMeter);
                }
            }

            // Verify that most recently used connection is still valid
            var last = _settings.LastActiveConnection;

            if (last != null && connections.TryGetValue(last.Name, out IConnection lastConnection) && !lastConnection.IsRemote && !IsValidLocalConnection(last.Name, last.Path))
            {
                // Installation was removed or otherwise disappeared
                _settings.LastActiveConnection = null;
            }

            if (_settings.LastActiveConnection == null && connections.Any())
            {
                // Perhaps first time launch with R preinstalled.
                var connection = PickBestLocalRConnection(connections.Values, localEngines);
                connection.LastUsed            = DateTime.Now;
                _settings.LastActiveConnection = new ConnectionInfo(connection)
                {
                    LastUsed = DateTime.Now
                };
            }

            return(connections);
        }