GridComputing
Job Management in Grid Computing
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
gridmanager.h
Go to the documentation of this file.
1 #ifndef GRIDMANAGER_H_
2 #define GRIDMANAGER_H_
3 
4 #include "utils.h"
5 #include "interfaces.h"
6 #include "machine.h"
7 #include "idleuser.h"
8 
9 
10 #include <map>
11 #include <vector>
12 #include <functional>
13 #include <set>
14 
15 class Menu;
16 class User;
17 class Job;
18 
20 
24 class GridManager : public ISave, public IUpdate
25 {
26 public:
27  typedef std::set<User*, IdLess<User>> UserSet;
28  typedef std::set<Machine*, IdLess<Machine>> MachineSet;
29  typedef std::set<PriorityMachine*, IdLess<PriorityMachine>> PriorityMachineSet;
30 
33 
35  ~GridManager();
36 
42  uint AddUser(User* user);
43 
49  uint AddMachine(Machine* machine);
50 
57 
63  bool RemoveUser(const User* user);
64 
70  bool RemoveUser(uint id);
71 
77  bool RemoveMachine(const Machine* machine);
78 
84  bool RemoveMachine(uint id);
85 
91  bool RemovePriorityMachine(const PriorityMachine* machine);
92 
98  bool RemovePriorityMachine(uint id);
99 
105  User* GetUser(uint id) const;
106 
111  uint GetNumberOfUsers() const { return _users.size(); }
112 
118  Machine* GetMachine(uint id) const;
119 
126 
131  uint GetNumberOfMachines() const { return _machines.size(); }
132 
138 
144  bool Save(ByteBuffer& bb) const override;
145 
150  void Update(uint32 diff);
151 
157  static GridManager* Load(ByteBuffer& bb);
158 
165  bool AddJobByUser(int userId, Job* job) { return AddJobByUser(GetUser(userId), job); }
166 
173  bool AddJobByUser(User* user, Job* job);
174 
181  bool RemoveMachineJob(Machine* machine, uint jobId);
182 
187  uint GetNumberOfJobs() const;
188 
195  template<class T>
196  std::vector<const T*> ApplyPredicate(std::function<bool(const T*)> predicate) const;
197 
205  template<class T, class R>
206  std::vector<R> ApplySelector(std::function<R(const T*)> selector, std::function<bool(const T*)> predicate) const;
207 
214  template<class T, class R>
215  std::vector<R> ApplySelector(std::function<R(const T*)> selector) const;
216 
217  static Menu* GetMenu() { return _menu; }
218 
219  const IdleUserContainer& GetIdleUsers() const { return _idleUsers; }
220 
221 private:
227  bool AddJob(Job* job);
228 
233 
241  std::queue<Job*> _waitingJobs;
242 
243 
244  static Menu* _menu;
245 
247 
248 private: // no copying
250  GridManager(const GridManager &);
253 };
254 
255 template<class T, class R>
256 std::vector<R> GridManager::ApplySelector(std::function<R(const T*)> selector) const
257 {
258  return ApplySelector<T, R>(selector, [](const T*) { return true; });
259 }
260 
261 template<class T, class R>
262 std::vector<R> GridManager::ApplySelector(std::function<R(const T*)> selector, std::function<bool(const T*)> predicate) const
263 {
264  // get all machines, jobs, etc. that match the predicate
265  std::vector<const T*> ts = ApplyPredicate<T>(predicate);
266 
267  // apply our selector to the initial vector to create a new one
268  std::vector<R> result;
269  result.reserve(ts.size()); // max size of the resulting vector
270 
271  std::transform(ts.begin(), ts.end(), std::back_inserter(result), selector);
272 
273  return result;
274 }
275 
276 #endif // GRIDMANAGER_H_