1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.IO;
|
---|
6 | using System.ComponentModel;
|
---|
7 | using HeuristicLab.BackgroundProcessing;
|
---|
8 | using HeuristicLab.OKB.Client;
|
---|
9 | using HeuristicLab.OKB.Cockpit.Admin.OKBData;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.OKB.Cockpit.Admin {
|
---|
12 | public class DataClientHelper {
|
---|
13 |
|
---|
14 | BackgroundWorker host;
|
---|
15 | public Action<int> ReportProgress { get; set; }
|
---|
16 | System.ComponentModel.DoWorkEventArgs args;
|
---|
17 |
|
---|
18 | public DataClientHelper(BackgroundWorker host, DoWorkEventArgs args) {
|
---|
19 | this.host = host;
|
---|
20 | this.args = args;
|
---|
21 | if (host.WorkerReportsProgress)
|
---|
22 | ReportProgress = i => host.ReportProgress(i);
|
---|
23 | else
|
---|
24 | ReportProgress = new Action<int>(i => { });
|
---|
25 | }
|
---|
26 |
|
---|
27 | public byte[] Load(OKBData.EntityType type, int id) {
|
---|
28 | DataServiceClient dataClient = ClientFactory.Create<DataServiceClient, IDataService>();
|
---|
29 | int size = dataClient.Request(type, id);
|
---|
30 | using (MemoryStream dataStream = new MemoryStream()) {
|
---|
31 | int chunkSize = Math.Max(100, Math.Min(1024 * 1024, size / 100));
|
---|
32 | while (!host.CancellationPending && dataStream.Position < size) {
|
---|
33 | byte[] chunk = dataClient.GetNextChunk(chunkSize);
|
---|
34 | dataStream.Write(chunk, 0, chunk.Length);
|
---|
35 | ReportProgress(100 * (int)dataStream.Position / size);
|
---|
36 | }
|
---|
37 | if (host.CancellationPending) {
|
---|
38 | args.Cancel = true;
|
---|
39 | dataClient.AbortTransfer();
|
---|
40 | dataClient.Close();
|
---|
41 | return null;
|
---|
42 | } else {
|
---|
43 | dataClient.TransferDone();
|
---|
44 | dataClient.Close();
|
---|
45 | ReportProgress(100);
|
---|
46 | return dataStream.ToArray();
|
---|
47 | }
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | public void Save(OKBData.EntityType type, int id, byte[] data) {
|
---|
52 | DataServiceClient dataClient = ClientFactory.Create<DataServiceClient, IDataService>();
|
---|
53 | dataClient.Submit(type, id);
|
---|
54 | using (MemoryStream dataStream = new MemoryStream(data)) {
|
---|
55 | int size = Math.Max(100, Math.Min(1024 * 1024, (int)dataStream.Length / 100));
|
---|
56 | for (int i = 0; i < dataStream.Length && !host.CancellationPending; i += size) {
|
---|
57 | byte[] chunk = new byte[Math.Min(size, dataStream.Length - dataStream.Position)];
|
---|
58 | dataStream.Read(chunk, 0, chunk.Length);
|
---|
59 | dataClient.SetNextChunk(chunk);
|
---|
60 | ReportProgress(100 * (int)dataStream.Position / (int)dataStream.Length);
|
---|
61 | }
|
---|
62 | if (host.CancellationPending) {
|
---|
63 | args.Cancel = true;
|
---|
64 | dataClient.AbortTransfer();
|
---|
65 | } else {
|
---|
66 | dataClient.TransferDone();
|
---|
67 | }
|
---|
68 | dataClient.Close();
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | }
|
---|
73 | }
|
---|