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.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Hive.Server.Core.InternalInterfaces.DataAccess;
|
---|
27 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
28 | using System.Linq.Expressions;
|
---|
29 | using System.Runtime.CompilerServices;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Hive.Server.ADODataAccess {
|
---|
32 | class ClientAdapter: DataAdapterBase, IClientAdapter {
|
---|
33 | private dsHiveServerTableAdapters.ClientTableAdapter adapter =
|
---|
34 | new dsHiveServerTableAdapters.ClientTableAdapter();
|
---|
35 |
|
---|
36 | private dsHiveServer.ClientDataTable data =
|
---|
37 | new dsHiveServer.ClientDataTable();
|
---|
38 |
|
---|
39 | private IResourceAdapter resAdapter = null;
|
---|
40 |
|
---|
41 | private IResourceAdapter ResAdapter {
|
---|
42 | get {
|
---|
43 | if (resAdapter == null)
|
---|
44 | resAdapter = ServiceLocator.GetResourceAdapter();
|
---|
45 |
|
---|
46 | return resAdapter;
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | private IClientGroupAdapter clientGroupAdapter = null;
|
---|
51 |
|
---|
52 | private IClientGroupAdapter ClientGroupAdapter {
|
---|
53 | get {
|
---|
54 | if (clientGroupAdapter == null) {
|
---|
55 | clientGroupAdapter = ServiceLocator.GetClientGroupAdapter();
|
---|
56 | }
|
---|
57 |
|
---|
58 | return clientGroupAdapter;
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | private IJobAdapter jobAdapter = null;
|
---|
63 |
|
---|
64 | private IJobAdapter JobAdapter {
|
---|
65 | get {
|
---|
66 | if (jobAdapter == null) {
|
---|
67 | jobAdapter = ServiceLocator.GetJobAdapter();
|
---|
68 | }
|
---|
69 |
|
---|
70 | return jobAdapter;
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | public ClientAdapter() {
|
---|
75 | adapter.Fill(data);
|
---|
76 | }
|
---|
77 |
|
---|
78 | protected override void Update() {
|
---|
79 | this.adapter.Update(this.data);
|
---|
80 | }
|
---|
81 |
|
---|
82 | private ClientInfo Convert(dsHiveServer.ClientRow row,
|
---|
83 | ClientInfo client) {
|
---|
84 | if(row != null && client != null) {
|
---|
85 | /*Parent - resource*/
|
---|
86 | client.ResourceId = row.ResourceId;
|
---|
87 | ResAdapter.GetResourceById(client);
|
---|
88 |
|
---|
89 | /*ClientInfo*/
|
---|
90 | client.ClientId = row.GUID;
|
---|
91 |
|
---|
92 | if (!row.IsCPUSpeedNull())
|
---|
93 | client.CpuSpeedPerCore = row.CPUSpeed;
|
---|
94 | else
|
---|
95 | client.CpuSpeedPerCore = 0;
|
---|
96 |
|
---|
97 | if (!row.IsMemoryNull())
|
---|
98 | client.Memory = row.Memory;
|
---|
99 | else
|
---|
100 | client.Memory = 0;
|
---|
101 |
|
---|
102 | if (!row.IsLoginNull())
|
---|
103 | client.Login = row.Login;
|
---|
104 | else
|
---|
105 | client.Login = DateTime.MinValue;
|
---|
106 |
|
---|
107 | if (!row.IsStatusNull())
|
---|
108 | client.State = (State)Enum.Parse(typeof(State), row.Status, true);
|
---|
109 | else
|
---|
110 | client.State = State.idle;
|
---|
111 |
|
---|
112 | if (!row.IsNumberOfCoresNull())
|
---|
113 | client.NrOfCores = row.NumberOfCores;
|
---|
114 | else
|
---|
115 | client.NrOfCores = 0;
|
---|
116 |
|
---|
117 | //todo: config adapter (client.config)
|
---|
118 |
|
---|
119 | return client;
|
---|
120 | }
|
---|
121 | else
|
---|
122 | return null;
|
---|
123 | }
|
---|
124 |
|
---|
125 | private dsHiveServer.ClientRow Convert(ClientInfo client,
|
---|
126 | dsHiveServer.ClientRow row) {
|
---|
127 | if (client != null && row != null) {
|
---|
128 | row.GUID = client.ClientId;
|
---|
129 | row.CPUSpeed = client.CpuSpeedPerCore;
|
---|
130 | row.Memory = client.Memory;
|
---|
131 | row.Login = client.Login;
|
---|
132 | row.Status = client.State.ToString();
|
---|
133 | row.NumberOfCores = client.NrOfCores;
|
---|
134 |
|
---|
135 | //todo: config adapter
|
---|
136 | /*if (client.Config != null)
|
---|
137 | row.ClientConfigId = client.Config.ClientConfigId;
|
---|
138 | else
|
---|
139 | row.ClientConfigId = null;*/
|
---|
140 | }
|
---|
141 |
|
---|
142 | return row;
|
---|
143 | }
|
---|
144 |
|
---|
145 | #region IClientAdapter Members
|
---|
146 | [MethodImpl(MethodImplOptions.Synchronized)]
|
---|
147 | public void UpdateClient(ClientInfo client) {
|
---|
148 | if (client != null) {
|
---|
149 | ResAdapter.UpdateResource(client);
|
---|
150 |
|
---|
151 | dsHiveServer.ClientRow row =
|
---|
152 | data.FindByResourceId(client.ResourceId);
|
---|
153 |
|
---|
154 | if (row == null) {
|
---|
155 | row = data.NewClientRow();
|
---|
156 | row.ResourceId = client.ResourceId;
|
---|
157 | data.AddClientRow(row);
|
---|
158 | }
|
---|
159 |
|
---|
160 | Convert(client, row);
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | public ClientInfo GetClientById(Guid clientId) {
|
---|
165 | ClientInfo client = new ClientInfo();
|
---|
166 |
|
---|
167 | dsHiveServer.ClientRow row = null;
|
---|
168 | IEnumerable<dsHiveServer.ClientRow> clients =
|
---|
169 | from c in
|
---|
170 | data.AsEnumerable<dsHiveServer.ClientRow>()
|
---|
171 | where !c.IsGUIDNull() && c.GUID == clientId
|
---|
172 | select c;
|
---|
173 | if (clients.Count<dsHiveServer.ClientRow>() == 1)
|
---|
174 | row = clients.First<dsHiveServer.ClientRow>();
|
---|
175 |
|
---|
176 | if (row != null) {
|
---|
177 | Convert(row, client);
|
---|
178 |
|
---|
179 | return client;
|
---|
180 | } else {
|
---|
181 | return null;
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | public ClientInfo GetClientById(long id) {
|
---|
186 | ClientInfo client = new ClientInfo();
|
---|
187 |
|
---|
188 | dsHiveServer.ClientRow row = data.FindByResourceId(id);
|
---|
189 |
|
---|
190 | if (row != null) {
|
---|
191 | Convert(row, client);
|
---|
192 |
|
---|
193 | return client;
|
---|
194 | } else {
|
---|
195 | return null;
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | public ClientInfo GetClientByName(string name) {
|
---|
200 | ClientInfo client = new ClientInfo();
|
---|
201 |
|
---|
202 | Resource res =
|
---|
203 | ResAdapter.GetResourceByName(name);
|
---|
204 |
|
---|
205 | if (res != null) {
|
---|
206 | dsHiveServer.ClientRow row =
|
---|
207 | data.FindByResourceId(res.ResourceId);
|
---|
208 |
|
---|
209 | if (row != null) {
|
---|
210 | Convert(row, client);
|
---|
211 |
|
---|
212 | return client;
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | return null;
|
---|
217 | }
|
---|
218 |
|
---|
219 | public ICollection<ClientInfo> GetAllClients() {
|
---|
220 | ICollection<ClientInfo> allClients =
|
---|
221 | new List<ClientInfo>();
|
---|
222 |
|
---|
223 | foreach (dsHiveServer.ClientRow row in data) {
|
---|
224 | ClientInfo client = new ClientInfo();
|
---|
225 | Convert(row, client);
|
---|
226 | allClients.Add(client);
|
---|
227 | }
|
---|
228 |
|
---|
229 | return allClients;
|
---|
230 | }
|
---|
231 |
|
---|
232 | [MethodImpl(MethodImplOptions.Synchronized)]
|
---|
233 | public bool DeleteClient(ClientInfo client) {
|
---|
234 | if (client != null) {
|
---|
235 | dsHiveServer.ClientRow row = null;
|
---|
236 | IEnumerable<dsHiveServer.ClientRow> clients =
|
---|
237 | from c in
|
---|
238 | data.AsEnumerable<dsHiveServer.ClientRow>()
|
---|
239 | where !c.IsGUIDNull() && c.GUID == client.ClientId
|
---|
240 | select c;
|
---|
241 | if (clients.Count<dsHiveServer.ClientRow>() == 1)
|
---|
242 | row = clients.First<dsHiveServer.ClientRow>();
|
---|
243 |
|
---|
244 | if (row != null) {
|
---|
245 | //Referential integrity with client groups
|
---|
246 | ICollection<ClientGroup> clientGroups =
|
---|
247 | ClientGroupAdapter.MemberOf(client);
|
---|
248 | foreach (ClientGroup group in clientGroups) {
|
---|
249 | group.Resources.Remove(client);
|
---|
250 | ClientGroupAdapter.UpdateClientGroup(group);
|
---|
251 | }
|
---|
252 |
|
---|
253 | //Referential integrity with jobs
|
---|
254 | ICollection<Job> jobs =
|
---|
255 | JobAdapter.GetJobsOf(client);
|
---|
256 | foreach (Job job in jobs) {
|
---|
257 | JobAdapter.DeleteJob(job);
|
---|
258 | }
|
---|
259 |
|
---|
260 | row.Delete();
|
---|
261 | adapter.Update(row);
|
---|
262 |
|
---|
263 | return ResAdapter.DeleteResource(client);
|
---|
264 | }
|
---|
265 | }
|
---|
266 |
|
---|
267 | return false;
|
---|
268 | }
|
---|
269 |
|
---|
270 | #endregion
|
---|
271 | }
|
---|
272 | }
|
---|