Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Client.ExecutionEngine/Executor.cs @ 885

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

continued work from #390, removed a fake CreateJob method and replaced all fake Jobs with real ones.

File size: 3.4 KB
RevLine 
[771]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.Common;
27using System.Xml;
28using System.Xml.Serialization;
29using System.IO;
[793]30using HeuristicLab.Hive.Contracts;
[816]31using HeuristicLab.Core;
[771]32
33namespace HeuristicLab.Hive.Client.ExecutionEngine {
34  public class Executor: MarshalByRefObject {
35    public long JobId { get; set; }
[779]36    public IJob Job { get; set; }
[771]37    public MessageContainer.MessageType CurrentMessage { get; set; }
38    public MessageQueue Queue { get; set; }
39
[830]40    public void Start(byte[] serializedJob) {     
41      Job = (IJob)PersistenceManager.RestoreFromGZip(serializedJob);
[771]42      Job.JobStopped += new EventHandler(Job_JobStopped);
43      Job.Start();
44    }
45
46    public void Abort() {
47      CurrentMessage = MessageContainer.MessageType.AbortJob;
48      Job.Stop();     
49    }
50
51    void Job_JobStopped(object sender, EventArgs e) {
52      if (CurrentMessage == MessageContainer.MessageType.NoMessage)
53        Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.FinishedJob, JobId));
54      else if (CurrentMessage == MessageContainer.MessageType.RequestSnapshot)
55        Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.SnapshotReady, JobId));
56      else if (CurrentMessage == MessageContainer.MessageType.AbortJob)
57        Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.JobAborted, JobId));
58    }
59
[816]60    public byte[] GetSnapshot() {
[771]61      //if the job is still running, something went VERY bad.
62      if (Job.Running) {
63        return null;
64      } else {
65        // Clear the Status message
66        CurrentMessage = MessageContainer.MessageType.NoMessage;
67        // Pack the whole job inside an xml document
[816]68        byte[] job = SerializeJobObject();       
[771]69        // Restart the job
70        Job.Start();
71        // Return the Snapshot
[779]72        return job;
[771]73      }
74    }
75
[816]76    public byte[] GetFinishedJob() {
[771]77      //Job isn't finished!
78      if (Job.Running) {
79        return null;
80      } else {
[779]81        return SerializeJobObject();
[771]82      }
83    }
84
85
86    public void RequestSnapshot() {
87      CurrentMessage = MessageContainer.MessageType.RequestSnapshot;
88      Job.Stop();
89    }
90
[816]91    private byte[] SerializeJobObject() {
92      return PersistenceManager.SaveToGZip(Job);
[771]93    }
94
[816]95    private void RestoreJobObject(byte[] sjob) {
96      Job = (IJob)PersistenceManager.RestoreFromGZip(sjob);
[771]97    }
98
99    public Executor() {
100      CurrentMessage = MessageContainer.MessageType.NoMessage;
[816]101      Job = new TestJob();
[771]102    }   
103  }
104}
Note: See TracBrowser for help on using the repository browser.