Free cookie consent management tool by TermsFeed Policy Generator

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

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

added licence information and comments (#467)

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