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