Free cookie consent management tool by TermsFeed Policy Generator

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

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

forgot to check in a class file (#358)

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