// Метод, вызываемый каждую секунду. Проверяет, сломается ли ресурс. private void OnTickEvent(Object source, System.Timers.ElapsedEventArgs e) { double randomNumber = Program.rnd.NextDouble(); if (randomNumber <= breakProbability) { timer.Enabled = false; Console.WriteLine("Ресурс " + (Model.vRes_s.IndexOf(this) + 1) + " сломался, время починки = " + repairTimeSec + " секунд"); if (state == RESOURCE_STATE.BUSY) { Program.expirationQueue.Add(new Tuple <int, UserRequest>(vReqQueue[0].maxWaitTimeSec, vReqQueue[0])); Console.WriteLine("Запрос перемещён в очередь."); Program.resortExpirationQueue(true); Program.stopwatch.Start(); } state = RESOURCE_STATE.BROKEN; timer.Elapsed -= OnTickEvent; timer.Elapsed += OnRepaired; stopwatch.Reset(); stopwatch.Start(); timer.Enabled = true; timer.Interval = repairTimeSec * 1000; } }
//Считывает модель из файла private static void GetModel() { Console.WriteLine("Обновить файл?"); if (Console.ReadLine().ToUpper() == "Y") { CreateModel(); } else { using (var reader = new StreamReader(SetUp.Path)) { string line; line = reader.ReadLine(); string[] p = line.Split(new[] { ' ' }); //ExpirationQueue Console.WriteLine("Очередь запросов:"); for (int i = 0; i < Convert.ToInt32(p[0]); i++) { int time = Convert.ToInt32(p[1 + 4 * i]); int id = Convert.ToInt32(p[2 + 4 * i]); int resourceId = Convert.ToInt32(p[3 + 4 * i]); int maxWaitTime = Convert.ToInt32(p[4 + 4 * i]); UserRequest request = new UserRequest(resourceId, maxWaitTime); Program.expirationQueue.Add(new Tuple <int, UserRequest>(time, request)); Console.WriteLine("Запрос id = " + id + "; resourceId = " + resourceId + "; maxWaitTime = " + maxWaitTime + "; remainingTime = " + time); } while ((line = reader.ReadLine()) != null) //Ресурсы { string[] parts = line.Split(new[] { ' ' }); RESOURCE_STATE state = (RESOURCE_STATE)Enum.Parse(typeof(RESOURCE_STATE), parts[0], true); double breakProbability = Convert.ToDouble(parts[1]); int repairTime = Convert.ToInt32(parts[2]); List <UserRequest> queue = new List <UserRequest>(); int c = Convert.ToInt32(parts[3]); for (int j = 0; j < c; j++) { int id = Convert.ToInt32(parts[4 + 3 * j]); int resourceId = Convert.ToInt32(parts[5 + 3 * j]); int maxWaitTime = Convert.ToInt32(parts[6 + 3 * j]); UserRequest request = new UserRequest(resourceId, maxWaitTime); queue.Add(request); } Resource r = new Resource(state, breakProbability, repairTime, queue); Model.vRes_s.Add(r); } } } }
// Метод, вызываемый, когда сломанный ресурс починен. private void OnRepaired(Object source, System.Timers.ElapsedEventArgs e) { stopwatch.Stop(); stopwatch.Reset(); timer.Enabled = false; Console.WriteLine("Ресурс " + (Model.vRes_s.IndexOf(this) + 1) + " починен"); state = RESOURCE_STATE.FREE; if (vReqQueue.Count != 0) { Console.WriteLine("Запрос занял ресурс " + (Model.vRes_s.IndexOf(this) + 1)); state = RESOURCE_STATE.BUSY; Program.deleteRequestFromExpirationQueue(vReqQueue[0]); Program.resortExpirationQueue(false); Program.stopwatch.Start(); } timer.Elapsed -= OnRepaired; timer.Elapsed += OnTickEvent; timer.Enabled = true; timer.Interval = 1000; }
public Stopwatch stopwatch; //Нужно для корректной паузы таймера public Resource(RESOURCE_STATE curState, double breakProbability, int repairTimeSec, List <UserRequest> vReqQueue) { state = curState; this.breakProbability = breakProbability; this.repairTimeSec = repairTimeSec; this.vReqQueue = vReqQueue; timer = new System.Timers.Timer(); //TODO: проверить логику if (state == RESOURCE_STATE.FREE || state == RESOURCE_STATE.BUSY) //Если ресурс не сломан { timer.Interval = 1000; //Запустить ежесекундный таймер (для проверки, сломается ли ресурс) timer.Elapsed += OnTickEvent; } else //Если ресурс сломан { timer.Interval = repairTimeSec * 1000; timer.Elapsed += OnRepaired; } timer.AutoReset = true; timer.Enabled = true; //Запускаем таймер stopwatch = new Stopwatch(); }