Free cookie consent management tool by TermsFeed Policy Generator

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

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

implemented the Status method for the web interface (#401)

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