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