private void ReadConnections(ConfigParams connections) { lock (_lock) { _items.Clear(); foreach (var entry in connections) { var item = new DiscoveryItem() { Key = entry.Key, Connection = ConnectionParams.FromString(entry.Value) }; _items.Add(item); } } }
/// <summary> /// Resolves a single connection parameters by its key. /// </summary> /// <param name="correlationId">(optional) transaction id to trace execution through call chain.</param> /// <param name="key">a key to uniquely identify the connection.</param> /// <returns>a resolved connection.</returns> public async Task <ConnectionParams> ResolveOneAsync(string correlationId, string key) { ConnectionParams connection = null; lock (_lock) { foreach (var item in _items) { if (item.Key == key && item.Connection != null) { connection = item.Connection; break; } } } return(await Task.FromResult(connection)); }
/// <summary> /// Validates connection parameters. /// This method can be overriden in child classes. /// Throw error if validation failed. /// </summary> /// <param name="correlationId">(optional) transaction id to trace execution through call chain.</param> /// <param name="connection">connection parameters to be validated</param> protected void ValidateConnection(string correlationId, ConnectionParams connection) { if (connection == null) { throw new ConfigException(correlationId, "NO_CONNECTION", "Connection parameters are not set is not set"); } // URI usually contains all information var uri = connection.Uri; if (uri != null) { return; } var protocol = connection.GetProtocolWithDefault(_defaultProtocol); if (protocol == null) { throw new ConfigException(correlationId, "NO_PROTOCOL", "Connection protocol is not set"); } if (_supportedProtocols != null && _supportedProtocols.IndexOf(protocol) < 0) { throw new ConfigException(correlationId, "UNSUPPORTED_PROTOCOL", "The protocol " + protocol + " is not supported"); } var host = connection.Host; if (host == null) { throw new ConfigException(correlationId, "NO_HOST", "Connection host is not set"); } var port = connection.GetPortWithDefault(_defaultPort); if (port == 0) { throw new ConfigException(correlationId, "NO_PORT", "Connection port is not set"); } }
/// <summary> /// Resolves all component connection. If connections are configured to be /// retrieved from Discovery service it finds a IDiscovery and resolves the connection there. /// </summary> /// <param name="correlationId">(optional) transaction id to trace execution through call chain.</param> /// <returns>a list of resolved connections.</returns> public async Task <List <ConnectionParams> > ResolveAllAsync(string correlationId) { var resolved = new List <ConnectionParams>(); var toResolve = new List <ConnectionParams>(); // Sort connections foreach (var connection in _connections) { if (connection.UseDiscovery) { toResolve.Add(connection); } else { resolved.Add(connection); } } // Resolve addresses that require that if (toResolve.Count <= 0) { return(resolved); } foreach (var connection in toResolve) { var resolvedConnections = await ResolveAllInDiscoveryAsync(correlationId, connection); foreach (var resolvedConnection in resolvedConnections) { // Merge configured and new parameters var localResolvedConnection = new ConnectionParams(ConfigParams.MergeConfigs(connection, resolvedConnection)); resolved.Add(localResolvedConnection); } } return(resolved); }
/// <summary> /// Merges connection options with connection parameters /// This method can be overriden in child classes. /// </summary> /// <param name="options">connection options</param> /// <param name="connection">connection parameters to be merged</param> /// <returns>merged connection options.</returns> protected ConfigParams MergeConnection(ConfigParams options, ConnectionParams connection) { var mergedOptions = options.SetDefaults(connection); return(mergedOptions); }
/// <summary> /// Configures component by passing configuration parameters. /// </summary> /// <param name="config">configuration parameters to be set.</param> /// <param name="configAsDefault">boolean parameter for default configuration. If "true" /// the default value will be added to the result.</param> public void Configure(ConfigParams config, bool configAsDefault = true) { _connections.AddRange(ConnectionParams.ManyFromConfig(config, configAsDefault)); }
private async Task <List <ConnectionParams> > ResolveAllInDiscoveryAsync(string correlationId, ConnectionParams connection) { var result = new List <ConnectionParams>(); if (connection.UseDiscovery == false) { return(result); } var key = connection.DiscoveryKey; if (_references == null) { return(null); } var discoveries = _references.GetOptional <IDiscovery>(new Descriptor("*", "discovery", "*", "*", "*")); if (discoveries.Count == 0) { throw new ConfigException(correlationId, "CANNOT_RESOLVE", "Discovery wasn't found to make resolution"); } foreach (var discovery in discoveries) { var resolvedConnections = await discovery.ResolveAllAsync(correlationId, key); if (resolvedConnections != null) { result.AddRange(resolvedConnections); } } return(result); }
/// <summary> /// Adds a new connection to component connections /// </summary> /// <param name="connection">new connection parameters to be added</param> public void Add(ConnectionParams connection) { _connections.Add(connection); }