Free cookie consent management tool by TermsFeed Policy Generator

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

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

finished implementation for #425

File size: 7.2 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 System.ServiceModel.Description;
41using HeuristicLab.Hive.Client.Core.ClientConsoleService;
42using HeuristicLab.Hive.Client.Core.ConfigurationManager;
43
44
45namespace HeuristicLab.Hive.Client.Core {
46  public class Core {
47
48    public delegate string GetASnapshotDelegate();
49
50    Dictionary<long, Executor> engines = new Dictionary<long, Executor>();
51    Dictionary<long, AppDomain> appDomains = new Dictionary<long, AppDomain>();
52
53    private WcfService wcfService;
54
55    public void Start() {
56
57      ClientConsoleServer server = new ClientConsoleServer();
58      server.StartClientConsoleServer(new Uri("net.tcp://127.0.0.1:8000/ClientConsole/"));
59
60      ConfigManager manager = ConfigManager.Instance;
61      manager.Core = this;
62
63      wcfService = WcfService.Instance;
64      ConnectionContainer cc = ConfigManager.Instance.GetServerIPAndPort();
65      if (cc.IPAdress == String.Empty || cc.Port == 0) {
66        wcfService.Connect("10.20.53.3", 9000);
67      }
68      wcfService.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(wcfService_LoginCompleted);
69      wcfService.PullJobCompleted += new EventHandler<PullJobCompletedEventArgs>(wcfService_PullJobCompleted);
70      wcfService.SendJobResultCompleted += new EventHandler<SendJobResultCompletedEventArgs>(wcfService_SendJobResultCompleted);
71      wcfService.ConnectionRestored += new EventHandler(wcfService_ConnectionRestored);
72      wcfService.ServerChanged += new EventHandler(wcfService_ServerChanged);
73
74      wcfService.LoginAsync(ConfigManager.Instance.GetClientInfo());
75
76      Heartbeat beat = new Heartbeat { Interval = 10000 };
77      beat.StartHeartbeat();     
78
79      MessageQueue queue = MessageQueue.GetInstance();
80      while (true) {
81        MessageContainer container = queue.GetMessage();
82        Debug.WriteLine("Main loop received this message: " + container.Message.ToString());
83        Logging.GetInstance().Info(this.ToString(), container.Message.ToString());
84        DetermineAction(container);
85      }
86    }
87
88    private void DetermineAction(MessageContainer container) {
89      switch (container.Message) {
90        case MessageContainer.MessageType.AbortJob:
91          engines[container.JobId].Abort();
92          break;
93        case MessageContainer.MessageType.JobAborted:
94          Debug.WriteLine("-- Job Aborted Message received");
95          break;
96
97        case MessageContainer.MessageType.RequestSnapshot:
98          engines[container.JobId].RequestSnapshot();
99          break;
100        case MessageContainer.MessageType.SnapshotReady:
101          Thread ssr = new Thread(new ParameterizedThreadStart(GetSnapshot));
102          ssr.Start(container.JobId);         
103          break;
104
105        case MessageContainer.MessageType.FetchJob:
106          wcfService.PullJobAsync(Guid.NewGuid());
107          break;         
108        case MessageContainer.MessageType.FinishedJob:
109          Thread finThread = new Thread(new ParameterizedThreadStart(GetFinishedJob));
110          finThread.Start(container.JobId);         
111          break;     
112      }
113    }
114
115    #region Async Threads for the EE
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 = ConfigManager.Instance.GetClientInfo() };
122      wcfService.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    #endregion
131
132    #region wcfService Events
133
134    void wcfService_ConnectionRestored(object sender, EventArgs e) {
135      //Do some fancy new things here... e.g: check all appdomains if there are still active Jobs that need to be transmitted
136    }
137
138    void wcfService_LoginCompleted(object sender, LoginCompletedEventArgs e) {
139      if (e.Result.Success) {
140        Logging.GetInstance().Info(this.ToString(), "Login completed to Hive Server @ " + DateTime.Now);       
141      } else
142        Logging.GetInstance().Error(this.ToString(), e.Result.StatusMessage);
143    }   
144
145    void wcfService_PullJobCompleted(object sender, PullJobCompletedEventArgs e) {
146      bool sandboxed = false;
147
148      PluginManager.Manager.Initialize();
149      AppDomain appDomain =  PluginManager.Manager.CreateAndInitAppDomainWithSandbox(e.Result.JobId.ToString(), sandboxed, typeof(TestJob));
150     
151      appDomains.Add(e.Result.JobId, appDomain);
152
153      Executor engine = (Executor)appDomain.CreateInstanceAndUnwrap(typeof(Executor).Assembly.GetName().Name, typeof(Executor).FullName);
154      engine.JobId = e.Result.JobId;
155      engine.Queue = MessageQueue.GetInstance();
156      engine.Start(e.Result.SerializedJob);
157      engines.Add(e.Result.JobId, engine);
158
159      ClientStatusInfo.JobsFetched++;
160
161      Debug.WriteLine("Increment FetchedJobs to:"+ClientStatusInfo.JobsFetched);
162    }
163
164    void wcfService_SendJobResultCompleted(object sender, SendJobResultCompletedEventArgs e) {
165      if (e.Result.Success) {
166        AppDomain.Unload(appDomains[e.Result.JobId]);
167        appDomains.Remove(e.Result.JobId);
168        engines.Remove(e.Result.JobId);
169        ClientStatusInfo.JobsProcessed++;
170        Debug.WriteLine("ProcessedJobs to:" + ClientStatusInfo.JobsProcessed);
171      } else {
172        Debug.WriteLine("Job sending FAILED!");
173      }
174    }
175
176    void wcfService_ServerChanged(object sender, EventArgs e) {
177      foreach(KeyValuePair<long, AppDomain> entries in appDomains)
178        AppDomain.Unload(appDomains[entries.Key]);
179      appDomains = new Dictionary<long, AppDomain>();
180      engines = new Dictionary<long, Executor>();
181    }
182
183
184    #endregion
185
186    public Dictionary<long, Executor> GetExecutionEngines() {
187      return engines;
188    }
189  }
190}
Note: See TracBrowser for help on using the repository browser.