LdClient(Configuration configuration, User user) { Config = configuration; connectionLock = new SemaphoreSlim(1, 1); persister = Factory.CreatePersister(configuration); deviceInfo = Factory.CreateDeviceInfo(configuration); flagListenerManager = Factory.CreateFeatureFlagListenerManager(configuration); // If you pass in a null user or user with a null key, one will be assigned to them. if (user == null || user.Key == null) { User = UserWithUniqueKey(user); } else { User = user; } flagCacheManager = Factory.CreateFlagCacheManager(configuration, persister, flagListenerManager, User); connectionManager = Factory.CreateConnectionManager(configuration); updateProcessor = Factory.CreateUpdateProcessor(configuration, User, flagCacheManager); eventProcessor = Factory.CreateEventProcessor(configuration); SetupConnectionManager(); }
internal static IMobileUpdateProcessor CreateUpdateProcessor(Configuration configuration, User user, IFlagCacheManager flagCacheManager, StreamManager.EventSourceCreator source = null) { if (configuration.MobileUpdateProcessor != null) { return(configuration.MobileUpdateProcessor); } IMobileUpdateProcessor updateProcessor = null; if (configuration.Offline) { Log.InfoFormat("Was configured to be offline, starting service with NullUpdateProcessor"); return(new NullUpdateProcessor()); } if (configuration.IsStreamingEnabled) { updateProcessor = new MobileStreamingProcessor(configuration, flagCacheManager, user, source); } else { var featureFlagRequestor = new FeatureFlagRequestor(configuration, user); updateProcessor = new MobilePollingProcessor(featureFlagRequestor, flagCacheManager, user, configuration.PollingInterval); } return(updateProcessor); }
void ClearUpdateProcessor() { if (updateProcessor != null) { updateProcessor.Dispose(); updateProcessor = null; } }
public void Dispose() { IMobileUpdateProcessor processor = null; LockUtils.WithWriteLock(_lock, () => { if (_disposed) { return; } processor = _updateProcessor; _updateProcessor = null; _updateProcessorFactory = null; _disposed = true; }); processor?.Dispose(); }
/// <summary> /// Sets the factory function for creating an update processor, and attempts to connect if /// appropriate. /// </summary> /// <remarks> /// The factory function encapsulates all the information that <see cref="LdClient"/> takes into /// account when making a connection, i.e. whether we are in streaming or polling mode, the /// polling interval, and the curent user. <c>ConnectionManager</c> itself has no knowledge of /// those things. /// /// Besides updating the private factory function field, we do the following: /// /// If the function is null, we drop our current connection (if any), and we will not make /// any connections no matter what other properties are changed as long as it is still null. /// /// If it is non-null and we already have the same factory function, nothing happens. /// /// If it is non-null and we do not already have the same factory function, but other conditions /// disallow making a connection, nothing happens. /// /// If it is non-null and we do not already have the same factory function, and no other /// conditions disallow making a connection, we create an update processor and tell it to start. /// In this case, we also reset <see cref="Initialized"/> to false if <c>resetInitialized</c> is /// true. /// /// The returned task is immediately completed unless we are making a new connection, in which /// case it is completed when the update processor signals success or failure. The task yields /// a true result if we successfully made a connection <i>or</i> if we decided not to connect /// because we are in offline mode. In other words, the result is true if /// <see cref="Initialized"/> is true. /// </remarks> /// <param name="updateProcessorFactory">a factory function or null</param> /// <param name="resetInitialized">true if we should reset the initialized state (e.g. if we /// are switching users</param> /// <returns>a task as described above</returns> public Task <bool> SetUpdateProcessorFactory(Func <IMobileUpdateProcessor> updateProcessorFactory, bool resetInitialized) { return(LockUtils.WithWriteLock(_lock, () => { if (_disposed || _updateProcessorFactory == updateProcessorFactory) { return Task.FromResult(false); } _updateProcessorFactory = updateProcessorFactory; _updateProcessor?.Dispose(); _updateProcessor = null; if (resetInitialized) { _initialized = false; } return OpenOrCloseConnectionIfNecessary(); // not awaiting })); }
LdClient(Configuration configuration, User user) { if (configuration == null) { throw new ArgumentNullException("configuration"); } if (user == null) { throw new ArgumentNullException("user"); } Config = configuration; connectionLock = new SemaphoreSlim(1, 1); persister = Factory.CreatePersister(configuration); deviceInfo = Factory.CreateDeviceInfo(configuration); flagListenerManager = Factory.CreateFeatureFlagListenerManager(configuration); platformAdapter = Factory.CreatePlatformAdapter(configuration); // If you pass in a user with a null or blank key, one will be assigned to them. if (String.IsNullOrEmpty(user.Key)) { User = UserWithUniqueKey(user); } else { User = user; } flagCacheManager = Factory.CreateFlagCacheManager(configuration, persister, flagListenerManager, User); connectionManager = Factory.CreateConnectionManager(configuration); updateProcessor = Factory.CreateUpdateProcessor(configuration, User, flagCacheManager); eventProcessor = Factory.CreateEventProcessor(configuration); eventProcessor.SendEvent(eventFactory.NewIdentifyEvent(User)); SetupConnectionManager(); }
// This method is called while _lock is being held. If we're starting up a new connection, we do // *not* wait for it to succeed; we return a Task that will be completed once it succeeds. In all // other cases we return an immediately-completed Task. private Task <bool> OpenOrCloseConnectionIfNecessary() { if (!_started) { return(Task.FromResult(false)); } if (_networkEnabled && !_forceOffline) { if (_updateProcessor == null && _updateProcessorFactory != null) { _updateProcessor = _updateProcessorFactory(); return(_updateProcessor.Start() .ContinueWith(SetInitializedIfUpdateProcessorStartedSuccessfully)); } } else { _updateProcessor?.Dispose(); _updateProcessor = null; _initialized = true; return(Task.FromResult(true)); } return(Task.FromResult(false)); }
/// <summary> /// Sets the IMobileUpdateProcessor instance, used internally for stubbing mock instances. /// </summary> /// <param name="configuration">Configuration.</param> /// <param name="mobileUpdateProcessor">Mobile update processor.</param> /// <returns>the same <c>Configuration</c> instance</returns> internal static Configuration WithUpdateProcessor(this Configuration configuration, IMobileUpdateProcessor mobileUpdateProcessor) { configuration.MobileUpdateProcessor = mobileUpdateProcessor; return(configuration); }
void ClearAndSetUpdateProcessor() { ClearUpdateProcessor(); updateProcessor = Factory.CreateUpdateProcessor(Config, User, flagCacheManager); }