Free cookie consent management tool by TermsFeed Policy Generator

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

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

impl update (#437)

File size: 7.9 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 System.ServiceModel.Description;
[919]41using HeuristicLab.Hive.Client.Core.ClientConsoleService;
[932]42using HeuristicLab.Hive.Client.Core.ConfigurationManager;
[993]43using HeuristicLab.Hive.Client.Communication.ServerService;
[1001]44using HeuristicLab.Hive.JobBase;
[714]45
[768]46
[714]47namespace HeuristicLab.Hive.Client.Core {
[997]48  public class Core: MarshalByRefObject {
[804]49    public delegate string GetASnapshotDelegate();
50
[768]51    Dictionary<long, Executor> engines = new Dictionary<long, Executor>();
[770]52    Dictionary<long, AppDomain> appDomains = new Dictionary<long, AppDomain>();
[1005]53    Dictionary<long, Job> jobs = new Dictionary<long, Job>();
54
[923]55    private WcfService wcfService;
[798]56
[790]57    public void Start() {
[900]58
[901]59      ClientConsoleServer server = new ClientConsoleServer();
60      server.StartClientConsoleServer(new Uri("net.tcp://127.0.0.1:8000/ClientConsole/"));
[843]61
[932]62      ConfigManager manager = ConfigManager.Instance;
[908]63      manager.Core = this;
64
[923]65      wcfService = WcfService.Instance;
[944]66      ConnectionContainer cc = ConfigManager.Instance.GetServerIPAndPort();
[949]67      if (cc.IPAdress != String.Empty && cc.Port != 0) {
68        wcfService.Connect(cc.IPAdress, cc.Port);
[944]69      }
[993]70
[923]71      wcfService.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(wcfService_LoginCompleted);
72      wcfService.PullJobCompleted += new EventHandler<PullJobCompletedEventArgs>(wcfService_PullJobCompleted);
[993]73      wcfService.SendJobResultCompleted += new EventHandler<SendJobResultCompletedEventArgs>(wcfService_SendJobResultCompleted);     
[924]74      wcfService.ConnectionRestored += new EventHandler(wcfService_ConnectionRestored);
[932]75      wcfService.ServerChanged += new EventHandler(wcfService_ServerChanged);
[993]76       
[900]77      Heartbeat beat = new Heartbeat { Interval = 10000 };
[841]78      beat.StartHeartbeat();     
79
[735]80      MessageQueue queue = MessageQueue.GetInstance();
81      while (true) {
82        MessageContainer container = queue.GetMessage();
[779]83        Debug.WriteLine("Main loop received this message: " + container.Message.ToString());
[790]84        Logging.GetInstance().Info(this.ToString(), container.Message.ToString());
[768]85        DetermineAction(container);
[735]86      }
87    }
[768]88
89    private void DetermineAction(MessageContainer container) {
[779]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;
[790]97
[779]98        case MessageContainer.MessageType.RequestSnapshot:
99          engines[container.JobId].RequestSnapshot();
100          break;
101        case MessageContainer.MessageType.SnapshotReady:
[811]102          Thread ssr = new Thread(new ParameterizedThreadStart(GetSnapshot));
103          ssr.Start(container.JobId);         
[779]104          break;
[790]105
[811]106        case MessageContainer.MessageType.FetchJob:
[923]107          wcfService.PullJobAsync(Guid.NewGuid());
[811]108          break;         
[779]109        case MessageContainer.MessageType.FinishedJob:
[811]110          Thread finThread = new Thread(new ParameterizedThreadStart(GetFinishedJob));
111          finThread.Start(container.JobId);         
112          break;     
[779]113      }
114    }
[790]115
[923]116    #region Async Threads for the EE
117   
[811]118    private void GetFinishedJob(object jobId) {
119      long jId = (long)jobId;
[830]120      byte[] sJob = engines[jId].GetFinishedJob();
[1005]121
122      JobResult jobResult = new JobResult { Job = jobs[jId], Result = sJob, Client = ConfigManager.Instance.GetClientInfo() };
[993]123      wcfService.SendJobResultAsync(jobResult, true);     
[804]124    }
125
[811]126    private void GetSnapshot(object jobId) {
127      long jId = (long)jobId;
[816]128      byte[] obj = engines[jId].GetSnapshot();
[811]129    }
130
[923]131    #endregion
132
133    #region wcfService Events
134
[924]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
[923]139    void wcfService_LoginCompleted(object sender, LoginCompletedEventArgs e) {
140      if (e.Result.Success) {
[944]141        Logging.GetInstance().Info(this.ToString(), "Login completed to Hive Server @ " + DateTime.Now);       
[923]142      } else
143        Logging.GetInstance().Error(this.ToString(), e.Result.StatusMessage);
144    }   
145
146    void wcfService_PullJobCompleted(object sender, PullJobCompletedEventArgs e) {
[960]147      if (e.Result.StatusMessage != ApplicationConstants.RESPONSE_COMMUNICATOR_NO_JOBS_LEFT) {
[1001]148        bool sandboxed = false;
[798]149
[960]150        PluginManager.Manager.Initialize();
[1005]151        AppDomain appDomain =  PluginManager.Manager.CreateAndInitAppDomainWithSandbox(e.Result.Job.Id.ToString(), sandboxed, typeof(TestJob));
[997]152        appDomain.UnhandledException += new UnhandledExceptionEventHandler(appDomain_UnhandledException);
153
[1005]154        jobs.Add(e.Result.Job.Id, e.Result.Job);
155        appDomains.Add(e.Result.Job.Id, appDomain);
[798]156
[960]157        Executor engine = (Executor)appDomain.CreateInstanceAndUnwrap(typeof(Executor).Assembly.GetName().Name, typeof(Executor).FullName);
[1005]158        engine.JobId = e.Result.Job.Id;
[960]159        engine.Queue = MessageQueue.GetInstance();
160        engine.Start(e.Result.SerializedJob);
[1005]161        engines.Add(e.Result.Job.Id, engine);
[798]162
[960]163        ClientStatusInfo.JobsFetched++;
[798]164
[960]165        Debug.WriteLine("Increment FetchedJobs to:" + ClientStatusInfo.JobsFetched);
166      }
[798]167    }
168
[923]169    void wcfService_SendJobResultCompleted(object sender, SendJobResultCompletedEventArgs e) {
[840]170      if (e.Result.Success) {
[1005]171        AppDomain.Unload(appDomains[e.Result.Job.Id]);
172        appDomains.Remove(e.Result.Job.Id);
173        engines.Remove(e.Result.Job.Id);
[1015]174        jobs.Remove(e.Result.Job.Id);
[932]175        ClientStatusInfo.JobsProcessed++;
176        Debug.WriteLine("ProcessedJobs to:" + ClientStatusInfo.JobsProcessed);
[840]177      } else {
178        Debug.WriteLine("Job sending FAILED!");
179      }
[779]180    }
[908]181
[932]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>();
[993]187      wcfService.LoginAsync(ConfigManager.Instance.GetClientInfo());
[932]188    }
189
190
[923]191    #endregion
192
[908]193    public Dictionary<long, Executor> GetExecutionEngines() {
194      return engines;
195    }
[997]196
197    void appDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
198      Logging.GetInstance().Error(this.ToString(), " Exception: " + e.ExceptionObject.ToString());
199    }
[714]200  }
201}
Note: See TracBrowser for help on using the repository browser.