1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
6 | using System.Timers;
|
---|
7 | using HeuristicLab.Hive.Contracts;
|
---|
8 | using System.Collections;
|
---|
9 | using System.Threading;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.Hive.Server.ServerConsole {
|
---|
12 | public class JobDataFetcher {
|
---|
13 | public List<State> PollStates { get; set; }
|
---|
14 |
|
---|
15 | public event EventHandler NewDataAvailable;
|
---|
16 |
|
---|
17 | private System.Timers.Timer timer;
|
---|
18 |
|
---|
19 | public List<JobDto> CachedJobs { get; set; }
|
---|
20 |
|
---|
21 | public double Interval {
|
---|
22 | get { return timer.Interval; }
|
---|
23 | set { timer.Interval = value; }
|
---|
24 | }
|
---|
25 |
|
---|
26 | private int _nrOfEntriesOnPage;
|
---|
27 | public int NrOfEntriesOnPage {
|
---|
28 | get {
|
---|
29 | return _nrOfEntriesOnPage;
|
---|
30 | }
|
---|
31 | set {
|
---|
32 | Console.WriteLine("Setting for " + PollStates + " changed to: " + value);
|
---|
33 | _nrOfEntriesOnPage = value;
|
---|
34 | Thread t = new Thread(new ThreadStart(DoUpdateRun));
|
---|
35 | t.Start();
|
---|
36 | }
|
---|
37 | }
|
---|
38 | public int CurrentPage { get; set; }
|
---|
39 |
|
---|
40 | public JobDataFetcher(IEnumerable<State> states, int nrOfEntries) {
|
---|
41 | PollStates = new List<State>();
|
---|
42 | CachedJobs = new List<JobDto>();
|
---|
43 | NrOfEntriesOnPage = nrOfEntries;
|
---|
44 | CurrentPage = 0;
|
---|
45 | PollStates.AddRange(states);
|
---|
46 | timer = new System.Timers.Timer();
|
---|
47 | timer.Interval = 5000;
|
---|
48 | timer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
|
---|
49 | }
|
---|
50 |
|
---|
51 | public void Start() {
|
---|
52 | timer.Start();
|
---|
53 | }
|
---|
54 |
|
---|
55 | public void Forward() {
|
---|
56 | CurrentPage++;
|
---|
57 | DoUpdateRun();
|
---|
58 | }
|
---|
59 | public void Backward() {
|
---|
60 | if (CurrentPage != 0)
|
---|
61 | CurrentPage--;
|
---|
62 | DoUpdateRun();
|
---|
63 | }
|
---|
64 |
|
---|
65 | private void Timer_Elapsed(object sender, ElapsedEventArgs e) {
|
---|
66 | DoUpdateRun();
|
---|
67 | }
|
---|
68 |
|
---|
69 | private void DoUpdateRun() {
|
---|
70 | foreach (State stat in PollStates) {
|
---|
71 | ResponseList<JobDto> resList = ServiceLocator.GetJobManager().GetAllJobsWithFilter(stat, CurrentPage * NrOfEntriesOnPage,
|
---|
72 | NrOfEntriesOnPage);
|
---|
73 | if (resList.Success) {
|
---|
74 | if (resList.List.Count == 0) {
|
---|
75 | if (CurrentPage > 0) {
|
---|
76 | CurrentPage--;
|
---|
77 | }
|
---|
78 | } else {
|
---|
79 | CachedJobs = new List<JobDto>();
|
---|
80 | CachedJobs.AddRange(resList.List);
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 | if (NewDataAvailable != null)
|
---|
85 | NewDataAvailable(this, new EventArgs());
|
---|
86 | }
|
---|
87 | }
|
---|
88 | } |
---|