Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Client.Core/Core.cs @ 886

Last change on this file since 886 was 886, checked in by kgrading, 16 years ago

worked on #410

File size: 7.1 KB
RevLine 
[735]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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;
[714]23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
[768]26using HeuristicLab.Hive.Client.ExecutionEngine;
[735]27using HeuristicLab.Hive.Client.Common;
[768]28using System.Threading;
[770]29using System.Reflection;
30using System.Diagnostics;
31using System.Security.Permissions;
32using System.Security.Policy;
33using System.Security;
[790]34using HeuristicLab.Hive.Client.Communication;
[793]35using HeuristicLab.Hive.Contracts.BusinessObjects;
36using HeuristicLab.Hive.Contracts;
[804]37using System.Runtime.Remoting.Messaging;
[816]38using HeuristicLab.PluginInfrastructure;
[843]39using System.ServiceModel;
40using HeuristicLab.Hive.Client.Communication.Interfaces;
41using System.ServiceModel.Description;
[714]42
[768]43
[714]44namespace HeuristicLab.Hive.Client.Core {
45  public class Core {
[768]46
[804]47    public delegate string GetASnapshotDelegate();
48
[768]49    Dictionary<long, Executor> engines = new Dictionary<long, Executor>();
[770]50    Dictionary<long, AppDomain> appDomains = new Dictionary<long, AppDomain>();
51
[798]52    private ClientCommunicatorClient clientCommunicator;
53
[790]54    public void Start() {
[882]55       /*DiscoveryService discService =
[843]56        new DiscoveryService();
57      IClientConsoleCommunicator[] clientCommunicatorInstances =
58        discService.GetInstances<IClientConsoleCommunicator>();
59
60      if (clientCommunicatorInstances.Length > 0) {
61        ServiceHost serviceHost =
62                new ServiceHost(clientCommunicatorInstances[0].GetType(),
63                  new Uri("http://localhost:9000/ClientConsole"));
64
65        System.ServiceModel.Channels.Binding binding =
66          new NetNamedPipeBinding();
67
68        serviceHost.AddServiceEndpoint(
69          typeof(IClientConsoleCommunicator),
70              binding,
71              "ClientConsoleCommunicator");
72
73        ServiceMetadataBehavior behavior =
74              new ServiceMetadataBehavior();
75        serviceHost.Description.Behaviors.Add(behavior);
76
77        serviceHost.AddServiceEndpoint(
78            typeof(IMetadataExchange),
79            MetadataExchangeBindings.CreateMexNamedPipeBinding(),
80            "mex");
81
82        serviceHost.Open();
[882]83      }*/
[843]84
[798]85      clientCommunicator = ServiceLocator.GetClientCommunicator();
[790]86      clientCommunicator.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(ClientCommunicator_LoginCompleted);
[808]87      clientCommunicator.PullJobCompleted += new EventHandler<PullJobCompletedEventArgs>(ClientCommunicator_PullJobCompleted);
[830]88      clientCommunicator.SendJobResultCompleted += new EventHandler<SendJobResultCompletedEventArgs>(ClientCommunicator_SendJobResultCompleted);
[882]89      //clientCommunicator.LoginAsync(ConfigurationManager.GetInstance().GetClientInfo());
[790]90
[886]91      Heartbeat beat = new Heartbeat { Interval = 30000 };
[841]92      beat.StartHeartbeat();     
93
[735]94      MessageQueue queue = MessageQueue.GetInstance();
95      while (true) {
96        MessageContainer container = queue.GetMessage();
[779]97        Debug.WriteLine("Main loop received this message: " + container.Message.ToString());
[790]98        Logging.GetInstance().Info(this.ToString(), container.Message.ToString());
[768]99        DetermineAction(container);
[735]100      }
101    }
[768]102
[798]103    void ClientCommunicator_LoginCompleted(object sender, LoginCompletedEventArgs e) {
[790]104      if (e.Result.Success) {
105        Logging.GetInstance().Info(this.ToString(), "Login completed to Hive Server @ " + DateTime.Now);
[841]106        ConfigurationManager.GetInstance().Loggedin();
[790]107        Status.LoginTime = DateTime.Now;
108        Status.LoggedIn = true;
109      } else
110        Logging.GetInstance().Error(this.ToString(), e.Result.StatusMessage);
111    }
112
[768]113    private void DetermineAction(MessageContainer container) {
[779]114      switch (container.Message) {
115        case MessageContainer.MessageType.AbortJob:
116          engines[container.JobId].Abort();
117          break;
118        case MessageContainer.MessageType.JobAborted:
119          Debug.WriteLine("-- Job Aborted Message received");
120          break;
[790]121
[779]122        case MessageContainer.MessageType.RequestSnapshot:
123          engines[container.JobId].RequestSnapshot();
124          break;
125        case MessageContainer.MessageType.SnapshotReady:
[811]126          Thread ssr = new Thread(new ParameterizedThreadStart(GetSnapshot));
127          ssr.Start(container.JobId);         
[779]128          break;
[790]129
[811]130        case MessageContainer.MessageType.FetchJob:
[798]131          clientCommunicator.PullJobAsync(Guid.NewGuid());
[811]132          break;         
[779]133        case MessageContainer.MessageType.FinishedJob:
[811]134          Thread finThread = new Thread(new ParameterizedThreadStart(GetFinishedJob));
135          finThread.Start(container.JobId);         
136          break;     
[779]137      }
138    }
[790]139
[811]140    private void GetFinishedJob(object jobId) {
141      long jId = (long)jobId;
[830]142      byte[] sJob = engines[jId].GetFinishedJob();
[816]143     
[841]144      JobResult jobResult = new JobResult { JobId = jId, Result = sJob, Client = ConfigurationManager.GetInstance().GetClientInfo() };
[830]145      clientCommunicator.SendJobResultAsync(jobResult, true);
[804]146    }
147
[811]148    private void GetSnapshot(object jobId) {
149      long jId = (long)jobId;
[816]150      byte[] obj = engines[jId].GetSnapshot();
[811]151    }
152
[798]153    void ClientCommunicator_PullJobCompleted(object sender, PullJobCompletedEventArgs e) {
154      bool sandboxed = false;
155
[886]156      PluginManager.Manager.Initialize();
157      AppDomain appDomain =  PluginManager.Manager.CreateAndInitAppDomainWithSandbox(e.Result.JobId.ToString(), sandboxed, typeof(TestJob));
[882]158     
[830]159      appDomains.Add(e.Result.JobId, appDomain);
[798]160
161      Executor engine = (Executor)appDomain.CreateInstanceAndUnwrap(typeof(Executor).Assembly.GetName().Name, typeof(Executor).FullName);
[830]162      engine.JobId = e.Result.JobId;
[798]163      engine.Queue = MessageQueue.GetInstance();
[830]164      engine.Start(e.Result.SerializedJob);
165      engines.Add(e.Result.JobId, engine);
[798]166
167      Status.CurrentJobs++;
168
169      Debug.WriteLine("Increment CurrentJobs to:"+Status.CurrentJobs.ToString());
170    }
171
[840]172    void ClientCommunicator_SendJobResultCompleted(object sender, SendJobResultCompletedEventArgs e) {
173      if (e.Result.Success) {
174        AppDomain.Unload(appDomains[e.Result.JobId]);
175        appDomains.Remove(e.Result.JobId);
176        engines.Remove(e.Result.JobId);
177        Status.CurrentJobs--;
178        Debug.WriteLine("Decrement CurrentJobs to:" + Status.CurrentJobs.ToString());
179      } else {
180        Debug.WriteLine("Job sending FAILED!");
181      }
[779]182    }
[714]183  }
184}
Note: See TracBrowser for help on using the repository browser.