[826] | 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;
|
---|
[925] | 28 | using System.Linq.Expressions;
|
---|
| 29 | using System.Runtime.CompilerServices;
|
---|
[826] | 30 |
|
---|
| 31 | namespace HeuristicLab.Hive.Server.ADODataAccess {
|
---|
[925] | 32 | class ClientAdapter: DataAdapterBase, IClientAdapter {
|
---|
[826] | 33 | private dsHiveServerTableAdapters.ClientTableAdapter adapter =
|
---|
| 34 | new dsHiveServerTableAdapters.ClientTableAdapter();
|
---|
| 35 |
|
---|
[925] | 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 | }
|
---|
[845] | 49 |
|
---|
[899] | 50 | private ClientInfo Convert(dsHiveServer.ClientRow row,
|
---|
| 51 | ClientInfo client) {
|
---|
| 52 | if(row != null && client != null) {
|
---|
[845] | 53 | /*Parent - resource*/
|
---|
[899] | 54 | client.ResourceId = row.ResourceId;
|
---|
[925] | 55 | resAdapter.GetResourceById(client);
|
---|
[826] | 56 |
|
---|
[845] | 57 | /*ClientInfo*/
|
---|
| 58 | client.ClientId = row.GUID;
|
---|
[899] | 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())
|
---|
[845] | 76 | client.State = (State)Enum.Parse(typeof(State), row.Status, true);
|
---|
[899] | 77 | else
|
---|
| 78 | client.State = State.idle;
|
---|
[845] | 79 |
|
---|
[899] | 80 | if (!row.IsNumberOfCoresNull())
|
---|
| 81 | client.NrOfCores = row.NumberOfCores;
|
---|
| 82 | else
|
---|
| 83 | client.NrOfCores = 0;
|
---|
| 84 |
|
---|
[845] | 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 |
|
---|
[925] | 113 | #region IClientAdapter Members
|
---|
| 114 | [MethodImpl(MethodImplOptions.Synchronized)]
|
---|
[826] | 115 | public void UpdateClient(ClientInfo client) {
|
---|
| 116 | if (client != null) {
|
---|
| 117 | resAdapter.UpdateResource(client);
|
---|
| 118 |
|
---|
[925] | 119 | dsHiveServer.ClientRow row =
|
---|
| 120 | data.FindByResourceId(client.ResourceId);
|
---|
[826] | 121 |
|
---|
[925] | 122 | if (row == null) {
|
---|
[826] | 123 | row = data.NewClientRow();
|
---|
| 124 | row.ResourceId = client.ResourceId;
|
---|
| 125 | data.AddClientRow(row);
|
---|
[925] | 126 | }
|
---|
[826] | 127 |
|
---|
[845] | 128 | Convert(client, row);
|
---|
[826] | 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | public ClientInfo GetClientById(Guid clientId) {
|
---|
[899] | 133 | ClientInfo client = new ClientInfo();
|
---|
[925] | 134 |
|
---|
[936] | 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>();
|
---|
[925] | 143 |
|
---|
| 144 | if (row != null) {
|
---|
[899] | 145 | Convert(row, client);
|
---|
| 146 |
|
---|
| 147 | return client;
|
---|
[826] | 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) {
|
---|
[899] | 158 | ClientInfo client = new ClientInfo();
|
---|
| 159 | Convert(row, client);
|
---|
| 160 | allClients.Add(client);
|
---|
[826] | 161 | }
|
---|
| 162 |
|
---|
| 163 | return allClients;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[925] | 166 | [MethodImpl(MethodImplOptions.Synchronized)]
|
---|
[826] | 167 | public bool DeleteClient(ClientInfo client) {
|
---|
[925] | 168 | if (client != null) {
|
---|
[936] | 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>();
|
---|
[925] | 177 |
|
---|
| 178 | if (row != null) {
|
---|
| 179 | data.RemoveClientRow(row);
|
---|
| 180 |
|
---|
| 181 | return resAdapter.DeleteResource(client);
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | return false;
|
---|
[826] | 186 | }
|
---|
| 187 |
|
---|
| 188 | #endregion
|
---|
| 189 | }
|
---|
| 190 | }
|
---|