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 | using HeuristicLab.Hive.Contracts;
|
---|
31 | using HeuristicLab.Core;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Hive.Client.ExecutionEngine {
|
---|
34 | public class Executor: MarshalByRefObject {
|
---|
35 | public long JobId { get; set; }
|
---|
36 | public IJob Job { get; set; }
|
---|
37 | public MessageContainer.MessageType CurrentMessage { get; set; }
|
---|
38 | public MessageQueue Queue { get; set; }
|
---|
39 |
|
---|
40 | public void Start(byte[] serializedJob) {
|
---|
41 | Job = (IJob)PersistenceManager.RestoreFromGZip(serializedJob);
|
---|
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 |
|
---|
60 | public byte[] GetSnapshot() {
|
---|
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
|
---|
68 | byte[] job = SerializeJobObject();
|
---|
69 | // Restart the job
|
---|
70 | Job.Start();
|
---|
71 | // Return the Snapshot
|
---|
72 | return job;
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | public byte[] GetFinishedJob() {
|
---|
77 | //Job isn't finished!
|
---|
78 | if (Job.Running) {
|
---|
79 | return null;
|
---|
80 | } else {
|
---|
81 | return SerializeJobObject();
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | public void RequestSnapshot() {
|
---|
87 | CurrentMessage = MessageContainer.MessageType.RequestSnapshot;
|
---|
88 | Job.Stop();
|
---|
89 | }
|
---|
90 |
|
---|
91 | private byte[] SerializeJobObject() {
|
---|
92 | return PersistenceManager.SaveToGZip(Job);
|
---|
93 | }
|
---|
94 |
|
---|
95 | private void RestoreJobObject(byte[] sjob) {
|
---|
96 | Job = (IJob)PersistenceManager.RestoreFromGZip(sjob);
|
---|
97 | }
|
---|
98 |
|
---|
99 | public Executor() {
|
---|
100 | CurrentMessage = MessageContainer.MessageType.NoMessage;
|
---|
101 | Job = new TestJob();
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|