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 | private List<State> pollStates;
|
---|
14 |
|
---|
15 | public IEnumerable<State> 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 | public double Interval {
|
---|
28 | get { return timer.Interval; }
|
---|
29 | set { timer.Interval = value; }
|
---|
30 | }
|
---|
31 |
|
---|
32 | private int _nrOfEntriesOnPage;
|
---|
33 | public int NrOfEntriesOnPage {
|
---|
34 | get {
|
---|
35 | return _nrOfEntriesOnPage;
|
---|
36 | }
|
---|
37 | set {
|
---|
38 | Console.WriteLine("Setting for " + PollStates + " changed to: " + value);
|
---|
39 | _nrOfEntriesOnPage = value;
|
---|
40 | Thread t = new Thread(new ThreadStart(DoUpdateRun));
|
---|
41 | t.Start();
|
---|
42 | }
|
---|
43 | }
|
---|
44 | public int CurrentPage { get; set; }
|
---|
45 |
|
---|
46 | public JobDataFetcher(IEnumerable<State> states, int nrOfEntries) {
|
---|
47 | pollStates = new List<State>();
|
---|
48 | CachedJobs = new List<JobDto>();
|
---|
49 | NrOfEntriesOnPage = nrOfEntries;
|
---|
50 | CurrentPage = 0;
|
---|
51 | pollStates.AddRange(states);
|
---|
52 | timer = new System.Timers.Timer();
|
---|
53 | timer.Interval = 5000;
|
---|
54 | timer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
|
---|
55 | }
|
---|
56 |
|
---|
57 | public void Start() {
|
---|
58 | timer.Start();
|
---|
59 | }
|
---|
60 |
|
---|
61 | public void Forward() {
|
---|
62 | CurrentPage++;
|
---|
63 | DoUpdateRun();
|
---|
64 | }
|
---|
65 | public void Backward() {
|
---|
66 | if (CurrentPage != 0)
|
---|
67 | CurrentPage--;
|
---|
68 | DoUpdateRun();
|
---|
69 | }
|
---|
70 |
|
---|
71 | private void Timer_Elapsed(object sender, ElapsedEventArgs e) {
|
---|
72 | DoUpdateRun();
|
---|
73 | }
|
---|
74 |
|
---|
75 | private void DoUpdateRun() {
|
---|
76 | // [chn] Exception occured here: PollStates enumeration changed!
|
---|
77 | // todo: make access to PollStates thread-safe!
|
---|
78 | // * added lock on locker -> didn't help
|
---|
79 | // * made public property PollStates an IEnumerable to be immutable -> lets see
|
---|
80 | lock (locker) {
|
---|
81 | foreach (State stat in PollStates) {
|
---|
82 | ResponseList<JobDto> resList = ServiceLocator.GetJobManager().GetAllJobsWithFilter(stat, CurrentPage * NrOfEntriesOnPage,
|
---|
83 | NrOfEntriesOnPage);
|
---|
84 | if (resList.Success) {
|
---|
85 | if (resList.List.Count == 0) {
|
---|
86 | if (CurrentPage > 0) {
|
---|
87 | CurrentPage--;
|
---|
88 | }
|
---|
89 | } else {
|
---|
90 | CachedJobs = new List<JobDto>();
|
---|
91 | CachedJobs.AddRange(resList.List);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|
95 | if (NewDataAvailable != null)
|
---|
96 | NewDataAvailable(this, new EventArgs());
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|
100 | } |
---|