source: trunk/CrypPlugins/P2PEditor/Distributed/JobListManager.cs @ 1545

Last change on this file since 1545 was 1545, checked in by Paul Lelgemann, 12 years ago

o CrypP2P: refactored P2PBase, removed race condition and unused code

File size: 3.4 KB
Line 
1using System.Collections.Generic;
2using System.IO;
3using System.Runtime.Serialization.Formatters.Binary;
4using Cryptool.P2P;
5using Cryptool.PluginBase;
6
7namespace Cryptool.P2PEditor.Distributed
8{
9    public class JobListManager
10    {
11        private const string JoblistKey = "Cryptool.P2PEditor.JobList";
12        private const string WorkspaceKeyPrefix = "Workspace";
13        private readonly P2PEditor _p2PEditor;
14
15        public JobListManager(P2PEditor p2PEditor)
16        {
17            _p2PEditor = p2PEditor;
18        }
19
20        public List<DistributedJob> JobList()
21        {
22            _p2PEditor.GuiLogMessage("Fetching DHT job list...", NotificationLevel.Debug);
23
24            if (!P2PManager.Instance.IsP2PConnected())
25            {
26                _p2PEditor.GuiLogMessage("P2P not connected, cannot fetch job list.", NotificationLevel.Error);
27                return new List<DistributedJob>();
28            }
29
30            byte[] serialisedJobList = P2PManager.Retrieve(JoblistKey);
31
32            if (serialisedJobList == null)
33            {
34                // no job list in DHT, create empty list
35                _p2PEditor.GuiLogMessage("No list in DHT, creating empty list.", NotificationLevel.Debug);
36                return new List<DistributedJob>();
37            }
38
39            var memoryStream = new MemoryStream(serialisedJobList);
40
41            var bformatter = new BinaryFormatter();
42            return (List<DistributedJob>) bformatter.Deserialize(memoryStream);
43        }
44
45        public void AddDistributedJob(DistributedJob distributedJob)
46        {
47            _p2PEditor.GuiLogMessage("Distributing new job...", NotificationLevel.Debug);
48
49            if (!P2PManager.Instance.IsP2PConnected())
50            {
51                _p2PEditor.GuiLogMessage("P2P not connected, cannot distribute job.", NotificationLevel.Error);
52                return;
53            }
54
55            List<DistributedJob> currentJobList = JobList();
56            currentJobList.Add(distributedJob);
57
58            var memoryStream = new MemoryStream();
59            var bformatter = new BinaryFormatter();
60
61            bformatter.Serialize(memoryStream, currentJobList);
62            P2PManager.Store(JoblistKey, memoryStream.ToArray());
63
64            byte[] workspaceData = File.ReadAllBytes(distributedJob.LocalFilePath);
65            _p2PEditor.GuiLogMessage(
66                "Workspace size: " + workspaceData.Length + ", storing at " + GenerateWorkspaceKey(distributedJob),
67                NotificationLevel.Debug);
68            P2PManager.Store(GenerateWorkspaceKey(distributedJob), workspaceData);
69
70            _p2PEditor.GuiLogMessage("Distributed job " + distributedJob.JobLabel, NotificationLevel.Info);
71        }
72
73        public void CompleteDistributedJob(DistributedJob distributedJob)
74        {
75            if (File.Exists(distributedJob.LocalFilePath))
76            {
77                _p2PEditor.GuiLogMessage("Local file exists, cannot complete job.", NotificationLevel.Error);
78                return;
79            }
80
81            distributedJob.ConvertRawWorkspaceToLocalFile(P2PManager.Retrieve(GenerateWorkspaceKey(distributedJob)));
82        }
83
84        private static string GenerateWorkspaceKey(DistributedJob distributedJob)
85        {
86            return string.Format("{0}.{1}.{2}", JoblistKey, WorkspaceKeyPrefix, distributedJob.JobGuid);
87        }
88    }
89}
Note: See TracBrowser for help on using the repository browser.