[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 |
|
---|
| 22 | using System;
|
---|
[714] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
[768] | 26 | using HeuristicLab.Hive.Client.ExecutionEngine;
|
---|
[735] | 27 | using HeuristicLab.Hive.Client.Common;
|
---|
[768] | 28 | using System.Threading;
|
---|
[770] | 29 | using System.Reflection;
|
---|
| 30 | using System.Diagnostics;
|
---|
| 31 | using System.Security.Permissions;
|
---|
| 32 | using System.Security.Policy;
|
---|
| 33 | using System.Security;
|
---|
[790] | 34 | using HeuristicLab.Hive.Client.Communication;
|
---|
[793] | 35 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
| 36 | using HeuristicLab.Hive.Contracts;
|
---|
[804] | 37 | using System.Runtime.Remoting.Messaging;
|
---|
[816] | 38 | using HeuristicLab.PluginInfrastructure;
|
---|
[843] | 39 | using System.ServiceModel;
|
---|
| 40 | using HeuristicLab.Hive.Client.Communication.Interfaces;
|
---|
| 41 | using System.ServiceModel.Description;
|
---|
[714] | 42 |
|
---|
[768] | 43 |
|
---|
[714] | 44 | namespace 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 |
|
---|
| 52 | public static StrongName CreateStrongName(Assembly assembly) {
|
---|
| 53 | if (assembly == null)
|
---|
| 54 | throw new ArgumentNullException("assembly");
|
---|
| 55 |
|
---|
| 56 | AssemblyName assemblyName = assembly.GetName();
|
---|
| 57 | Debug.Assert(assemblyName != null, "Could not get assembly name");
|
---|
| 58 |
|
---|
| 59 | // get the public key blob
|
---|
| 60 | byte[] publicKey = assemblyName.GetPublicKey();
|
---|
| 61 | if (publicKey == null || publicKey.Length == 0)
|
---|
| 62 | throw new InvalidOperationException("Assembly is not strongly named");
|
---|
| 63 |
|
---|
| 64 | StrongNamePublicKeyBlob keyBlob = new StrongNamePublicKeyBlob(publicKey);
|
---|
| 65 |
|
---|
| 66 | // and create the StrongName
|
---|
| 67 | return new StrongName(keyBlob, assemblyName.Name, assemblyName.Version);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[798] | 70 | private ClientCommunicatorClient clientCommunicator;
|
---|
| 71 |
|
---|
[790] | 72 | public void Start() {
|
---|
[843] | 73 | DiscoveryService discService =
|
---|
| 74 | new DiscoveryService();
|
---|
| 75 | IClientConsoleCommunicator[] clientCommunicatorInstances =
|
---|
| 76 | discService.GetInstances<IClientConsoleCommunicator>();
|
---|
| 77 |
|
---|
| 78 | if (clientCommunicatorInstances.Length > 0) {
|
---|
| 79 | ServiceHost serviceHost =
|
---|
| 80 | new ServiceHost(clientCommunicatorInstances[0].GetType(),
|
---|
| 81 | new Uri("http://localhost:9000/ClientConsole"));
|
---|
| 82 |
|
---|
| 83 | System.ServiceModel.Channels.Binding binding =
|
---|
| 84 | new NetNamedPipeBinding();
|
---|
| 85 |
|
---|
| 86 | serviceHost.AddServiceEndpoint(
|
---|
| 87 | typeof(IClientConsoleCommunicator),
|
---|
| 88 | binding,
|
---|
| 89 | "ClientConsoleCommunicator");
|
---|
| 90 |
|
---|
| 91 | ServiceMetadataBehavior behavior =
|
---|
| 92 | new ServiceMetadataBehavior();
|
---|
| 93 | serviceHost.Description.Behaviors.Add(behavior);
|
---|
| 94 |
|
---|
| 95 | serviceHost.AddServiceEndpoint(
|
---|
| 96 | typeof(IMetadataExchange),
|
---|
| 97 | MetadataExchangeBindings.CreateMexNamedPipeBinding(),
|
---|
| 98 | "mex");
|
---|
| 99 |
|
---|
| 100 | serviceHost.Open();
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[798] | 103 | clientCommunicator = ServiceLocator.GetClientCommunicator();
|
---|
[790] | 104 | clientCommunicator.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(ClientCommunicator_LoginCompleted);
|
---|
[808] | 105 | clientCommunicator.PullJobCompleted += new EventHandler<PullJobCompletedEventArgs>(ClientCommunicator_PullJobCompleted);
|
---|
[830] | 106 | clientCommunicator.SendJobResultCompleted += new EventHandler<SendJobResultCompletedEventArgs>(ClientCommunicator_SendJobResultCompleted);
|
---|
[841] | 107 | clientCommunicator.LoginAsync(ConfigurationManager.GetInstance().GetClientInfo());
|
---|
[790] | 108 |
|
---|
[841] | 109 | Heartbeat beat = new Heartbeat { Interval = 5000 };
|
---|
| 110 | beat.StartHeartbeat();
|
---|
| 111 |
|
---|
[735] | 112 | MessageQueue queue = MessageQueue.GetInstance();
|
---|
| 113 | while (true) {
|
---|
| 114 | MessageContainer container = queue.GetMessage();
|
---|
[779] | 115 | Debug.WriteLine("Main loop received this message: " + container.Message.ToString());
|
---|
[790] | 116 | Logging.GetInstance().Info(this.ToString(), container.Message.ToString());
|
---|
[768] | 117 | DetermineAction(container);
|
---|
[735] | 118 | }
|
---|
| 119 | }
|
---|
[768] | 120 |
|
---|
[798] | 121 | void ClientCommunicator_LoginCompleted(object sender, LoginCompletedEventArgs e) {
|
---|
[790] | 122 | if (e.Result.Success) {
|
---|
| 123 | Logging.GetInstance().Info(this.ToString(), "Login completed to Hive Server @ " + DateTime.Now);
|
---|
[841] | 124 | ConfigurationManager.GetInstance().Loggedin();
|
---|
[790] | 125 | Status.LoginTime = DateTime.Now;
|
---|
| 126 | Status.LoggedIn = true;
|
---|
| 127 | } else
|
---|
| 128 | Logging.GetInstance().Error(this.ToString(), e.Result.StatusMessage);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[770] | 131 | private AppDomain CreateNewAppDomain(bool sandboxed) {
|
---|
| 132 | PermissionSet pset;
|
---|
| 133 | if (sandboxed) {
|
---|
| 134 | pset = new PermissionSet(PermissionState.None);
|
---|
[790] | 135 | pset.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
|
---|
[770] | 136 | } else {
|
---|
| 137 | pset = new PermissionSet(PermissionState.Unrestricted);
|
---|
| 138 | }
|
---|
| 139 | AppDomainSetup setup = new AppDomainSetup();
|
---|
| 140 | setup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
|
---|
| 141 | //Temp Fix!
|
---|
| 142 | setup.PrivateBinPath = "plugins";
|
---|
[790] | 143 | return System.AppDomain.CreateDomain("appD", AppDomain.CurrentDomain.Evidence, setup, pset, CreateStrongName(Assembly.GetExecutingAssembly()));
|
---|
[770] | 144 |
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[768] | 147 | private void DetermineAction(MessageContainer container) {
|
---|
[779] | 148 | switch (container.Message) {
|
---|
| 149 | case MessageContainer.MessageType.AbortJob:
|
---|
| 150 | engines[container.JobId].Abort();
|
---|
| 151 | break;
|
---|
| 152 | case MessageContainer.MessageType.JobAborted:
|
---|
| 153 | Debug.WriteLine("-- Job Aborted Message received");
|
---|
| 154 | break;
|
---|
[790] | 155 |
|
---|
[779] | 156 | case MessageContainer.MessageType.RequestSnapshot:
|
---|
| 157 | engines[container.JobId].RequestSnapshot();
|
---|
| 158 | break;
|
---|
| 159 | case MessageContainer.MessageType.SnapshotReady:
|
---|
[811] | 160 | Thread ssr = new Thread(new ParameterizedThreadStart(GetSnapshot));
|
---|
| 161 | ssr.Start(container.JobId);
|
---|
[779] | 162 | break;
|
---|
[790] | 163 |
|
---|
[811] | 164 | case MessageContainer.MessageType.FetchJob:
|
---|
[798] | 165 | clientCommunicator.PullJobAsync(Guid.NewGuid());
|
---|
[811] | 166 | break;
|
---|
[779] | 167 | case MessageContainer.MessageType.FinishedJob:
|
---|
[811] | 168 | Thread finThread = new Thread(new ParameterizedThreadStart(GetFinishedJob));
|
---|
| 169 | finThread.Start(container.JobId);
|
---|
| 170 | break;
|
---|
[779] | 171 | }
|
---|
| 172 | }
|
---|
[790] | 173 |
|
---|
[811] | 174 | private void GetFinishedJob(object jobId) {
|
---|
| 175 | long jId = (long)jobId;
|
---|
[830] | 176 | byte[] sJob = engines[jId].GetFinishedJob();
|
---|
[816] | 177 |
|
---|
[841] | 178 | JobResult jobResult = new JobResult { JobId = jId, Result = sJob, Client = ConfigurationManager.GetInstance().GetClientInfo() };
|
---|
[830] | 179 | clientCommunicator.SendJobResultAsync(jobResult, true);
|
---|
[804] | 180 | }
|
---|
| 181 |
|
---|
[811] | 182 | private void GetSnapshot(object jobId) {
|
---|
| 183 | long jId = (long)jobId;
|
---|
[816] | 184 | byte[] obj = engines[jId].GetSnapshot();
|
---|
[811] | 185 | }
|
---|
| 186 |
|
---|
[798] | 187 | void ClientCommunicator_PullJobCompleted(object sender, PullJobCompletedEventArgs e) {
|
---|
| 188 | bool sandboxed = false;
|
---|
| 189 |
|
---|
[830] | 190 | //IJob job = new TestJob { JobId = e.Result.JobId };
|
---|
[798] | 191 |
|
---|
[816] | 192 | PluginManager pm = PluginManager.Manager;
|
---|
| 193 | AppDomain appDomain = pm.CreateAndInitAppDomain("AppDomain");
|
---|
| 194 |
|
---|
| 195 | //AppDomain appDomain = CreateNewAppDomain(sandboxed);
|
---|
[830] | 196 | appDomains.Add(e.Result.JobId, appDomain);
|
---|
[798] | 197 |
|
---|
| 198 | Executor engine = (Executor)appDomain.CreateInstanceAndUnwrap(typeof(Executor).Assembly.GetName().Name, typeof(Executor).FullName);
|
---|
[830] | 199 | engine.JobId = e.Result.JobId;
|
---|
[798] | 200 | engine.Queue = MessageQueue.GetInstance();
|
---|
[830] | 201 | engine.Start(e.Result.SerializedJob);
|
---|
| 202 | engines.Add(e.Result.JobId, engine);
|
---|
[798] | 203 |
|
---|
| 204 | Status.CurrentJobs++;
|
---|
| 205 |
|
---|
| 206 | Debug.WriteLine("Increment CurrentJobs to:"+Status.CurrentJobs.ToString());
|
---|
| 207 | }
|
---|
| 208 |
|
---|
[840] | 209 | void ClientCommunicator_SendJobResultCompleted(object sender, SendJobResultCompletedEventArgs e) {
|
---|
| 210 | if (e.Result.Success) {
|
---|
| 211 | AppDomain.Unload(appDomains[e.Result.JobId]);
|
---|
| 212 | appDomains.Remove(e.Result.JobId);
|
---|
| 213 | engines.Remove(e.Result.JobId);
|
---|
| 214 | Status.CurrentJobs--;
|
---|
| 215 | Debug.WriteLine("Decrement CurrentJobs to:" + Status.CurrentJobs.ToString());
|
---|
| 216 | } else {
|
---|
| 217 | Debug.WriteLine("Job sending FAILED!");
|
---|
| 218 | }
|
---|
[779] | 219 | }
|
---|
[714] | 220 | }
|
---|
| 221 | }
|
---|