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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Hive.Client.Common;
|
---|
27 | using System.Xml;
|
---|
28 | using System.Xml.Serialization;
|
---|
29 | using System.IO;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Hive.Client.ExecutionEngine {
|
---|
32 | public class Executor: MarshalByRefObject {
|
---|
33 | public long JobId { get; set; }
|
---|
34 | public IJob 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 String 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 | // Restart the job
|
---|
67 | Job.Start();
|
---|
68 | // Return the Snapshot
|
---|
69 | return job;
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | public String GetFinishedJob() {
|
---|
74 | //Job isn't finished!
|
---|
75 | if (Job.Running) {
|
---|
76 | return null;
|
---|
77 | } else {
|
---|
78 | return SerializeJobObject();
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | public void RequestSnapshot() {
|
---|
84 | CurrentMessage = MessageContainer.MessageType.RequestSnapshot;
|
---|
85 | Job.Stop();
|
---|
86 | }
|
---|
87 |
|
---|
88 | private String SerializeJobObject() {
|
---|
89 | XmlSerializer serializer = new XmlSerializer(typeof(TestJob));
|
---|
90 | MemoryStream ms = new MemoryStream();
|
---|
91 | serializer.Serialize(ms, Job);
|
---|
92 | StreamReader reader = new StreamReader(ms);
|
---|
93 | return reader.ReadToEnd();
|
---|
94 | }
|
---|
95 |
|
---|
96 | private void RestoreJobObject(String serializedJob) {
|
---|
97 | System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
|
---|
98 | XmlSerializer serializer = new XmlSerializer(typeof(TestJob));
|
---|
99 | MemoryStream ms = new MemoryStream();
|
---|
100 | ms.Write(encoding.GetBytes(serializedJob), 0, serializedJob.Length);
|
---|
101 | Job = (TestJob) serializer.Deserialize(ms);
|
---|
102 | }
|
---|
103 |
|
---|
104 | public Executor() {
|
---|
105 | CurrentMessage = MessageContainer.MessageType.NoMessage;
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|