Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 813 was 793, checked in by whackl, 16 years ago

Removing and Refactoring MessageContainer (#379)

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;
30using HeuristicLab.Hive.Contracts;
31
32namespace HeuristicLab.Hive.Client.ExecutionEngine {
33  public class Executor: MarshalByRefObject {
34    public long JobId { get; set; }
35    public IJob Job { get; set; }
36    public MessageContainer.MessageType CurrentMessage { get; set; }
37    public MessageQueue Queue { get; set; }
38
39    public void Start() {
40      Job.JobStopped += new EventHandler(Job_JobStopped);
41      Job.Start();
42    }
43
44    public void Abort() {
45      CurrentMessage = MessageContainer.MessageType.AbortJob;
46      Job.Stop();     
47    }
48
49    void Job_JobStopped(object sender, EventArgs e) {
50      if (CurrentMessage == MessageContainer.MessageType.NoMessage)
51        Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.FinishedJob, JobId));
52      else if (CurrentMessage == MessageContainer.MessageType.RequestSnapshot)
53        Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.SnapshotReady, JobId));
54      else if (CurrentMessage == MessageContainer.MessageType.AbortJob)
55        Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.JobAborted, JobId));
56    }
57
58    public String GetSnapshot() {
59      //if the job is still running, something went VERY bad.
60      if (Job.Running) {
61        return null;
62      } else {
63        // Clear the Status message
64        CurrentMessage = MessageContainer.MessageType.NoMessage;
65        // Pack the whole job inside an xml document
66        String job = SerializeJobObject();       
67        // Restart the job
68        Job.Start();
69        // Return the Snapshot
70        return job;
71      }
72    }
73
74    public String GetFinishedJob() {
75      //Job isn't finished!
76      if (Job.Running) {
77        return null;
78      } else {
79        return SerializeJobObject();
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.