Changeset 16692 for branches/2521_ProblemRefactoring/HeuristicLab.Clients.OKB/3.3/Query/QueryClient.cs
- Timestamp:
- 03/18/19 17:24:30 (5 years ago)
- Location:
- branches/2521_ProblemRefactoring
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring
- Property svn:ignore
-
old new 24 24 protoc.exe 25 25 obj 26 .vs
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/2521_ProblemRefactoring/HeuristicLab.Clients.OKB/3.3/Query/QueryClient.cs
r12012 r16692 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 5Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.IO; 24 25 using System.Linq; 26 using System.ServiceModel; 25 27 using HeuristicLab.Clients.Common; 26 28 using HeuristicLab.Common; 27 29 using HeuristicLab.Core; 30 using HeuristicLab.Persistence.Default.Xml; 28 31 29 32 namespace HeuristicLab.Clients.OKB.Query { … … 38 41 } 39 42 43 private int endpointRetries; 44 private string workingEndpoint; 45 40 46 #region Properties 41 47 private List<Filter> filters; … … 61 67 filters.AddRange(CallQueryService<List<Filter>>(s => s.GetFilters())); 62 68 valueNames.AddRange(CallQueryService<List<ValueName>>(s => s.GetValueNames())); 63 } 64 finally { 69 } finally { 65 70 OnRefreshed(); 66 71 } 67 72 } 68 73 public void RefreshAsync(Action<Exception> exceptionCallback) { 69 var call = new Func<Exception>(delegate () {74 var call = new Func<Exception>(delegate () { 70 75 try { 71 76 Refresh(); 72 } 73 catch (Exception ex) { 77 } catch (Exception ex) { 74 78 return ex; 75 79 } 76 80 return null; 77 81 }); 78 call.BeginInvoke(delegate (IAsyncResult result) {82 call.BeginInvoke(delegate (IAsyncResult result) { 79 83 Exception ex = call.EndInvoke(result); 80 84 if (ex != null) exceptionCallback(ex); … … 95 99 public IEnumerable<Run> GetRunsWithValues(IEnumerable<long> ids, bool includeBinaryValues, IEnumerable<ValueName> vn) { 96 100 return CallQueryService<IEnumerable<Run>>(s => s.GetRunsWithValues(ids.ToList(), includeBinaryValues, vn.ToList())); 101 } 102 #endregion 103 104 #region OKB-Item Conversion 105 public Optimization.IRun ConvertToOptimizationRun(Run run) { 106 Optimization.Run optRun = new Optimization.Run(); 107 foreach (Value value in run.ParameterValues) 108 optRun.Parameters.Add(value.Name, ConvertToItem(value)); 109 foreach (Value value in run.ResultValues) 110 optRun.Results.Add(value.Name, ConvertToItem(value)); 111 return optRun; 112 } 113 114 public IItem ConvertToItem(Value value) { 115 if (value is BinaryValue) { 116 IItem item = null; 117 var binaryValue = (BinaryValue)value; 118 if (binaryValue.Value != null) { 119 using (var stream = new MemoryStream(binaryValue.Value)) { 120 try { 121 item = XmlParser.Deserialize<IItem>(stream); 122 } catch (Exception) { } 123 stream.Close(); 124 } 125 } 126 return item ?? new Data.StringValue(value.DataType.Name); 127 } else if (value is BoolValue) { 128 return new Data.BoolValue(((BoolValue)value).Value); 129 } else if (value is FloatValue) { 130 return new Data.DoubleValue(((FloatValue)value).Value); 131 } else if (value is PercentValue) { 132 return new Data.PercentValue(((PercentValue)value).Value); 133 } else if (value is DoubleValue) { 134 return new Data.DoubleValue(((DoubleValue)value).Value); 135 } else if (value is IntValue) { 136 return new Data.IntValue((int)((IntValue)value).Value); 137 } else if (value is LongValue) { 138 return new Data.IntValue((int)((LongValue)value).Value); 139 } else if (value is StringValue) { 140 return new Data.StringValue(((StringValue)value).Value); 141 } else if (value is TimeSpanValue) { 142 return new Data.TimeSpanValue(TimeSpan.FromSeconds((long)((TimeSpanValue)value).Value)); 143 } 144 return null; 97 145 } 98 146 #endregion … … 112 160 113 161 #region Helpers 162 private QueryServiceClient NewServiceClient() { 163 if (endpointRetries >= Properties.Settings.Default.MaxEndpointRetries) 164 return CreateClient(workingEndpoint); 165 166 var configurations = Properties.Settings.Default.EndpointConfigurationPriorities; 167 Exception exception = null; 168 169 foreach (var endpointConfigurationName in configurations) { 170 try { 171 var cl = CreateClient(endpointConfigurationName); 172 cl.Open(); 173 workingEndpoint = endpointConfigurationName; 174 return cl; 175 } catch (EndpointNotFoundException e) { 176 exception = e; 177 ++endpointRetries; 178 } 179 } 180 181 throw exception ?? new EndpointNotFoundException("No endpoint for Query service found."); 182 } 183 184 private QueryServiceClient CreateClient(string endpointConfigurationName) { 185 var cl = ClientFactory.CreateClient<QueryServiceClient, IQueryService>(endpointConfigurationName); 186 return cl; 187 } 188 114 189 private T CallQueryService<T>(Func<IQueryService, T> call) { 115 QueryServiceClient client = ClientFactory.CreateClient<QueryServiceClient, IQueryService>(); 190 QueryServiceClient client = NewServiceClient(); 191 116 192 try { 117 193 return call(client); 118 } 119 finally { 194 } finally { 120 195 try { 121 196 client.Close(); 122 } 123 catch (Exception) { 197 } catch (Exception) { 124 198 client.Abort(); 125 199 }
Note: See TracChangeset
for help on using the changeset viewer.