/// <summary> /// The constructor. /// </summary> /// <param name="configuration">The configuration to use for the cache client.</param> public CacheClient(CacheClientConfigurationSection configuration) { // Sanitize if (configuration == null) { throw new ArgumentNullException("configuration"); } // Load custom logging _logger = CustomTypesLoader.LoadLogger(configuration); // Configure custom serializer _binarySerializer = CustomTypesLoader.LoadSerializer(configuration); CacheHostBucket currentCacheHostBucket = new CacheHostBucket(); // checks the auto discover hosts is enabled, in this case hosts configuration will be discarded if (configuration.AutoDetectCacheHosts) { if (configuration.UdpMulticastIp == null) { throw new ArgumentNullException("UdpMulticastIp"); } if (!(configuration.UdpMulticastPort > 0)) { throw new ArgumentException("UdpMulticastPort"); } _logger.Info("Launching multicast", "Launching the auto discover thread to discover cache hosts connected..."); _cacheDiscoveryManager = new MulticastUDPCacheHostAutoDetectManager(IPAddress.Parse(configuration.UdpMulticastIp), configuration.UdpMulticastPort); _multicastThread = new Thread(new ThreadStart(_cacheDiscoveryManager.Run)); _multicastThread.Start(); } PerformCacheHostsFromConfiguration(configuration, currentCacheHostBucket); }
/// <summary> /// The constructor. /// </summary> public CacheClient() { // Load custom logging _logger = CustomTypesLoader.LoadLogger(); // Configure custom serializer _binarySerializer = CustomTypesLoader.LoadSerializer(); // Get the cache hosts from configuration var cacheHosts = CacheClientConfigurationSection.Settings.CacheHosts; // Get the cache host reconnect interval from configuration var hostReconnectIntervalSeconds = CacheClientConfigurationSection.Settings.HostReconnectIntervalSeconds; // Sanitize if (cacheHosts == null) { throw new ConfigurationErrorsException("At least one cache host must be specified in your application's configuration."); } // Get the host redundancy layers from configuration var hostRedundancyLayers = CacheClientConfigurationSection.Settings.HostRedundancyLayers; CacheHostBucket currentCacheHostBucket = new CacheHostBucket(); // Assign the cache hosts to buckets in a specified order foreach (CacheHostElement cacheHost in cacheHosts.OfType <CacheHostElement>().OrderBy(i => i.Address).ThenBy(i => i.Port)) { // Instantiate a communication client var communicationClient = new CommunicationClient(cacheHost.Address, cacheHost.Port, hostReconnectIntervalSeconds * 1000, 1000, 4096); // Hook up the disconnected and reconnected events communicationClient.Disconnected += OnClientDisconnected; communicationClient.Reconnected += OnClientReconnected; // Hook up the message receive event communicationClient.MessageReceived += ReceiveMessage; // Add to cache host bucket currentCacheHostBucket.AddCacheHost(communicationClient); // check if done with bucket if (currentCacheHostBucket.Count == hostRedundancyLayers + 1) { _cacheHostBuckets.Add(currentCacheHostBucket); currentCacheHostBucket = new CacheHostBucket(); } } // Final safety check for uneven cache host distributions if (currentCacheHostBucket.Count > 0) { _cacheHostBuckets.Add(currentCacheHostBucket); } _logger.Info("Cache Host Assignment", string.Format("Assigned {0} cache hosts to {1} cache host buckets ({2} per bucket)", cacheHosts.Count, _cacheHostBuckets.Count, hostRedundancyLayers + 1)); // Now attempt to connect to each host foreach (var cacheHostBucket in _cacheHostBuckets) { cacheHostBucket.PerformActionOnAll(c => c.Connect()); } }