Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added caching, thread safety to DataAccess layer (#372)

File size: 5.1 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 =
136        data.Single<dsHiveServer.ClientRow>(
137          r => !r.IsGUIDNull() && r.GUID == clientId);
138
139      if (row != null) {
140        Convert(row, client);
141
142        return client;
143      } else {
144        return null;
145      }
146    }
147
148    public ICollection<ClientInfo> GetAllClients() {
149      ICollection<ClientInfo> allClients =
150        new List<ClientInfo>();
151
152      foreach (dsHiveServer.ClientRow row in data) {
153        ClientInfo client = new ClientInfo();
154        Convert(row, client);
155        allClients.Add(client);
156      }
157
158      return allClients;
159    }
160
161    [MethodImpl(MethodImplOptions.Synchronized)]
162    public bool DeleteClient(ClientInfo client) {
163      if (client != null) {
164        dsHiveServer.ClientRow row =
165          data.Single<dsHiveServer.ClientRow>(
166            r => r.GUID == client.ClientId);
167
168        if (row != null) {
169          data.RemoveClientRow(row);
170
171          return resAdapter.DeleteResource(client);
172        }
173      }
174
175      return false;
176    }
177
178    #endregion
179  }
180}
Note: See TracBrowser for help on using the repository browser.