#region License Information /* HeuristicLab * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Threading; using HeuristicLab.Common; using HeuristicLab.Hive.Contracts; using HeuristicLab.Hive.Contracts.BusinessObjects; using HeuristicLab.Hive.Contracts.Interfaces; using HeuristicLab.Hive.Contracts.ResponseObjects; using HeuristicLab.Clients.Common; namespace HeuristicLab.Hive.ExperimentManager { internal class JobResultPoller { private bool stopRequested { get; set; } private AutoResetEvent waitHandle; private Thread thread; private HiveJob hiveJob; public HiveJob HiveJob { get { return hiveJob; } set { hiveJob = value; } } private TimeSpan interval; public TimeSpan Interval { get { return interval; } set { interval = value; } } private bool isPolling; public bool IsPolling { get { return isPolling; } set { if (isPolling != value) { isPolling = value; OnIsPollingChanged(); } } } public JobResultPoller(HiveJob hiveJob, TimeSpan interval) { this.isPolling = false; this.hiveJob = hiveJob; this.interval = interval; } public void Start() { stopRequested = false; thread = new Thread(RunPolling); thread.Start(); IsPolling = true; } public void Stop() { // use AutoResetEvent.Set instead if Thread.Interrupt because its much cleaner stopRequested = true; waitHandle.Set(); IsPolling = false; thread = null; } public void RunPolling() { try { waitHandle = new AutoResetEvent(false); while (!stopRequested) { OnPollingStarted(); FetchJobResults(); OnPollingFinished(); waitHandle.WaitOne(Interval); } waitHandle.Close(); } catch (Exception e) { OnExceptionOccured(e); IsPolling = false; } } private void FetchJobResults() { int repetitions = 5; ResponseObject response = null; while (response == null && repetitions > 0) { repetitions--; try { using (Disposable service = ServiceLocator.Instance.StreamedClientFacadePool.GetService()) { response = service.Obj.GetChildJobResults(hiveJob.JobDto.Id, true, true); } } catch (Exception e) { if (repetitions == 0) throw e; } } if (response.StatusMessage == ResponseStatus.Ok) { OnJobResultsReceived(response.Obj); } else { throw new JobResultPollingException(response.StatusMessage.ToString()); } } public event EventHandler> JobResultsReceived; private void OnJobResultsReceived(JobResultList jobResults) { var handler = JobResultsReceived; if (handler != null) handler(this, new EventArgs(jobResults)); } public event EventHandler> ExceptionOccured; private void OnExceptionOccured(Exception e) { var handler = ExceptionOccured; if (handler != null) handler(this, new EventArgs(e)); } public event EventHandler IsPollingChanged; private void OnIsPollingChanged() { var handler = IsPollingChanged; if (handler != null) handler(this, EventArgs.Empty); } public event EventHandler PollingStarted; private void OnPollingStarted() { var handler = PollingStarted; if (handler != null) handler(this, EventArgs.Empty); } public event EventHandler PollingFinished; private void OnPollingFinished() { var handler = PollingFinished; if (handler != null) handler(this, EventArgs.Empty); } } }