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 System.ServiceModel.Description;
|
---|
41 | using HeuristicLab.Hive.Client.Core.ClientConsoleService;
|
---|
42 | using HeuristicLab.Hive.Client.Core.ConfigurationManager;
|
---|
43 | using HeuristicLab.Hive.Client.Communication.ServerService;
|
---|
44 | using HeuristicLab.Hive.JobBase;
|
---|
45 |
|
---|
46 |
|
---|
47 | namespace HeuristicLab.Hive.Client.Core {
|
---|
48 | public class Core: MarshalByRefObject {
|
---|
49 | public delegate string GetASnapshotDelegate();
|
---|
50 |
|
---|
51 | public static Object Locker { get; set; }
|
---|
52 |
|
---|
53 | Dictionary<long, Executor> engines = new Dictionary<long, Executor>();
|
---|
54 | Dictionary<long, AppDomain> appDomains = new Dictionary<long, AppDomain>();
|
---|
55 | Dictionary<long, Job> jobs = new Dictionary<long, Job>();
|
---|
56 |
|
---|
57 | private WcfService wcfService;
|
---|
58 |
|
---|
59 | public void Start() {
|
---|
60 | Logging.GetInstance().Info(this.ToString(), "Hive Client started");
|
---|
61 | ClientConsoleServer server = new ClientConsoleServer();
|
---|
62 | server.StartClientConsoleServer(new Uri("net.tcp://127.0.0.1:8000/ClientConsole/"));
|
---|
63 |
|
---|
64 | ConfigManager manager = ConfigManager.Instance;
|
---|
65 | manager.Core = this;
|
---|
66 |
|
---|
67 | wcfService = WcfService.Instance;
|
---|
68 | ConnectionContainer cc = ConfigManager.Instance.GetServerIPAndPort();
|
---|
69 | if (cc.IPAdress != String.Empty && cc.Port != 0) {
|
---|
70 | wcfService.Connect(cc.IPAdress, cc.Port);
|
---|
71 | }
|
---|
72 |
|
---|
73 | wcfService.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(wcfService_LoginCompleted);
|
---|
74 | wcfService.PullJobCompleted += new EventHandler<PullJobCompletedEventArgs>(wcfService_PullJobCompleted);
|
---|
75 | wcfService.SendJobResultCompleted += new EventHandler<SendJobResultCompletedEventArgs>(wcfService_SendJobResultCompleted);
|
---|
76 | wcfService.ConnectionRestored += new EventHandler(wcfService_ConnectionRestored);
|
---|
77 | wcfService.ServerChanged += new EventHandler(wcfService_ServerChanged);
|
---|
78 |
|
---|
79 | Heartbeat beat = new Heartbeat { Interval = 10000 };
|
---|
80 | beat.StartHeartbeat();
|
---|
81 |
|
---|
82 | MessageQueue queue = MessageQueue.GetInstance();
|
---|
83 | while (true) {
|
---|
84 | MessageContainer container = queue.GetMessage();
|
---|
85 | Debug.WriteLine("Main loop received this message: " + container.Message.ToString());
|
---|
86 | Logging.GetInstance().Info(this.ToString(), container.Message.ToString());
|
---|
87 | DetermineAction(container);
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | private void DetermineAction(MessageContainer container) {
|
---|
92 | switch (container.Message) {
|
---|
93 | case MessageContainer.MessageType.AbortJob:
|
---|
94 | engines[container.JobId].Abort();
|
---|
95 | break;
|
---|
96 | case MessageContainer.MessageType.JobAborted:
|
---|
97 | Debug.WriteLine("-- Job Aborted Message received");
|
---|
98 | break;
|
---|
99 |
|
---|
100 | case MessageContainer.MessageType.RequestSnapshot:
|
---|
101 | engines[container.JobId].RequestSnapshot();
|
---|
102 | break;
|
---|
103 | case MessageContainer.MessageType.SnapshotReady:
|
---|
104 | Thread ssr = new Thread(new ParameterizedThreadStart(GetSnapshot));
|
---|
105 | ssr.Start(container.JobId);
|
---|
106 | break;
|
---|
107 |
|
---|
108 | case MessageContainer.MessageType.FetchJob:
|
---|
109 | wcfService.PullJobAsync(Guid.NewGuid());
|
---|
110 | break;
|
---|
111 | case MessageContainer.MessageType.FinishedJob:
|
---|
112 | Thread finThread = new Thread(new ParameterizedThreadStart(GetFinishedJob));
|
---|
113 | finThread.Start(container.JobId);
|
---|
114 | break;
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | #region Async Threads for the EE
|
---|
119 |
|
---|
120 | private void GetFinishedJob(object jobId) {
|
---|
121 | long jId = (long)jobId;
|
---|
122 | byte[] sJob = engines[jId].GetFinishedJob();
|
---|
123 |
|
---|
124 | JobResult jobResult = new JobResult { Job = jobs[jId], Result = sJob, Client = ConfigManager.Instance.GetClientInfo() };
|
---|
125 | wcfService.SendJobResultAsync(jobResult, true);
|
---|
126 | }
|
---|
127 |
|
---|
128 | private void GetSnapshot(object jobId) {
|
---|
129 | long jId = (long)jobId;
|
---|
130 | byte[] obj = engines[jId].GetSnapshot();
|
---|
131 | }
|
---|
132 |
|
---|
133 | #endregion
|
---|
134 |
|
---|
135 | #region wcfService Events
|
---|
136 |
|
---|
137 | void wcfService_ConnectionRestored(object sender, EventArgs e) {
|
---|
138 | //Do some fancy new things here... e.g: check all appdomains if there are still active Jobs that need to be transmitted
|
---|
139 | }
|
---|
140 |
|
---|
141 | void wcfService_LoginCompleted(object sender, LoginCompletedEventArgs e) {
|
---|
142 | if (e.Result.Success) {
|
---|
143 | Logging.GetInstance().Info(this.ToString(), "Login completed to Hive Server @ " + DateTime.Now);
|
---|
144 | } else
|
---|
145 | Logging.GetInstance().Error(this.ToString(), e.Result.StatusMessage);
|
---|
146 | }
|
---|
147 |
|
---|
148 | void wcfService_PullJobCompleted(object sender, PullJobCompletedEventArgs e) {
|
---|
149 | if (e.Result.StatusMessage != ApplicationConstants.RESPONSE_COMMUNICATOR_NO_JOBS_LEFT) {
|
---|
150 | bool sandboxed = false;
|
---|
151 |
|
---|
152 | PluginManager.Manager.Initialize();
|
---|
153 | AppDomain appDomain = PluginManager.Manager.CreateAndInitAppDomainWithSandbox(e.Result.Job.Id.ToString(), sandboxed, typeof(TestJob));
|
---|
154 | appDomain.UnhandledException += new UnhandledExceptionEventHandler(appDomain_UnhandledException);
|
---|
155 | lock (Locker) {
|
---|
156 | if (!jobs.ContainsKey(e.Result.Job.Id)) {
|
---|
157 | jobs.Add(e.Result.Job.Id, e.Result.Job);
|
---|
158 | appDomains.Add(e.Result.Job.Id, appDomain);
|
---|
159 |
|
---|
160 | Executor engine = (Executor)appDomain.CreateInstanceAndUnwrap(typeof(Executor).Assembly.GetName().Name, typeof(Executor).FullName);
|
---|
161 | engine.JobId = e.Result.Job.Id;
|
---|
162 | engine.Queue = MessageQueue.GetInstance();
|
---|
163 | engine.Start(e.Result.SerializedJob);
|
---|
164 | engines.Add(e.Result.Job.Id, engine);
|
---|
165 |
|
---|
166 | ClientStatusInfo.JobsFetched++;
|
---|
167 |
|
---|
168 | Debug.WriteLine("Increment FetchedJobs to:" + ClientStatusInfo.JobsFetched);
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | void wcfService_SendJobResultCompleted(object sender, SendJobResultCompletedEventArgs e) {
|
---|
175 | if (e.Result.Success) {
|
---|
176 | lock (Locker) {
|
---|
177 | AppDomain.Unload(appDomains[e.Result.Job.Id]);
|
---|
178 | appDomains.Remove(e.Result.Job.Id);
|
---|
179 | engines.Remove(e.Result.Job.Id);
|
---|
180 | jobs.Remove(e.Result.Job.Id);
|
---|
181 | ClientStatusInfo.JobsProcessed++;
|
---|
182 | }
|
---|
183 | Debug.WriteLine("ProcessedJobs to:" + ClientStatusInfo.JobsProcessed);
|
---|
184 | } else {
|
---|
185 | Debug.WriteLine("Job sending FAILED!");
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | void wcfService_ServerChanged(object sender, EventArgs e) {
|
---|
190 | foreach(KeyValuePair<long, AppDomain> entries in appDomains)
|
---|
191 | AppDomain.Unload(appDomains[entries.Key]);
|
---|
192 | appDomains = new Dictionary<long, AppDomain>();
|
---|
193 | engines = new Dictionary<long, Executor>();
|
---|
194 | wcfService.LoginAsync(ConfigManager.Instance.GetClientInfo());
|
---|
195 | }
|
---|
196 |
|
---|
197 |
|
---|
198 | #endregion
|
---|
199 |
|
---|
200 | public Dictionary<long, Executor> GetExecutionEngines() {
|
---|
201 | return engines;
|
---|
202 | }
|
---|
203 |
|
---|
204 | void appDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
|
---|
205 | Logging.GetInstance().Error(this.ToString(), " Exception: " + e.ExceptionObject.ToString());
|
---|
206 | }
|
---|
207 | }
|
---|
208 | }
|
---|