Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 924 was 924, checked in by kgrading, 15 years ago

added a connectionRestored Event (#418)

File size: 6.8 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;
[714]42
[768]43
[714]44namespace 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
[923]52    private WcfService wcfService;
[798]53
[790]54    public void Start() {
[900]55
[901]56      ClientConsoleServer server = new ClientConsoleServer();
57      server.StartClientConsoleServer(new Uri("net.tcp://127.0.0.1:8000/ClientConsole/"));
[843]58
[908]59      ConfigurationManager manager = ConfigurationManager.GetInstance();
60      manager.Core = this;
61
[923]62      wcfService = WcfService.Instance;
63      wcfService.Connect("192.168.132.1", "9000");
[790]64
[923]65      wcfService.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(wcfService_LoginCompleted);
66      wcfService.PullJobCompleted += new EventHandler<PullJobCompletedEventArgs>(wcfService_PullJobCompleted);
67      wcfService.SendJobResultCompleted += new EventHandler<SendJobResultCompletedEventArgs>(wcfService_SendJobResultCompleted);
[924]68      wcfService.ConnectionRestored += new EventHandler(wcfService_ConnectionRestored);
[923]69
70      wcfService.LoginAsync(ConfigurationManager.GetInstance().GetClientInfo());
71
[900]72      Heartbeat beat = new Heartbeat { Interval = 10000 };
[841]73      beat.StartHeartbeat();     
74
[735]75      MessageQueue queue = MessageQueue.GetInstance();
76      while (true) {
77        MessageContainer container = queue.GetMessage();
[779]78        Debug.WriteLine("Main loop received this message: " + container.Message.ToString());
[790]79        Logging.GetInstance().Info(this.ToString(), container.Message.ToString());
[768]80        DetermineAction(container);
[735]81      }
82    }
[768]83
84    private void DetermineAction(MessageContainer container) {
[779]85      switch (container.Message) {
86        case MessageContainer.MessageType.AbortJob:
87          engines[container.JobId].Abort();
88          break;
89        case MessageContainer.MessageType.JobAborted:
90          Debug.WriteLine("-- Job Aborted Message received");
91          break;
[790]92
[779]93        case MessageContainer.MessageType.RequestSnapshot:
94          engines[container.JobId].RequestSnapshot();
95          break;
96        case MessageContainer.MessageType.SnapshotReady:
[811]97          Thread ssr = new Thread(new ParameterizedThreadStart(GetSnapshot));
98          ssr.Start(container.JobId);         
[779]99          break;
[790]100
[811]101        case MessageContainer.MessageType.FetchJob:
[923]102          wcfService.PullJobAsync(Guid.NewGuid());
[811]103          break;         
[779]104        case MessageContainer.MessageType.FinishedJob:
[811]105          Thread finThread = new Thread(new ParameterizedThreadStart(GetFinishedJob));
106          finThread.Start(container.JobId);         
107          break;     
[779]108      }
109    }
[790]110
[923]111    #region Async Threads for the EE
112   
[811]113    private void GetFinishedJob(object jobId) {
114      long jId = (long)jobId;
[830]115      byte[] sJob = engines[jId].GetFinishedJob();
[816]116     
[841]117      JobResult jobResult = new JobResult { JobId = jId, Result = sJob, Client = ConfigurationManager.GetInstance().GetClientInfo() };
[923]118      wcfService.SendJobResultAsync(jobResult, true);
[804]119    }
120
[811]121    private void GetSnapshot(object jobId) {
122      long jId = (long)jobId;
[816]123      byte[] obj = engines[jId].GetSnapshot();
[811]124    }
125
[923]126    #endregion
127
128    #region wcfService Events
129
[924]130    void wcfService_ConnectionRestored(object sender, EventArgs e) {
131      //Do some fancy new things here... e.g: check all appdomains if there are still active Jobs that need to be transmitted
132    }
133
[923]134    void wcfService_LoginCompleted(object sender, LoginCompletedEventArgs e) {
135      if (e.Result.Success) {
136        Logging.GetInstance().Info(this.ToString(), "Login completed to Hive Server @ " + DateTime.Now);
137        ConfigurationManager.GetInstance().Loggedin();
138        Status.LoginTime = DateTime.Now;
139        Status.LoggedIn = true;
140      } else
141        Logging.GetInstance().Error(this.ToString(), e.Result.StatusMessage);
142    }   
143
144    void wcfService_PullJobCompleted(object sender, PullJobCompletedEventArgs e) {
[798]145      bool sandboxed = false;
146
[886]147      PluginManager.Manager.Initialize();
148      AppDomain appDomain =  PluginManager.Manager.CreateAndInitAppDomainWithSandbox(e.Result.JobId.ToString(), sandboxed, typeof(TestJob));
[882]149     
[830]150      appDomains.Add(e.Result.JobId, appDomain);
[798]151
152      Executor engine = (Executor)appDomain.CreateInstanceAndUnwrap(typeof(Executor).Assembly.GetName().Name, typeof(Executor).FullName);
[830]153      engine.JobId = e.Result.JobId;
[798]154      engine.Queue = MessageQueue.GetInstance();
[830]155      engine.Start(e.Result.SerializedJob);
156      engines.Add(e.Result.JobId, engine);
[798]157
158      Status.CurrentJobs++;
159
160      Debug.WriteLine("Increment CurrentJobs to:"+Status.CurrentJobs.ToString());
161    }
162
[923]163    void wcfService_SendJobResultCompleted(object sender, SendJobResultCompletedEventArgs e) {
[840]164      if (e.Result.Success) {
165        AppDomain.Unload(appDomains[e.Result.JobId]);
166        appDomains.Remove(e.Result.JobId);
167        engines.Remove(e.Result.JobId);
168        Status.CurrentJobs--;
169        Debug.WriteLine("Decrement CurrentJobs to:" + Status.CurrentJobs.ToString());
170      } else {
171        Debug.WriteLine("Job sending FAILED!");
172      }
[779]173    }
[908]174
[923]175    #endregion
176
[908]177    public Dictionary<long, Executor> GetExecutionEngines() {
178      return engines;
179    }
[714]180  }
181}
Note: See TracBrowser for help on using the repository browser.