[13862] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2015 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.IO;
|
---|
| 25 | using System.Linq;
|
---|
| 26 | using HeuristicLab.Clients.Common;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.Persistence.Default.Xml;
|
---|
| 30 | using System.ServiceModel;
|
---|
| 31 | using HeuristicLab.Clients.Hive.WebJobManager.Services;
|
---|
| 32 | using HeuristicLab.Clients.OKB.Query;
|
---|
| 33 | using System.ServiceModel.Security;
|
---|
| 34 | using HeuristicLab.Clients.Hive.WebJobManager.ViewModels;
|
---|
| 35 |
|
---|
| 36 | namespace HeuristicLab.Clients.Hive.WebJobManager.Services.Imports
|
---|
| 37 | {
|
---|
| 38 | [Item("QueryWebClient", "OKB query client.")]
|
---|
| 39 | public sealed class QueryWebClient : IContent
|
---|
| 40 | {
|
---|
| 41 | private string log;
|
---|
| 42 | private string pass;
|
---|
| 43 |
|
---|
| 44 | private QueryServiceClient client;
|
---|
| 45 | public Guid UserId { get; set; }
|
---|
| 46 | public QueryWebClient(Guid u) : this()
|
---|
| 47 | {
|
---|
| 48 | UserId = u;
|
---|
| 49 | client = null;
|
---|
| 50 | log = WebLoginService.Instance.getServiceLocator(UserId).Username;
|
---|
| 51 | pass = WebLoginService.Instance.getServiceLocator(UserId).Password;
|
---|
| 52 | }
|
---|
| 53 | public QueryWebClient(LoginViewModel log, string pass): this()
|
---|
| 54 | {
|
---|
| 55 | client = null;
|
---|
| 56 | this.UserId = log.userId;
|
---|
| 57 | this.log = log.loginName;
|
---|
| 58 | this.pass = pass;
|
---|
| 59 |
|
---|
| 60 | }
|
---|
| 61 | #region Properties
|
---|
| 62 | private List<Filter> filters;
|
---|
| 63 | public IEnumerable<Filter> Filters
|
---|
| 64 | {
|
---|
| 65 | get { return filters; }
|
---|
| 66 | }
|
---|
| 67 | private List<ValueName> valueNames;
|
---|
| 68 | public IEnumerable<ValueName> ValueNames
|
---|
| 69 | {
|
---|
| 70 | get { return valueNames; }
|
---|
| 71 | }
|
---|
| 72 | #endregion
|
---|
| 73 |
|
---|
| 74 | private QueryWebClient()
|
---|
| 75 | {
|
---|
| 76 | filters = new List<Filter>();
|
---|
| 77 | valueNames = new List<ValueName>();
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | #region Refresh
|
---|
| 81 | public void Refresh()
|
---|
| 82 | {
|
---|
| 83 | OnRefreshing();
|
---|
| 84 | filters = new List<Filter>();
|
---|
| 85 | valueNames = new List<ValueName>();
|
---|
| 86 | try
|
---|
| 87 | {
|
---|
| 88 | filters.AddRange(CallQueryService<List<Filter>>(s => s.GetFilters()));
|
---|
| 89 | valueNames.AddRange(CallQueryService<List<ValueName>>(s => s.GetValueNames()));
|
---|
| 90 | }
|
---|
| 91 | finally
|
---|
| 92 | {
|
---|
| 93 | OnRefreshed();
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 | public void RefreshAsync(Action<Exception> exceptionCallback)
|
---|
| 97 | {
|
---|
| 98 | var call = new Func<Exception>(delegate ()
|
---|
| 99 | {
|
---|
| 100 | try
|
---|
| 101 | {
|
---|
| 102 | Refresh();
|
---|
| 103 | }
|
---|
| 104 | catch (Exception ex)
|
---|
| 105 | {
|
---|
| 106 | return ex;
|
---|
| 107 | }
|
---|
| 108 | return null;
|
---|
| 109 | });
|
---|
| 110 | call.BeginInvoke(delegate (IAsyncResult result)
|
---|
| 111 | {
|
---|
| 112 | Exception ex = call.EndInvoke(result);
|
---|
| 113 | if (ex != null) exceptionCallback(ex);
|
---|
| 114 | }, null);
|
---|
| 115 | }
|
---|
| 116 | #endregion
|
---|
| 117 |
|
---|
| 118 | #region Query Methods
|
---|
| 119 | public long GetNumberOfRuns(Filter filter)
|
---|
| 120 | {
|
---|
| 121 | return CallQueryService<long>(x => x.GetNumberOfRuns(filter));
|
---|
| 122 | }
|
---|
| 123 | public IEnumerable<long> GetRunIds(Filter filter)
|
---|
| 124 | {
|
---|
| 125 | return CallQueryService<IEnumerable<long>>(x => x.GetRunIds(filter));
|
---|
| 126 | }
|
---|
| 127 | public IEnumerable<Run> GetRuns(IEnumerable<long> ids, bool includeBinaryValues)
|
---|
| 128 | {
|
---|
| 129 | return CallQueryService<IEnumerable<Run>>(s => s.GetRuns(ids.ToList(), includeBinaryValues));
|
---|
| 130 | }
|
---|
| 131 | public IEnumerable<Run> GetRunsWithValues(IEnumerable<long> ids, bool includeBinaryValues, IEnumerable<ValueName> vn)
|
---|
| 132 | {
|
---|
| 133 | return CallQueryService<IEnumerable<Run>>(s => s.GetRunsWithValues(ids.ToList(), includeBinaryValues, vn.ToList()));
|
---|
| 134 | }
|
---|
| 135 | #endregion
|
---|
| 136 |
|
---|
| 137 | #region OKB-Item Conversion
|
---|
| 138 | public Optimization.IRun ConvertToOptimizationRun(Run run)
|
---|
| 139 | {
|
---|
| 140 | Optimization.Run optRun = new Optimization.Run();
|
---|
| 141 | foreach (Value value in run.ParameterValues)
|
---|
| 142 | optRun.Parameters.Add(value.Name, ConvertToItem(value));
|
---|
| 143 | foreach (Value value in run.ResultValues)
|
---|
| 144 | optRun.Results.Add(value.Name, ConvertToItem(value));
|
---|
| 145 | return optRun;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | public IItem ConvertToItem(Value value)
|
---|
| 149 | {
|
---|
| 150 | if (value is BinaryValue)
|
---|
| 151 | {
|
---|
| 152 | IItem item = null;
|
---|
| 153 | var binaryValue = (BinaryValue)value;
|
---|
| 154 | if (binaryValue.Value != null)
|
---|
| 155 | {
|
---|
| 156 | using (var stream = new MemoryStream(binaryValue.Value))
|
---|
| 157 | {
|
---|
| 158 | try
|
---|
| 159 | {
|
---|
| 160 | item = XmlParser.Deserialize<IItem>(stream);
|
---|
| 161 | }
|
---|
| 162 | catch (Exception) { }
|
---|
| 163 | stream.Close();
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 | return item ?? new Data.StringValue(value.DataType.Name);
|
---|
| 167 | }
|
---|
| 168 | else if (value is BoolValue)
|
---|
| 169 | {
|
---|
| 170 | return new Data.BoolValue(((BoolValue)value).Value);
|
---|
| 171 | }
|
---|
| 172 | else if (value is FloatValue)
|
---|
| 173 | {
|
---|
| 174 | return new Data.DoubleValue(((FloatValue)value).Value);
|
---|
| 175 | }
|
---|
| 176 | else if (value is PercentValue)
|
---|
| 177 | {
|
---|
| 178 | return new Data.PercentValue(((PercentValue)value).Value);
|
---|
| 179 | }
|
---|
| 180 | else if (value is DoubleValue)
|
---|
| 181 | {
|
---|
| 182 | return new Data.DoubleValue(((DoubleValue)value).Value);
|
---|
| 183 | }
|
---|
| 184 | else if (value is IntValue)
|
---|
| 185 | {
|
---|
| 186 | return new Data.IntValue((int)((IntValue)value).Value);
|
---|
| 187 | }
|
---|
| 188 | else if (value is LongValue)
|
---|
| 189 | {
|
---|
| 190 | return new Data.IntValue((int)((LongValue)value).Value);
|
---|
| 191 | }
|
---|
| 192 | else if (value is StringValue)
|
---|
| 193 | {
|
---|
| 194 | return new Data.StringValue(((StringValue)value).Value);
|
---|
| 195 | }
|
---|
| 196 | else if (value is TimeSpanValue)
|
---|
| 197 | {
|
---|
| 198 | return new Data.TimeSpanValue(TimeSpan.FromSeconds((long)((TimeSpanValue)value).Value));
|
---|
| 199 | }
|
---|
| 200 | return null;
|
---|
| 201 | }
|
---|
| 202 | #endregion
|
---|
| 203 |
|
---|
| 204 | #region Events
|
---|
| 205 | public event EventHandler Refreshing;
|
---|
| 206 | private void OnRefreshing()
|
---|
| 207 | {
|
---|
| 208 | EventHandler handler = Refreshing;
|
---|
| 209 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 210 | }
|
---|
| 211 | public event EventHandler Refreshed;
|
---|
| 212 | private void OnRefreshed()
|
---|
| 213 | {
|
---|
| 214 | EventHandler handler = Refreshed;
|
---|
| 215 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 216 | }
|
---|
| 217 | #endregion
|
---|
| 218 |
|
---|
| 219 | #region Helpers
|
---|
| 220 | private T CallQueryService<T>(Func<IQueryService, T> call)
|
---|
| 221 | {
|
---|
| 222 |
|
---|
| 223 | if (client == null || client.State == CommunicationState.Closed || client.State == CommunicationState.Faulted)
|
---|
| 224 | {
|
---|
| 225 | client = createServiceClient();
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | try
|
---|
| 229 | {
|
---|
| 230 | return call(client);
|
---|
| 231 | }
|
---|
| 232 | finally
|
---|
| 233 | {
|
---|
| 234 | try
|
---|
| 235 | {
|
---|
| 236 | client.Close();
|
---|
| 237 | }
|
---|
| 238 | catch (Exception)
|
---|
| 239 | {
|
---|
| 240 | client.Abort();
|
---|
| 241 | }
|
---|
| 242 | }
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | private QueryServiceClient createServiceClient()
|
---|
| 246 | {
|
---|
| 247 | WSHttpBinding binding = new WSHttpBinding();
|
---|
| 248 | // I think most (or all) of these are defaults--I just copied them from app.config:
|
---|
| 249 |
|
---|
| 250 | binding.Name = "wsHttpBinding_IQueryService";
|
---|
| 251 | binding.CloseTimeout = TimeSpan.FromMinutes(1);
|
---|
| 252 | binding.OpenTimeout = TimeSpan.FromMinutes(1);
|
---|
| 253 | binding.ReceiveTimeout = TimeSpan.FromMinutes(10);
|
---|
| 254 | binding.SendTimeout = TimeSpan.FromMinutes(1);
|
---|
| 255 | binding.BypassProxyOnLocal = false;
|
---|
| 256 | binding.TransactionFlow = false;
|
---|
| 257 |
|
---|
| 258 | binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
|
---|
| 259 | binding.MaxBufferPoolSize = 524288;
|
---|
| 260 | binding.MaxReceivedMessageSize = 200000000;
|
---|
| 261 | binding.MessageEncoding = WSMessageEncoding.Text;
|
---|
| 262 |
|
---|
| 263 | binding.TextEncoding = System.Text.Encoding.UTF8;
|
---|
| 264 | binding.UseDefaultWebProxy = true;
|
---|
| 265 | // binding.TransferMode = TransferMode.Buffered;
|
---|
| 266 | binding.AllowCookies = false;
|
---|
| 267 |
|
---|
| 268 | binding.ReaderQuotas.MaxDepth = 32;
|
---|
| 269 | binding.ReaderQuotas.MaxStringContentLength = 8192;
|
---|
| 270 | binding.ReaderQuotas.MaxArrayLength = 200000000;
|
---|
| 271 | binding.ReaderQuotas.MaxBytesPerRead = 4096;
|
---|
| 272 | binding.ReaderQuotas.MaxNameTableCharCount = 16384;
|
---|
| 273 |
|
---|
| 274 | binding.ReliableSession.Ordered = true;
|
---|
| 275 | binding.ReliableSession.InactivityTimeout = TimeSpan.Parse("00:10:00");
|
---|
| 276 | binding.ReliableSession.Enabled = false;
|
---|
| 277 |
|
---|
| 278 |
|
---|
| 279 | binding.Security.Mode = SecurityMode.Message;
|
---|
| 280 | binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
|
---|
| 281 | binding.Security.Message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Default;
|
---|
| 282 | // binding.Security.Message.NegotiateServiceCredential = true;
|
---|
| 283 |
|
---|
| 284 |
|
---|
| 285 | EndpointAddress end = new EndpointAddress("http://services.heuristiclab.com/OKB-3.3/QueryService.svc");
|
---|
| 286 |
|
---|
| 287 | QueryServiceClient client = new QueryServiceClient(binding, end);
|
---|
| 288 | client.ClientCredentials.UserName.UserName = log;
|
---|
| 289 | client.ClientCredentials.UserName.Password = pass;
|
---|
| 290 | client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
|
---|
| 291 |
|
---|
| 292 | return client;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | internal bool CheckLogin()
|
---|
| 296 | {
|
---|
| 297 | try
|
---|
| 298 | {
|
---|
| 299 | this.Refresh();
|
---|
| 300 | return true;
|
---|
| 301 | }
|
---|
| 302 | catch (MessageSecurityException e)
|
---|
| 303 | {
|
---|
| 304 | return false;
|
---|
| 305 | }
|
---|
| 306 | catch (SecurityAccessDeniedException e)
|
---|
| 307 | {
|
---|
| 308 | return false;
|
---|
| 309 | }
|
---|
| 310 | }
|
---|
| 311 | #endregion
|
---|
| 312 | }
|
---|
| 313 | }
|
---|