1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using System.Threading;
|
---|
26 | using System.ServiceModel;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Grid {
|
---|
29 | [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext=false)]
|
---|
30 | public class EngineStore : IEngineStore {
|
---|
31 | private Queue<Guid> engineQueue;
|
---|
32 | private Dictionary<Guid, byte[]> waitingEngines;
|
---|
33 | private Dictionary<Guid, byte[]> runningEngines;
|
---|
34 | private Dictionary<Guid, byte[]> results;
|
---|
35 | private object bigLock;
|
---|
36 |
|
---|
37 | private event EventHandler ResultRecieved;
|
---|
38 |
|
---|
39 | public int WaitingJobs {
|
---|
40 | get {
|
---|
41 | return waitingEngines.Count;
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public int RunningJobs {
|
---|
46 | get {
|
---|
47 | return runningEngines.Count;
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | public int WaitingResults {
|
---|
52 | get {
|
---|
53 | return results.Count;
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | public EngineStore() {
|
---|
58 | engineQueue = new Queue<Guid>();
|
---|
59 | waitingEngines = new Dictionary<Guid, byte[]>();
|
---|
60 | runningEngines = new Dictionary<Guid, byte[]>();
|
---|
61 | results = new Dictionary<Guid, byte[]>();
|
---|
62 | bigLock = new object();
|
---|
63 | }
|
---|
64 |
|
---|
65 | public bool TryTakeEngine(out Guid guid, out byte[] engine) {
|
---|
66 | lock (bigLock) {
|
---|
67 | if (engineQueue.Count == 0) {
|
---|
68 | guid = Guid.Empty;
|
---|
69 | engine = null;
|
---|
70 | return false;
|
---|
71 | } else {
|
---|
72 | guid = engineQueue.Dequeue();
|
---|
73 | engine = waitingEngines[guid];
|
---|
74 | waitingEngines.Remove(guid);
|
---|
75 | runningEngines[guid] = engine;
|
---|
76 | return true;
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | public void StoreResult(Guid guid, byte[] result) {
|
---|
82 | lock (bigLock) {
|
---|
83 | if (!runningEngines.ContainsKey(guid)) return; // ignore result when the engine is not known to be running
|
---|
84 |
|
---|
85 | runningEngines.Remove(guid);
|
---|
86 | results[guid] = result;
|
---|
87 | OnResultRecieved(guid);
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | internal void AddEngine(Guid guid, byte[] engine) {
|
---|
92 | lock (bigLock) {
|
---|
93 | engineQueue.Enqueue(guid);
|
---|
94 | waitingEngines.Add(guid, engine);
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | internal byte[] RemoveResult(Guid guid) {
|
---|
99 | lock (bigLock) {
|
---|
100 | byte[] result = results[guid];
|
---|
101 | results.Remove(guid);
|
---|
102 | return result;
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | internal byte[] GetResult(Guid guid) {
|
---|
107 | ManualResetEvent waitHandle = new ManualResetEvent(false);
|
---|
108 | lock (bigLock) {
|
---|
109 | if (results.ContainsKey(guid)) {
|
---|
110 | byte[] result = results[guid];
|
---|
111 | results.Remove(guid);
|
---|
112 | return result;
|
---|
113 | } else {
|
---|
114 | ResultRecieved += delegate(object source, EventArgs args) {
|
---|
115 | ResultRecievedEventArgs resultArgs = (ResultRecievedEventArgs)args;
|
---|
116 | if (resultArgs.resultGuid == guid) {
|
---|
117 | waitHandle.Set();
|
---|
118 | }
|
---|
119 | };
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | waitHandle.WaitOne();
|
---|
124 | waitHandle.Close();
|
---|
125 |
|
---|
126 | lock (bigLock) {
|
---|
127 | byte[] result = results[guid];
|
---|
128 | results.Remove(guid);
|
---|
129 | return result;
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | private void OnResultRecieved(Guid guid) {
|
---|
134 | ResultRecievedEventArgs args = new ResultRecievedEventArgs();
|
---|
135 | args.resultGuid = guid;
|
---|
136 | if (ResultRecieved != null) {
|
---|
137 | ResultRecieved(this, args);
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | private class ResultRecievedEventArgs : EventArgs {
|
---|
142 | public Guid resultGuid;
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|