#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Clients.Common;
using HeuristicLab.Common;
using HeuristicLab.Core;
using System.ServiceModel;
using HeuristicLab.Clients.Hive.WebJobManager.Services;
using HeuristicLab.Clients.Hive.WebJobManager.ViewModels;
using System.ServiceModel.Security;
using HeuristicLab.Clients.OKB.Administration;
namespace HeuristicLab.Clients.Hive.WebJobManager.Services.Imports
{
[Item("OkbAdministrationWebClient", "OKB administration web client.")]
public sealed class OkbAdministrationWebClient : IContent
{
private string log;
private string pass;
private AdministrationServiceClient client;
public Guid UserId { get; set; }
public OkbAdministrationWebClient(Guid u) : this()
{
UserId = u;
client = null;
log = WebLoginService.Instance.getServiceLocator(UserId).Username;
pass = WebLoginService.Instance.getServiceLocator(UserId).Password;
}
public OkbAdministrationWebClient(LoginViewModel log, string pass) : this()
{
client = null;
this.UserId = log.userId;
this.log = log.loginName;
this.pass = pass;
}
#region Properties
private ItemCollection platforms;
public ItemCollection Platforms
{
get { return platforms; }
}
private ItemCollection algorithmClasses;
public ItemCollection AlgorithmClasses
{
get { return algorithmClasses; }
}
private ItemCollection algorithms;
public ItemCollection Algorithms
{
get { return algorithms; }
}
private ItemCollection problemClasses;
public ItemCollection ProblemClasses
{
get { return problemClasses; }
}
private ItemCollection problems;
public ItemCollection Problems
{
get { return problems; }
}
#endregion
public OkbAdministrationWebClient() { }
#region Refresh
public void Refresh()
{
OnRefreshing();
platforms = new OKBItemCollection();
algorithmClasses = new OKBItemCollection();
algorithms = new OKBItemCollection();
problemClasses = new OKBItemCollection();
problems = new OKBItemCollection();
try
{
platforms.AddRange(CallAdministrationService>(s => s.GetPlatforms()).OrderBy(x => x.Name));
algorithmClasses.AddRange(CallAdministrationService>(s => s.GetAlgorithmClasses()).OrderBy(x => x.Name));
algorithms.AddRange(CallAdministrationService>(s => s.GetAlgorithms()).OrderBy(x => x.Name));
problemClasses.AddRange(CallAdministrationService>(s => s.GetProblemClasses()).OrderBy(x => x.Name));
problems.AddRange(CallAdministrationService>(s => s.GetProblems()).OrderBy(x => x.Name));
}
finally
{
OnRefreshed();
}
}
public void RefreshAsync(Action exceptionCallback)
{
var call = new Func(delegate ()
{
try
{
Refresh();
}
catch (Exception ex)
{
return ex;
}
return null;
});
call.BeginInvoke(delegate (IAsyncResult result)
{
Exception ex = call.EndInvoke(result);
if (ex != null) exceptionCallback(ex);
}, null);
}
#endregion
#region Store
public void Store(IOKBItem item)
{
if (item.Id == 0)
{
if (item is Platform)
item.Id = CallAdministrationService(s => s.AddPlatform((Platform)item));
else if (item is AlgorithmClass)
item.Id = CallAdministrationService(s => s.AddAlgorithmClass((AlgorithmClass)item));
else if (item is Algorithm)
item.Id = CallAdministrationService(s => s.AddAlgorithm((Algorithm)item));
else if (item is ProblemClass)
item.Id = CallAdministrationService(s => s.AddProblemClass((ProblemClass)item));
else if (item is Problem)
item.Id = CallAdministrationService(s => s.AddProblem((Problem)item));
}
else
{
if (item is Platform)
CallAdministrationService(s => s.UpdatePlatform((Platform)item));
else if (item is AlgorithmClass)
CallAdministrationService(s => s.UpdateAlgorithmClass((AlgorithmClass)item));
else if (item is Algorithm)
CallAdministrationService(s => s.UpdateAlgorithm((Algorithm)item));
else if (item is ProblemClass)
CallAdministrationService(s => s.UpdateProblemClass((ProblemClass)item));
else if (item is Problem)
CallAdministrationService(s => s.UpdateProblem((Problem)item));
}
}
#endregion
#region Delete
public void Delete(IOKBItem item)
{
if (item is Platform)
CallAdministrationService(s => s.DeletePlatform(item.Id));
else if (item is AlgorithmClass)
CallAdministrationService(s => s.DeleteAlgorithmClass(item.Id));
else if (item is Algorithm)
CallAdministrationService(s => s.DeleteAlgorithm(item.Id));
else if (item is ProblemClass)
CallAdministrationService(s => s.DeleteProblemClass(item.Id));
else if (item is Problem)
CallAdministrationService(s => s.DeleteProblem(item.Id));
item.Id = 0;
}
#endregion
#region Algorithm Methods
public List GetAlgorithmUsers(long algorithmId)
{
return CallAdministrationService>(s => s.GetAlgorithmUsers(algorithmId));
}
public void UpdateAlgorithmUsers(long algorithmId, List users)
{
CallAdministrationService(s => s.UpdateAlgorithmUsers(algorithmId, users));
}
public byte[] GetAlgorithmData(long algorithmId)
{
return CallAdministrationService(s => s.GetAlgorithmData(algorithmId));
}
public void UpdateAlgorithmData(long algorithmId, byte[] algorithmData)
{
CallAdministrationService(s => s.UpdateAlgorithmData(algorithmId, algorithmData));
}
#endregion
#region Problem Methods
public List GetProblemUsers(long problemId)
{
return CallAdministrationService>(s => s.GetProblemUsers(problemId));
}
public void UpdateProblemUsers(long problemId, List users)
{
CallAdministrationService(s => s.UpdateProblemUsers(problemId, users));
}
public byte[] GetProblemData(long problemId)
{
return CallAdministrationService(s => s.GetProblemData(problemId));
}
public void UpdateProblemData(long problemId, byte[] problemData)
{
CallAdministrationService(s => s.UpdateProblemData(problemId, problemData));
}
#endregion
#region Events
public event EventHandler Refreshing;
private void OnRefreshing()
{
EventHandler handler = Refreshing;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler Refreshed;
private void OnRefreshed()
{
var handler = Refreshed;
if (handler != null) handler(this, EventArgs.Empty);
}
#endregion
#region Helpers
private void CallAdministrationService(Action call)
{
if (client == null || client.State == CommunicationState.Closed || client.State == CommunicationState.Faulted)
{
client = createServiceClient();
}
try
{
call(client);
}
finally
{
try
{
client.Close();
}
catch (Exception)
{
client.Abort();
}
}
}
private T CallAdministrationService(Func call)
{
if (client == null || client.State == CommunicationState.Closed || client.State == CommunicationState.Faulted)
{
client = createServiceClient();
}
try
{
return call(client);
}
finally
{
try
{
client.Close();
}
catch (Exception)
{
client.Abort();
}
}
}
#endregion
#region Binding
private AdministrationServiceClient createServiceClient()
{
WSHttpBinding binding = new WSHttpBinding();
binding.Name = "wsHttpBinding_IAdministrationService";
binding.CloseTimeout = TimeSpan.FromMinutes(1);
binding.OpenTimeout = TimeSpan.FromMinutes(1);
binding.ReceiveTimeout = TimeSpan.FromMinutes(10);
binding.SendTimeout = TimeSpan.FromMinutes(1);
binding.BypassProxyOnLocal = false;
binding.TransactionFlow = false;
binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
binding.MaxBufferPoolSize = 524288;
binding.MaxReceivedMessageSize = 200000000;
binding.MessageEncoding = WSMessageEncoding.Text;
binding.TextEncoding = System.Text.Encoding.UTF8;
binding.UseDefaultWebProxy = true;
// binding.TransferMode = TransferMode.Buffered;
binding.AllowCookies = false;
binding.ReaderQuotas.MaxDepth = 32;
binding.ReaderQuotas.MaxStringContentLength = 8192;
binding.ReaderQuotas.MaxArrayLength = 200000000;
binding.ReaderQuotas.MaxBytesPerRead = 4096;
binding.ReaderQuotas.MaxNameTableCharCount = 16384;
binding.ReliableSession.Ordered = true;
binding.ReliableSession.InactivityTimeout = TimeSpan.Parse("00:10:00");
binding.ReliableSession.Enabled = false;
binding.Security.Mode = SecurityMode.Message;
binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
binding.Security.Message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Default;
// binding.Security.Message.NegotiateServiceCredential = true;
EndpointAddress end = new EndpointAddress("http://services.heuristiclab.com/OKB-3.3/AdministrationService.svc");
AdministrationServiceClient client = new AdministrationServiceClient(binding, end);
client.ClientCredentials.UserName.UserName = log;
client.ClientCredentials.UserName.Password = pass;
client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
return client;
}
internal bool CheckLogin()
{
try
{
this.Refresh();
return true;
}
catch (MessageSecurityException e )
{
return false;
}
catch (SecurityAccessDeniedException ex)
{
return false;
}
}
#endregion
}
}