Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Clients.Hive.WebJobManager/Services/Imports/OkbAdministrationWebClient.cs

Last change on this file was 13862, checked in by jlodewyc, 9 years ago

#2582 Start angular OKB manager, data loaded

File size: 13.3 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Clients.Common;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using System.ServiceModel;
29using HeuristicLab.Clients.Hive.WebJobManager.Services;
30using HeuristicLab.Clients.Hive.WebJobManager.ViewModels;
31using System.ServiceModel.Security;
32using HeuristicLab.Clients.OKB.Administration;
33
34namespace HeuristicLab.Clients.Hive.WebJobManager.Services.Imports
35{
36    [Item("OkbAdministrationWebClient", "OKB administration web client.")]
37    public sealed class OkbAdministrationWebClient : IContent
38    {
39
40
41
42        private string log;
43        private string pass;
44
45        private AdministrationServiceClient client;
46        public Guid UserId { get; set; }
47        public OkbAdministrationWebClient(Guid u) : this()
48        {
49            UserId = u;
50            client = null;
51            log = WebLoginService.Instance.getServiceLocator(UserId).Username;
52            pass = WebLoginService.Instance.getServiceLocator(UserId).Password;
53        }
54        public OkbAdministrationWebClient(LoginViewModel log, string pass) : this()
55        {
56            client = null;
57            this.UserId = log.userId;
58            this.log = log.loginName;
59            this.pass = pass;
60
61        }
62
63
64        #region Properties
65        private ItemCollection<Platform> platforms;
66        public ItemCollection<Platform> Platforms
67        {
68            get { return platforms; }
69        }
70        private ItemCollection<AlgorithmClass> algorithmClasses;
71        public ItemCollection<AlgorithmClass> AlgorithmClasses
72        {
73            get { return algorithmClasses; }
74        }
75        private ItemCollection<Algorithm> algorithms;
76        public ItemCollection<Algorithm> Algorithms
77        {
78            get { return algorithms; }
79        }
80        private ItemCollection<ProblemClass> problemClasses;
81        public ItemCollection<ProblemClass> ProblemClasses
82        {
83            get { return problemClasses; }
84        }
85        private ItemCollection<Problem> problems;
86        public ItemCollection<Problem> Problems
87        {
88            get { return problems; }
89        }
90        #endregion
91
92        public OkbAdministrationWebClient() { }
93
94        #region Refresh
95        public void Refresh()
96        {
97            OnRefreshing();
98
99            platforms = new OKBItemCollection<Platform>();
100            algorithmClasses = new OKBItemCollection<AlgorithmClass>();
101            algorithms = new OKBItemCollection<Algorithm>();
102            problemClasses = new OKBItemCollection<ProblemClass>();
103            problems = new OKBItemCollection<Problem>();
104
105            try
106            {
107                platforms.AddRange(CallAdministrationService<List<Platform>>(s => s.GetPlatforms()).OrderBy(x => x.Name));
108                algorithmClasses.AddRange(CallAdministrationService<List<AlgorithmClass>>(s => s.GetAlgorithmClasses()).OrderBy(x => x.Name));
109                algorithms.AddRange(CallAdministrationService<List<Algorithm>>(s => s.GetAlgorithms()).OrderBy(x => x.Name));
110                problemClasses.AddRange(CallAdministrationService<List<ProblemClass>>(s => s.GetProblemClasses()).OrderBy(x => x.Name));
111                problems.AddRange(CallAdministrationService<List<Problem>>(s => s.GetProblems()).OrderBy(x => x.Name));
112            }
113            finally
114            {
115                OnRefreshed();
116            }
117        }
118        public void RefreshAsync(Action<Exception> exceptionCallback)
119        {
120            var call = new Func<Exception>(delegate ()
121            {
122                try
123                {
124                    Refresh();
125                }
126                catch (Exception ex)
127                {
128                    return ex;
129                }
130                return null;
131            });
132            call.BeginInvoke(delegate (IAsyncResult result)
133            {
134                Exception ex = call.EndInvoke(result);
135                if (ex != null) exceptionCallback(ex);
136            }, null);
137        }
138        #endregion
139
140        #region Store
141        public void Store(IOKBItem item)
142        {
143            if (item.Id == 0)
144            {
145                if (item is Platform)
146                    item.Id = CallAdministrationService<long>(s => s.AddPlatform((Platform)item));
147                else if (item is AlgorithmClass)
148                    item.Id = CallAdministrationService<long>(s => s.AddAlgorithmClass((AlgorithmClass)item));
149                else if (item is Algorithm)
150                    item.Id = CallAdministrationService<long>(s => s.AddAlgorithm((Algorithm)item));
151                else if (item is ProblemClass)
152                    item.Id = CallAdministrationService<long>(s => s.AddProblemClass((ProblemClass)item));
153                else if (item is Problem)
154                    item.Id = CallAdministrationService<long>(s => s.AddProblem((Problem)item));
155            }
156            else
157            {
158                if (item is Platform)
159                    CallAdministrationService(s => s.UpdatePlatform((Platform)item));
160                else if (item is AlgorithmClass)
161                    CallAdministrationService(s => s.UpdateAlgorithmClass((AlgorithmClass)item));
162                else if (item is Algorithm)
163                    CallAdministrationService(s => s.UpdateAlgorithm((Algorithm)item));
164                else if (item is ProblemClass)
165                    CallAdministrationService(s => s.UpdateProblemClass((ProblemClass)item));
166                else if (item is Problem)
167                    CallAdministrationService(s => s.UpdateProblem((Problem)item));
168            }
169        }
170        #endregion
171
172        #region Delete
173        public  void Delete(IOKBItem item)
174        {
175            if (item is Platform)
176                CallAdministrationService(s => s.DeletePlatform(item.Id));
177            else if (item is AlgorithmClass)
178                CallAdministrationService(s => s.DeleteAlgorithmClass(item.Id));
179            else if (item is Algorithm)
180                CallAdministrationService(s => s.DeleteAlgorithm(item.Id));
181            else if (item is ProblemClass)
182                CallAdministrationService(s => s.DeleteProblemClass(item.Id));
183            else if (item is Problem)
184                CallAdministrationService(s => s.DeleteProblem(item.Id));
185            item.Id = 0;
186        }
187        #endregion
188
189        #region Algorithm Methods
190        public  List<Guid> GetAlgorithmUsers(long algorithmId)
191        {
192            return CallAdministrationService<List<Guid>>(s => s.GetAlgorithmUsers(algorithmId));
193        }
194        public  void UpdateAlgorithmUsers(long algorithmId, List<Guid> users)
195        {
196            CallAdministrationService(s => s.UpdateAlgorithmUsers(algorithmId, users));
197        }
198        public  byte[] GetAlgorithmData(long algorithmId)
199        {
200            return CallAdministrationService<byte[]>(s => s.GetAlgorithmData(algorithmId));
201        }
202        public  void UpdateAlgorithmData(long algorithmId, byte[] algorithmData)
203        {
204            CallAdministrationService(s => s.UpdateAlgorithmData(algorithmId, algorithmData));
205        }
206        #endregion
207
208        #region Problem Methods
209        public  List<Guid> GetProblemUsers(long problemId)
210        {
211            return CallAdministrationService<List<Guid>>(s => s.GetProblemUsers(problemId));
212        }
213        public  void UpdateProblemUsers(long problemId, List<Guid> users)
214        {
215            CallAdministrationService(s => s.UpdateProblemUsers(problemId, users));
216        }
217        public  byte[] GetProblemData(long problemId)
218        {
219            return CallAdministrationService<byte[]>(s => s.GetProblemData(problemId));
220        }
221        public  void UpdateProblemData(long problemId, byte[] problemData)
222        {
223            CallAdministrationService(s => s.UpdateProblemData(problemId, problemData));
224        }
225        #endregion
226
227        #region Events
228        public event EventHandler Refreshing;
229        private void OnRefreshing()
230        {
231            EventHandler handler = Refreshing;
232            if (handler != null) handler(this, EventArgs.Empty);
233        }
234        public event EventHandler Refreshed;
235        private void OnRefreshed()
236        {
237            var handler = Refreshed;
238            if (handler != null) handler(this, EventArgs.Empty);
239        }
240        #endregion
241
242        #region Helpers
243        private void CallAdministrationService(Action<IAdministrationService> call)
244        {
245            if (client == null || client.State == CommunicationState.Closed || client.State == CommunicationState.Faulted)
246            {
247                client = createServiceClient();
248            }
249            try
250            {
251                call(client);
252            }
253            finally
254            {
255                try
256                {
257                    client.Close();
258                }
259                catch (Exception)
260                {
261                    client.Abort();
262                }
263            }
264        }
265        private T CallAdministrationService<T>(Func<IAdministrationService, T> call)
266        {
267            if (client == null || client.State == CommunicationState.Closed || client.State == CommunicationState.Faulted)
268            {
269                client = createServiceClient();
270            }
271            try
272            {
273                return call(client);
274            }
275            finally
276            {
277                try
278                {
279                    client.Close();
280                }
281                catch (Exception)
282                {
283                    client.Abort();
284                }
285            }
286        }
287        #endregion
288
289        #region Binding
290        private AdministrationServiceClient createServiceClient()
291        {
292            WSHttpBinding binding = new WSHttpBinding();
293           
294
295            binding.Name = "wsHttpBinding_IAdministrationService";
296            binding.CloseTimeout = TimeSpan.FromMinutes(1);
297            binding.OpenTimeout = TimeSpan.FromMinutes(1);
298            binding.ReceiveTimeout = TimeSpan.FromMinutes(10);
299            binding.SendTimeout = TimeSpan.FromMinutes(1);
300            binding.BypassProxyOnLocal = false;
301            binding.TransactionFlow = false;
302
303            binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
304            binding.MaxBufferPoolSize = 524288;
305            binding.MaxReceivedMessageSize = 200000000;
306            binding.MessageEncoding = WSMessageEncoding.Text;
307
308            binding.TextEncoding = System.Text.Encoding.UTF8;
309            binding.UseDefaultWebProxy = true;
310            // binding.TransferMode = TransferMode.Buffered;
311            binding.AllowCookies = false;
312
313            binding.ReaderQuotas.MaxDepth = 32;
314            binding.ReaderQuotas.MaxStringContentLength = 8192;
315            binding.ReaderQuotas.MaxArrayLength = 200000000;
316            binding.ReaderQuotas.MaxBytesPerRead = 4096;
317            binding.ReaderQuotas.MaxNameTableCharCount = 16384;
318
319            binding.ReliableSession.Ordered = true;
320            binding.ReliableSession.InactivityTimeout = TimeSpan.Parse("00:10:00");
321            binding.ReliableSession.Enabled = false;
322
323
324            binding.Security.Mode = SecurityMode.Message;
325            binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
326            binding.Security.Message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Default;
327            // binding.Security.Message.NegotiateServiceCredential = true;
328
329
330            EndpointAddress end = new EndpointAddress("http://services.heuristiclab.com/OKB-3.3/AdministrationService.svc");
331
332            AdministrationServiceClient client = new AdministrationServiceClient(binding, end);
333            client.ClientCredentials.UserName.UserName = log;
334            client.ClientCredentials.UserName.Password = pass;
335            client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
336
337            return client;
338        }
339
340        internal bool CheckLogin()
341        {
342            try
343            {
344                this.Refresh();
345                return true;
346            }
347            catch (MessageSecurityException e )
348            {
349                return false;
350            }
351            catch (SecurityAccessDeniedException ex)
352            {
353                return false;
354            }
355        }
356        #endregion
357    }
358}
Note: See TracBrowser for help on using the repository browser.