Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/ClientAdapter.cs @ 936

Last change on this file since 936 was 936, checked in by svonolfe, 15 years ago

Implemented UserGroupAdapter (#372)

File size: 5.6 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Hive.Server.Core.InternalInterfaces.DataAccess;
27using HeuristicLab.Hive.Contracts.BusinessObjects;
28using System.Linq.Expressions;
29using System.Runtime.CompilerServices;
30
31namespace 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 =
40      ServiceLocator.GetResourceAdapter();
41
42    public ClientAdapter() {
43      adapter.Fill(data);
44    }
45
46    protected override void Update() {
47      this.adapter.Update(this.data);
48    }
49   
50    private ClientInfo Convert(dsHiveServer.ClientRow row,
51      ClientInfo client) {
52      if(row != null && client != null) {     
53        /*Parent - resource*/
54        client.ResourceId = row.ResourceId;
55        resAdapter.GetResourceById(client);
56
57        /*ClientInfo*/
58        client.ClientId = row.GUID;
59       
60        if (!row.IsCPUSpeedNull())
61          client.CpuSpeedPerCore = row.CPUSpeed;
62        else
63          client.CpuSpeedPerCore = 0;
64
65        if (!row.IsMemoryNull())
66          client.Memory = row.Memory;
67        else
68          client.Memory = 0;
69
70        if (!row.IsLoginNull())
71          client.Login = row.Login;
72        else
73          client.Login = DateTime.MinValue;
74
75        if (!row.IsStatusNull())
76          client.State = (State)Enum.Parse(typeof(State), row.Status, true);
77        else
78          client.State = State.idle;
79
80        if (!row.IsNumberOfCoresNull())
81          client.NrOfCores = row.NumberOfCores;
82        else
83          client.NrOfCores = 0;
84
85        //todo: config adapter (client.config)
86
87        return client;
88      }
89      else
90        return null;
91    }
92
93    private dsHiveServer.ClientRow Convert(ClientInfo client,
94      dsHiveServer.ClientRow row) {
95      if (client != null && row != null) {     
96        row.GUID = client.ClientId;
97        row.CPUSpeed = client.CpuSpeedPerCore;
98        row.Memory = client.Memory;
99        row.Login = client.Login;
100        row.Status = client.State.ToString();
101        row.NumberOfCores = client.NrOfCores;
102
103        //todo: config adapter
104        /*if (client.Config != null)
105          row.ClientConfigId = client.Config.ClientConfigId;
106         else
107          row.ClientConfigId = null;*/
108      }
109
110      return row;
111    }
112
113    #region IClientAdapter Members
114    [MethodImpl(MethodImplOptions.Synchronized)]
115    public void UpdateClient(ClientInfo client) {
116      if (client != null) {
117        resAdapter.UpdateResource(client);
118
119        dsHiveServer.ClientRow row =
120          data.FindByResourceId(client.ResourceId);
121
122        if (row == null) {
123          row = data.NewClientRow();
124          row.ResourceId = client.ResourceId;
125          data.AddClientRow(row);
126        }
127
128        Convert(client, row);
129      }
130    }
131
132    public ClientInfo GetClientById(Guid clientId) {
133      ClientInfo client = new ClientInfo();
134
135      dsHiveServer.ClientRow row = null;
136      IEnumerable<dsHiveServer.ClientRow> clients =
137            from c in
138              data.AsEnumerable<dsHiveServer.ClientRow>()
139            where !c.IsGUIDNull() && c.GUID == clientId
140            select c;
141      if (clients.Count<dsHiveServer.ClientRow>() == 1)
142        row = clients.First<dsHiveServer.ClientRow>();
143
144      if (row != null) {
145        Convert(row, client);
146
147        return client;
148      } else {
149        return null;
150      }
151    }
152
153    public ICollection<ClientInfo> GetAllClients() {
154      ICollection<ClientInfo> allClients =
155        new List<ClientInfo>();
156
157      foreach (dsHiveServer.ClientRow row in data) {
158        ClientInfo client = new ClientInfo();
159        Convert(row, client);
160        allClients.Add(client);
161      }
162
163      return allClients;
164    }
165
166    [MethodImpl(MethodImplOptions.Synchronized)]
167    public bool DeleteClient(ClientInfo client) {
168      if (client != null) {
169        dsHiveServer.ClientRow row = null;
170        IEnumerable<dsHiveServer.ClientRow> clients =
171              from c in
172                data.AsEnumerable<dsHiveServer.ClientRow>()
173              where !c.IsGUIDNull() && c.GUID == client.ClientId
174              select c;
175        if (clients.Count<dsHiveServer.ClientRow>() == 1)
176          row = clients.First<dsHiveServer.ClientRow>();
177
178        if (row != null) {
179          data.RemoveClientRow(row);
180
181          return resAdapter.DeleteResource(client);
182        }
183      }
184
185      return false;
186    }
187
188    #endregion
189  }
190}
Note: See TracBrowser for help on using the repository browser.