Free cookie consent management tool by TermsFeed Policy Generator

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

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

cont on #401

File size: 6.3 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 HeuristicLab.Hive.Client.Communication.Interfaces;
41using System.ServiceModel.Description;
[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
[798]52    private ClientCommunicatorClient clientCommunicator;
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
[798]59      clientCommunicator = ServiceLocator.GetClientCommunicator();
[790]60      clientCommunicator.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(ClientCommunicator_LoginCompleted);
[808]61      clientCommunicator.PullJobCompleted += new EventHandler<PullJobCompletedEventArgs>(ClientCommunicator_PullJobCompleted);
[830]62      clientCommunicator.SendJobResultCompleted += new EventHandler<SendJobResultCompletedEventArgs>(ClientCommunicator_SendJobResultCompleted);
[882]63      //clientCommunicator.LoginAsync(ConfigurationManager.GetInstance().GetClientInfo());
[790]64
[900]65      Heartbeat beat = new Heartbeat { Interval = 10000 };
[841]66      beat.StartHeartbeat();     
67
[735]68      MessageQueue queue = MessageQueue.GetInstance();
69      while (true) {
70        MessageContainer container = queue.GetMessage();
[779]71        Debug.WriteLine("Main loop received this message: " + container.Message.ToString());
[790]72        Logging.GetInstance().Info(this.ToString(), container.Message.ToString());
[768]73        DetermineAction(container);
[735]74      }
75    }
[768]76
[798]77    void ClientCommunicator_LoginCompleted(object sender, LoginCompletedEventArgs e) {
[790]78      if (e.Result.Success) {
79        Logging.GetInstance().Info(this.ToString(), "Login completed to Hive Server @ " + DateTime.Now);
[841]80        ConfigurationManager.GetInstance().Loggedin();
[790]81        Status.LoginTime = DateTime.Now;
82        Status.LoggedIn = true;
83      } else
84        Logging.GetInstance().Error(this.ToString(), e.Result.StatusMessage);
85    }
86
[768]87    private void DetermineAction(MessageContainer container) {
[779]88      switch (container.Message) {
89        case MessageContainer.MessageType.AbortJob:
90          engines[container.JobId].Abort();
91          break;
92        case MessageContainer.MessageType.JobAborted:
93          Debug.WriteLine("-- Job Aborted Message received");
94          break;
[790]95
[779]96        case MessageContainer.MessageType.RequestSnapshot:
97          engines[container.JobId].RequestSnapshot();
98          break;
99        case MessageContainer.MessageType.SnapshotReady:
[811]100          Thread ssr = new Thread(new ParameterizedThreadStart(GetSnapshot));
101          ssr.Start(container.JobId);         
[779]102          break;
[790]103
[811]104        case MessageContainer.MessageType.FetchJob:
[798]105          clientCommunicator.PullJobAsync(Guid.NewGuid());
[811]106          break;         
[779]107        case MessageContainer.MessageType.FinishedJob:
[811]108          Thread finThread = new Thread(new ParameterizedThreadStart(GetFinishedJob));
109          finThread.Start(container.JobId);         
110          break;     
[779]111      }
112    }
[790]113
[811]114    private void GetFinishedJob(object jobId) {
115      long jId = (long)jobId;
[830]116      byte[] sJob = engines[jId].GetFinishedJob();
[816]117     
[841]118      JobResult jobResult = new JobResult { JobId = jId, Result = sJob, Client = ConfigurationManager.GetInstance().GetClientInfo() };
[830]119      clientCommunicator.SendJobResultAsync(jobResult, true);
[804]120    }
121
[811]122    private void GetSnapshot(object jobId) {
123      long jId = (long)jobId;
[816]124      byte[] obj = engines[jId].GetSnapshot();
[811]125    }
126
[798]127    void ClientCommunicator_PullJobCompleted(object sender, PullJobCompletedEventArgs e) {
128      bool sandboxed = false;
129
[886]130      PluginManager.Manager.Initialize();
131      AppDomain appDomain =  PluginManager.Manager.CreateAndInitAppDomainWithSandbox(e.Result.JobId.ToString(), sandboxed, typeof(TestJob));
[882]132     
[830]133      appDomains.Add(e.Result.JobId, appDomain);
[798]134
135      Executor engine = (Executor)appDomain.CreateInstanceAndUnwrap(typeof(Executor).Assembly.GetName().Name, typeof(Executor).FullName);
[830]136      engine.JobId = e.Result.JobId;
[798]137      engine.Queue = MessageQueue.GetInstance();
[830]138      engine.Start(e.Result.SerializedJob);
139      engines.Add(e.Result.JobId, engine);
[798]140
141      Status.CurrentJobs++;
142
143      Debug.WriteLine("Increment CurrentJobs to:"+Status.CurrentJobs.ToString());
144    }
145
[840]146    void ClientCommunicator_SendJobResultCompleted(object sender, SendJobResultCompletedEventArgs e) {
147      if (e.Result.Success) {
148        AppDomain.Unload(appDomains[e.Result.JobId]);
149        appDomains.Remove(e.Result.JobId);
150        engines.Remove(e.Result.JobId);
151        Status.CurrentJobs--;
152        Debug.WriteLine("Decrement CurrentJobs to:" + Status.CurrentJobs.ToString());
153      } else {
154        Debug.WriteLine("Job sending FAILED!");
155      }
[779]156    }
[714]157  }
158}
Note: See TracBrowser for help on using the repository browser.