Free cookie consent management tool by TermsFeed Policy Generator

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

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

worked on #410

File size: 7.1 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       /*DiscoveryService discService =
56        new DiscoveryService();
57      IClientConsoleCommunicator[] clientCommunicatorInstances =
58        discService.GetInstances<IClientConsoleCommunicator>();
59
60      if (clientCommunicatorInstances.Length > 0) {
61        ServiceHost serviceHost =
62                new ServiceHost(clientCommunicatorInstances[0].GetType(),
63                  new Uri("http://localhost:9000/ClientConsole"));
64
65        System.ServiceModel.Channels.Binding binding =
66          new NetNamedPipeBinding();
67
68        serviceHost.AddServiceEndpoint(
69          typeof(IClientConsoleCommunicator),
70              binding,
71              "ClientConsoleCommunicator");
72
73        ServiceMetadataBehavior behavior =
74              new ServiceMetadataBehavior();
75        serviceHost.Description.Behaviors.Add(behavior);
76
77        serviceHost.AddServiceEndpoint(
78            typeof(IMetadataExchange),
79            MetadataExchangeBindings.CreateMexNamedPipeBinding(),
80            "mex");
81
82        serviceHost.Open();
83      }*/
84
85      clientCommunicator = ServiceLocator.GetClientCommunicator();
86      clientCommunicator.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(ClientCommunicator_LoginCompleted);
87      clientCommunicator.PullJobCompleted += new EventHandler<PullJobCompletedEventArgs>(ClientCommunicator_PullJobCompleted);
88      clientCommunicator.SendJobResultCompleted += new EventHandler<SendJobResultCompletedEventArgs>(ClientCommunicator_SendJobResultCompleted);
89      //clientCommunicator.LoginAsync(ConfigurationManager.GetInstance().GetClientInfo());
90
91      Heartbeat beat = new Heartbeat { Interval = 30000 };
92      beat.StartHeartbeat();     
93
94      MessageQueue queue = MessageQueue.GetInstance();
95      while (true) {
96        MessageContainer container = queue.GetMessage();
97        Debug.WriteLine("Main loop received this message: " + container.Message.ToString());
98        Logging.GetInstance().Info(this.ToString(), container.Message.ToString());
99        DetermineAction(container);
100      }
101    }
102
103    void ClientCommunicator_LoginCompleted(object sender, LoginCompletedEventArgs e) {
104      if (e.Result.Success) {
105        Logging.GetInstance().Info(this.ToString(), "Login completed to Hive Server @ " + DateTime.Now);
106        ConfigurationManager.GetInstance().Loggedin();
107        Status.LoginTime = DateTime.Now;
108        Status.LoggedIn = true;
109      } else
110        Logging.GetInstance().Error(this.ToString(), e.Result.StatusMessage);
111    }
112
113    private void DetermineAction(MessageContainer container) {
114      switch (container.Message) {
115        case MessageContainer.MessageType.AbortJob:
116          engines[container.JobId].Abort();
117          break;
118        case MessageContainer.MessageType.JobAborted:
119          Debug.WriteLine("-- Job Aborted Message received");
120          break;
121
122        case MessageContainer.MessageType.RequestSnapshot:
123          engines[container.JobId].RequestSnapshot();
124          break;
125        case MessageContainer.MessageType.SnapshotReady:
126          Thread ssr = new Thread(new ParameterizedThreadStart(GetSnapshot));
127          ssr.Start(container.JobId);         
128          break;
129
130        case MessageContainer.MessageType.FetchJob:
131          clientCommunicator.PullJobAsync(Guid.NewGuid());
132          break;         
133        case MessageContainer.MessageType.FinishedJob:
134          Thread finThread = new Thread(new ParameterizedThreadStart(GetFinishedJob));
135          finThread.Start(container.JobId);         
136          break;     
137      }
138    }
139
140    private void GetFinishedJob(object jobId) {
141      long jId = (long)jobId;
142      byte[] sJob = engines[jId].GetFinishedJob();
143     
144      JobResult jobResult = new JobResult { JobId = jId, Result = sJob, Client = ConfigurationManager.GetInstance().GetClientInfo() };
145      clientCommunicator.SendJobResultAsync(jobResult, true);
146    }
147
148    private void GetSnapshot(object jobId) {
149      long jId = (long)jobId;
150      byte[] obj = engines[jId].GetSnapshot();
151    }
152
153    void ClientCommunicator_PullJobCompleted(object sender, PullJobCompletedEventArgs e) {
154      bool sandboxed = false;
155
156      PluginManager.Manager.Initialize();
157      AppDomain appDomain =  PluginManager.Manager.CreateAndInitAppDomainWithSandbox(e.Result.JobId.ToString(), sandboxed, typeof(TestJob));
158     
159      appDomains.Add(e.Result.JobId, appDomain);
160
161      Executor engine = (Executor)appDomain.CreateInstanceAndUnwrap(typeof(Executor).Assembly.GetName().Name, typeof(Executor).FullName);
162      engine.JobId = e.Result.JobId;
163      engine.Queue = MessageQueue.GetInstance();
164      engine.Start(e.Result.SerializedJob);
165      engines.Add(e.Result.JobId, engine);
166
167      Status.CurrentJobs++;
168
169      Debug.WriteLine("Increment CurrentJobs to:"+Status.CurrentJobs.ToString());
170    }
171
172    void ClientCommunicator_SendJobResultCompleted(object sender, SendJobResultCompletedEventArgs e) {
173      if (e.Result.Success) {
174        AppDomain.Unload(appDomains[e.Result.JobId]);
175        appDomains.Remove(e.Result.JobId);
176        engines.Remove(e.Result.JobId);
177        Status.CurrentJobs--;
178        Debug.WriteLine("Decrement CurrentJobs to:" + Status.CurrentJobs.ToString());
179      } else {
180        Debug.WriteLine("Job sending FAILED!");
181      }
182    }
183  }
184}
Note: See TracBrowser for help on using the repository browser.