using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Hive.Contracts.Interfaces; using HeuristicLab.Hive.Contracts.ResponseObjects; using HeuristicLab.Hive.Contracts.BusinessObjects; using HeuristicLab.Hive.Experiment.Properties; using System.ServiceModel; using HeuristicLab.Hive.Contracts; namespace HeuristicLab.Hive.Experiment { [Item("Hive Client", "Connects to Hive and lists all submitted experiments by the current user.")] [Creatable("Hive")] public class HiveClient : Item { private static object locker = new object(); private ILog log; public ILog Log { get { return log; } } private HiveExperimentList hiveExperiments; public HiveExperimentList HiveExperiments { get { return hiveExperiments; } set { if (hiveExperiments != value) { if (hiveExperiments != null) { DeRegisterHiveExperimentsEvents(); } hiveExperiments = value; RegisterHiveExperimentsEvent(); OnHiveExperimentsChanged(); } } } private void RegisterHiveExperimentsEvent() { hiveExperiments.ItemsAdded += new Collections.CollectionItemsChangedEventHandler>(hiveExperiments_ItemsAdded); hiveExperiments.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler>(hiveExperiments_ItemsRemoved); hiveExperiments.ItemsMoved += new Collections.CollectionItemsChangedEventHandler>(hiveExperiments_ItemsMoved); } private void DeRegisterHiveExperimentsEvents() { hiveExperiments.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler>(hiveExperiments_ItemsAdded); hiveExperiments.ItemsRemoved -= new Collections.CollectionItemsChangedEventHandler>(hiveExperiments_ItemsRemoved); hiveExperiments.ItemsMoved -= new Collections.CollectionItemsChangedEventHandler>(hiveExperiments_ItemsMoved); } public HiveClient() { this.log = new Log(); this.HiveExperiments = new HiveExperimentList(); } public override Common.IDeepCloneable Clone(Common.Cloner cloner) { HiveClient clone = (HiveClient)base.Clone(cloner); clone.log = (ILog)cloner.Clone(this.log); clone.hiveExperiments = (HiveExperimentList)cloner.Clone(this.hiveExperiments); return clone; } public void UpdateExperimentList() { //IClientFacade client = CreateClientFacade(); using (Disposable service = ServiceLocator.Instance.ClientFacadePool.GetService()) { ResponseObject response = service.Obj.GetChildJobResults(null, false, false); RefreshExperimentList(response.Obj); } //ServiceLocator.DisposeClientFacade(client); } private void RefreshExperimentList(JobResultList jobResultList) { foreach (JobResult jobResult in jobResultList) { HiveExperiment hiveExperiment = GetHiveExperiment(jobResult); if (hiveExperiment == null) { // create new hiveExperiment = new HiveExperiment() { RootJobItem = new JobItem(jobResult) }; this.hiveExperiments.Add(hiveExperiment); } else { // update //hiveExperiment.RootJob.UpdateJob(jobResult); } } } private HiveExperiment GetHiveExperiment(JobResult jobResult) { //var matches = this.hiveExperiments.Where(exp => exp.RootJob != null && exp.RootJob.JobDto.Id == jobResult.JobId); //if (matches.Count() == 0) { // return null; //} else { // return matches.First(); //} return null; } //private IClientFacade CreateClientFacade() { // IClientFacade clientFacade = null; // try { // clientFacade = ServiceLocator.CreateClientFacade(Settings.Default.HiveServerIp); // } // catch (EndpointNotFoundException exception) { // LogMessage("Could not connect to Server: " + exception.Message); // } // return clientFacade; //} private void LogMessage(string message) { // HeuristicLab.Log is not Thread-Safe, so lock on every call lock (locker) { log.LogMessage(message); } } public event EventHandler HiveExperimentsChanged; private void OnHiveExperimentsChanged() { var handler = HiveExperimentsChanged; if (handler != null) handler(this, EventArgs.Empty); } void hiveExperiments_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs> e) { } void hiveExperiments_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs> e) { } void hiveExperiments_ItemsMoved(object sender, Collections.CollectionItemsChangedEventArgs> e) { } } }