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

Last change on this file since 1602 was 1602, checked in by saternus, 12 years ago

mod. binaryformatter

File size: 4.8 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            bformatter.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
43            return (List<DistributedJob>) bformatter.Deserialize(memoryStream);
44        }
45
46        public void AddDistributedJob(DistributedJob distributedJob)
47        {
48            _p2PEditor.GuiLogMessage("Distributing new job...", NotificationLevel.Debug);
49
50            if (!P2PManager.Instance.IsP2PConnected())
51            {
52                _p2PEditor.GuiLogMessage("P2P not connected, cannot distribute job.", NotificationLevel.Error);
53                return;
54            }
55
56            var currentJobList = JobList();
57            currentJobList.Add(distributedJob);
58
59            var memoryStream = new MemoryStream();
60            var bformatter = new BinaryFormatter();
61            bformatter.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
62
63            bformatter.Serialize(memoryStream, currentJobList);
64            P2PManager.Store(JoblistKey, memoryStream.ToArray());
65
66            var workspaceData = File.ReadAllBytes(distributedJob.LocalFilePath);
67            _p2PEditor.GuiLogMessage(
68                "Workspace size: " + workspaceData.Length + ", storing at " + GenerateWorkspaceKey(distributedJob),
69                NotificationLevel.Debug);
70            P2PManager.Store(GenerateWorkspaceKey(distributedJob), workspaceData);
71
72            _p2PEditor.GuiLogMessage("Distributed job " + distributedJob.JobName, NotificationLevel.Info);
73        }
74
75        public void DeleteDistributedJob(DistributedJob distributedJobToDelete)
76        {
77            _p2PEditor.GuiLogMessage("Deleting job...", NotificationLevel.Debug);
78
79            if (!P2PManager.Instance.IsP2PConnected())
80            {
81                _p2PEditor.GuiLogMessage("P2P not connected, cannot distribute job.", NotificationLevel.Error);
82                return;
83            }
84
85            var currentJobList = JobList();
86            currentJobList.Remove(distributedJobToDelete);
87
88            var memoryStream = new MemoryStream();
89            var bformatter = new BinaryFormatter();
90            bformatter.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
91
92            bformatter.Serialize(memoryStream, currentJobList);
93            P2PManager.Store(JoblistKey, memoryStream.ToArray());
94
95            // Retrieve job first to satify versioned DHT
96            P2PManager.Retrieve(GenerateWorkspaceKey(distributedJobToDelete));
97            P2PManager.Remove(GenerateWorkspaceKey(distributedJobToDelete));
98
99            _p2PEditor.GuiLogMessage("Deleted distributed job " + distributedJobToDelete.JobName, NotificationLevel.Info);
100        }
101
102        public void CompleteDistributedJob(DistributedJob distributedJob)
103        {
104            if (File.Exists(distributedJob.LocalFilePath))
105            {
106                _p2PEditor.GuiLogMessage("Local file exists, cannot complete job.", NotificationLevel.Error);
107                return;
108            }
109
110            distributedJob.ConvertRawWorkspaceToLocalFile(P2PManager.Retrieve(GenerateWorkspaceKey(distributedJob)));
111        }
112
113        private static string GenerateWorkspaceKey(DistributedJob distributedJob)
114        {
115            return string.Format("{0}.{1}.{2}", JoblistKey, WorkspaceKeyPrefix, distributedJob.JobGuid);
116        }
117    }
118}
Note: See TracBrowser for help on using the repository browser.