/// <summary> /// Constructs a new <see cref="ConsensusServer"/>. /// </summary> /// <param name="configuration">The <see cref="IServerConfiguration"/> of this <see cref="ConsensusServer"/>.</param> /// <param name="log">The <see cref="ILog"/> used by this <see cref="ConsensusServer"/> to store entries in.</param> /// <param name="protocol">The <see cref="IProtocol"/> using by this <see cref="ConsensusServer"/> to communicate with other <see cref="ConsensusServer"/>s in the same cluster.</param> /// <param name="scheduler">The <see cref="IResourceTrackingScheduler"/> on which to execute callbacks.</param> /// <exception cref="ArgumentNullException">Thrown if one of the parameters is null.</exception> public ConsensusServer(IServerConfiguration configuration, ILog log, IProtocol protocol, IResourceTrackingScheduler scheduler) { // validate arguments if (configuration == null) { throw new ArgumentNullException("configuration"); } if (scheduler == null) { throw new ArgumentNullException("scheduler"); } if (log == null) { throw new ArgumentNullException("log"); } if (protocol == null) { throw new ArgumentNullException("protocol"); } // store the arguments this.configuration = configuration; this.scheduler = scheduler; this.log = log; this.protocol = protocol; // create a new state state = new ConsensusServerState(this); }
/// <summary> /// Invokes the <paramref name="callback"/> after every elapsed <paramref name="timeout"/>. /// </summary> /// <param name="resouceResourceTrackingScheduler">The <see cref="IResourceTrackingScheduler"/> on which the timeout event will be executed.</param> /// <param name="callback">The callback which to invoke on timeout.</param> /// <param name="timeout">The timeout which to wait before <paramref name="callback"/> should be executed.</param> /// <returns>Returns a <see cref="ITimer"/> which controls the timer.</returns> /// <exception cref="ArgumentNullException">Thrown if one of the parameters is null.</exception> public static ITimer SetInterval(this IResourceTrackingScheduler resouceResourceTrackingScheduler, Action callback, TimeSpan timeout) { // validate arguments if (resouceResourceTrackingScheduler == null) { throw new ArgumentNullException("resouceResourceTrackingScheduler"); } if (callback == null) { throw new ArgumentNullException("callback"); } // create the timer which will schedule the callback var timer = new TimerControl(timeout, timeout); var token = timer.Token; // create a resource managed by the event loop var untrack = resouceResourceTrackingScheduler.TrackResource(token, timer); // dispose the timer if cancelled token.Register(() => { // untrack the resource untrack(token); // dispose the timer timer.Dispose(); }); // set the timer timer.Set(() => resouceResourceTrackingScheduler.Schedule(callback)); // return the timer return(timer); }
/// <summary> /// Constructs a new <see cref="ConsensusServer"/>. /// </summary> /// <param name="configuration">The <see cref="IServerConfiguration"/> of this <see cref="ConsensusServer"/>.</param> /// <param name="log">The <see cref="ILog"/> used by this <see cref="ConsensusServer"/> to store entries in.</param> /// <param name="protocol">The <see cref="IProtocol"/> using by this <see cref="ConsensusServer"/> to communicate with other <see cref="ConsensusServer"/>s in the same cluster.</param> /// <param name="scheduler">The <see cref="IResourceTrackingScheduler"/> on which to execute callbacks.</param> /// <exception cref="ArgumentNullException">Thrown if one of the parameters is null.</exception> public ConsensusServer(IServerConfiguration configuration, ILog log, IProtocol protocol, IResourceTrackingScheduler scheduler) { // validate arguments if (configuration == null) throw new ArgumentNullException("configuration"); if (scheduler == null) throw new ArgumentNullException("scheduler"); if (log == null) throw new ArgumentNullException("log"); if (protocol == null) throw new ArgumentNullException("protocol"); // store the arguments this.configuration = configuration; this.scheduler = scheduler; this.log = log; this.protocol = protocol; // create a new state state = new ConsensusServerState(this); }