Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.3/sources/HeuristicLab.Hive/HeuristicLab.Hive.Server.Console/3.3/JobDataFetcher.cs @ 5153

Last change on this file since 5153 was 4424, checked in by cneumuel, 14 years ago
  • Added and updated License Information in every file
  • Sort and remove usings in every file
  • Deleted obsolete DataAccess.ADOHelper
  • Deleted some obsolete files
File size: 2.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Threading;
5using System.Timers;
6using HeuristicLab.Hive.Contracts;
7using HeuristicLab.Hive.Contracts.BusinessObjects;
8using HeuristicLab.Hive.Contracts.Interfaces;
9using HeuristicLab.Hive.Contracts.ResponseObjects;
10
11namespace HeuristicLab.Hive.Server.ServerConsole {
12  public class JobDataFetcher {
13    private List<JobState> pollStates;
14
15    public IEnumerable<JobState> PollStates {
16      get { return pollStates; }
17    }
18
19    public event EventHandler NewDataAvailable;
20
21    private System.Timers.Timer timer;
22
23    private static object locker = new object();
24
25    public List<JobDto> CachedJobs { get; set; }
26
27    private IJobManager jobManager;
28
29    public double Interval {
30      get { return timer.Interval; }
31      set { timer.Interval = value; }
32    }
33
34    private int _nrOfEntriesOnPage;
35    public int NrOfEntriesOnPage {
36      get {
37        return _nrOfEntriesOnPage;
38      }
39      set {
40        Console.WriteLine("Setting for " + PollStates + " changed to: " + value);
41        _nrOfEntriesOnPage = value;
42        Thread t = new Thread(new ThreadStart(DoUpdateRun));
43        t.Start();
44      }
45    }
46    public int CurrentPage { get; set; }
47
48    public JobDataFetcher(IEnumerable<JobState> states, int nrOfEntries) {
49      pollStates = new List<JobState>();
50      CachedJobs = new List<JobDto>();
51      NrOfEntriesOnPage = nrOfEntries;
52      CurrentPage = 0;
53      pollStates.AddRange(states);
54      jobManager = ServiceLocator.GetJobManager();
55      timer = new System.Timers.Timer();
56      timer.Interval = 5000;
57      timer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
58    }
59
60    public void Start() {
61      timer.Start();
62    }
63
64    public void Forward() {
65      CurrentPage++;
66      DoUpdateRun();
67    }
68    public void Backward() {
69      if (CurrentPage != 0)
70        CurrentPage--;
71      DoUpdateRun();
72    }
73
74    private void Timer_Elapsed(object sender, ElapsedEventArgs e) {
75      DoUpdateRun();
76    }
77
78    private void DoUpdateRun() {
79      JobState[] pollStatesCopy = PollStates.ToArray();
80      for (int i = 0; i < pollStatesCopy.Length; i++) {
81        ResponseList<JobDto> resList = jobManager.GetAllJobsWithFilter(pollStatesCopy[i], CurrentPage * NrOfEntriesOnPage, NrOfEntriesOnPage);
82        if (resList.StatusMessage == ResponseStatus.Ok) {
83          if (resList.List.Count == 0) {
84            if (CurrentPage > 0) {
85              CurrentPage--;
86            }
87          } else {
88            CachedJobs = new List<JobDto>();
89            CachedJobs.AddRange(resList.List);
90          }
91        }
92      }
93      if (NewDataAvailable != null) {
94        NewDataAvailable(this, new EventArgs());
95      }
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.