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 | using HeuristicLab.Hive.Contracts.Interfaces;
|
---|
11 | using HeuristicLab.Hive.Contracts.ResponseObjects;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.Hive.Server.ServerConsole {
|
---|
14 | public class JobDataFetcher {
|
---|
15 | private List<JobState> pollStates;
|
---|
16 |
|
---|
17 | public IEnumerable<JobState> PollStates {
|
---|
18 | get { return pollStates; }
|
---|
19 | }
|
---|
20 |
|
---|
21 | public event EventHandler NewDataAvailable;
|
---|
22 |
|
---|
23 | private System.Timers.Timer timer;
|
---|
24 |
|
---|
25 | private static object locker = new object();
|
---|
26 |
|
---|
27 | public List<JobDto> CachedJobs { get; set; }
|
---|
28 |
|
---|
29 | private IJobManager jobManager;
|
---|
30 |
|
---|
31 | public double Interval {
|
---|
32 | get { return timer.Interval; }
|
---|
33 | set { timer.Interval = value; }
|
---|
34 | }
|
---|
35 |
|
---|
36 | private int _nrOfEntriesOnPage;
|
---|
37 | public int NrOfEntriesOnPage {
|
---|
38 | get {
|
---|
39 | return _nrOfEntriesOnPage;
|
---|
40 | }
|
---|
41 | set {
|
---|
42 | Console.WriteLine("Setting for " + PollStates + " changed to: " + value);
|
---|
43 | _nrOfEntriesOnPage = value;
|
---|
44 | Thread t = new Thread(new ThreadStart(DoUpdateRun));
|
---|
45 | t.Start();
|
---|
46 | }
|
---|
47 | }
|
---|
48 | public int CurrentPage { get; set; }
|
---|
49 |
|
---|
50 | public JobDataFetcher(IEnumerable<JobState> states, int nrOfEntries) {
|
---|
51 | pollStates = new List<JobState>();
|
---|
52 | CachedJobs = new List<JobDto>();
|
---|
53 | NrOfEntriesOnPage = nrOfEntries;
|
---|
54 | CurrentPage = 0;
|
---|
55 | pollStates.AddRange(states);
|
---|
56 | jobManager = ServiceLocator.GetJobManager();
|
---|
57 | timer = new System.Timers.Timer();
|
---|
58 | timer.Interval = 5000;
|
---|
59 | timer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
|
---|
60 | }
|
---|
61 |
|
---|
62 | public void Start() {
|
---|
63 | timer.Start();
|
---|
64 | }
|
---|
65 |
|
---|
66 | public void Forward() {
|
---|
67 | CurrentPage++;
|
---|
68 | DoUpdateRun();
|
---|
69 | }
|
---|
70 | public void Backward() {
|
---|
71 | if (CurrentPage != 0)
|
---|
72 | CurrentPage--;
|
---|
73 | DoUpdateRun();
|
---|
74 | }
|
---|
75 |
|
---|
76 | private void Timer_Elapsed(object sender, ElapsedEventArgs e) {
|
---|
77 | DoUpdateRun();
|
---|
78 | }
|
---|
79 |
|
---|
80 | private void DoUpdateRun() {
|
---|
81 | // [chn] Exception occured here: PollStates enumeration changed!
|
---|
82 | // todo: make access to PollStates thread-safe!
|
---|
83 | // * added lock on locker -> didn't help
|
---|
84 | // * made public property PollStates an IEnumerable to be immutable -> didn't help
|
---|
85 | // * using for loop
|
---|
86 |
|
---|
87 | JobState[] pollStatesCopy = PollStates.ToArray();
|
---|
88 | for (int i = 0; i < pollStatesCopy.Length; i++) {
|
---|
89 | ResponseList<JobDto> resList = jobManager.GetAllJobsWithFilter(pollStatesCopy[i], CurrentPage * NrOfEntriesOnPage, NrOfEntriesOnPage);
|
---|
90 | if (resList.StatusMessage == ResponseStatus.Ok) {
|
---|
91 | if (resList.List.Count == 0) {
|
---|
92 | if (CurrentPage > 0) {
|
---|
93 | CurrentPage--;
|
---|
94 | }
|
---|
95 | } else {
|
---|
96 | CachedJobs = new List<JobDto>();
|
---|
97 | CachedJobs.AddRange(resList.List);
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|
101 | if (NewDataAvailable != null) {
|
---|
102 | NewDataAvailable(this, new EventArgs());
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|
106 | } |
---|