/// <summary> /// Gets the next value in queue. /// </summary> /// <param name="currentValue">The current value.</param> /// <returns>NextValueResponse</returns> private NextValueResponse GetNextValueInQueue(float currentValue) { NextValueResponse nvr = new NextValueResponse(); nvr.NextValue = currentValue; if (this.nodeData == null) { return(nvr); } for (int i = 0; i < this.nodeData.Count; i++) { if (this.nodeData[i] == currentValue) { if ((i + 1) != this.nodeData.Count) { nvr.NextValue = this.nodeData[i + 1]; return(nvr); } } } return(nvr); }
/// <summary> /// Starts the thread. /// </summary> private void StartThread() { this.logger.Info($"Node_{this.id} Starting"); if (!this.LoadNodeData()) { this.Dispose(); } while (!this.token.IsCancellationRequested) { try { ICommand command = null; if (this.commandQueue.TryDequeue(out command)) { switch (command.CommandType) { case Enums.CommandType.Sort_Elements: this.logger.Debug("Sort Elements Request"); this.nodeData?.Sort(); // TODO space complexity break; case Enums.CommandType.Display_Elements: this.logger.Debug("Sort Elements Request"); this.logger.Debug(this.nodeData); break; case Enums.CommandType.Initial_Stat_Req: this.logger.Debug("Publish Initial Stats Request"); command.Sender.QueueCommand(new StatResponse() { Sender = this, CommandType = Enums.CommandType.Initial_Stat_Response, Size = this.nodeData?.Count ?? 0, Min = this.nodeData?[0] ?? 0, Max = this.nodeData?[this.nodeData.Count - 1] ?? 0, }); break; case Enums.CommandType.Publish_Stats: StatRequest sr = (StatRequest)command; StatResponse sresp = this.GetStatsForSpecificRange(sr.Min, sr.Max); command.Sender.QueueCommand(sresp); break; case Enums.CommandType.Publish_NextValue: NextValueRequest nvr = (NextValueRequest)command; NextValueResponse nxtValResp = this.GetNextValueInQueue(nvr.CurrentValue); command.Sender.QueueCommand(nxtValResp); break; default: this.logger.Debug("Unhandled Command. Ignoring"); break; } } } catch (Exception ex) { this.logger.Fatal("Exception Occured"); this.logger.Fatal($"Exception Data - {ex.Message}"); this.logger.Fatal($"Inner Exception Data - {ex.InnerException?.Message}"); this.logger.Fatal($"Stack Trace - {ex.StackTrace}"); } } }