Free cookie consent management tool by TermsFeed Policy Generator

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

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

login bugfixing for (#401)

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