Free cookie consent management tool by TermsFeed Policy Generator

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

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

impl update (#437)

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