Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ProgressClasses.cs « Main « Library « Duplicati - github.com/duplicati/duplicati.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 00886ae3418d0ce13014b99a57150e1a8019ae30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//  Copyright (C) 2013, Duplicati Team

//  http://www.duplicati.com, info@duplicati.com
//
//  This library is free software; you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as
//  published by the Free Software Foundation; either version 2.1 of the
//  License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful, but
//  WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;

namespace Duplicati.Library.Main
{
    /// <summary>
    /// Interface for recieving messages from the Duplicati operations
    /// </summary>
    public interface IMessageSink
    {
        void BackendEvent(BackendActionType action, BackendEventType type, string path, long size);
        void VerboseEvent(string message, object[] args);
        void MessageEvent(string message);
        void RetryEvent(string message, Exception ex);
        void WarningEvent(string message, Exception ex);
        void ErrorEvent(string message, Exception ex);
        void DryrunEvent(string message);
        
        /// <summary>
        /// Sets the backend progress update object
        /// </summary>
        /// <value>The backend progress update object</value>
        IBackendProgress BackendProgress { set; }
        
        /// <summary>
        /// Sets the operation progress update object
        /// </summary>
        /// <value>The operation progress update object</value>
        IOperationProgress OperationProgress { set; }
    }
    
    /// <summary>
    /// Backend progress update object.
    /// The engine updates these statistics very often,
    /// so an event based system would take up too many resources.
    /// Instead, this interface allows the client to poll
    /// for updates as often as desired.
    /// </summary>
    public interface IBackendProgress
    {
        /// <summary>
        /// Update with the current action, path, size, progress and bytes_pr_second.
        /// </summary>
        /// <param name="action">The current action</param>
        /// <param name="path">The current path</param>
        /// <param name="size">The current size</param>
        /// <param name="progress">The current number of transferred bytes</param>
        /// <param name="bytes_pr_second">Transfer speed in bytes pr second, -1 for unknown</param>
        void Update(out BackendActionType action, out string path, out long size, out long progress, out long bytes_pr_second);
    }
    
    /// <summary>
    /// Interface for updating the backend progress
    /// </summary>
    internal interface IBackendProgressUpdater
    {
        /// <summary>
        /// Register the start of a new action
        /// </summary>
        /// <param name="action">The action that is starting</param>
        /// <param name="path">The path being operated on</param>
        /// <param name="size">The size of the file being transferred</param>
        void StartAction(BackendActionType action, string path, long size);
        /// <summary>
        /// Updates the current progress
        /// </summary>
        /// <param name="progress">he current number of transferred bytes</param>
        void UpdateProgress(long progress);
    }
    
    /// <summary>
    /// Combined interface for the backend progress updater and the backend progress item
    /// </summary>
    internal interface IBackendProgressUpdaterAndReporter : IBackendProgressUpdater, IBackendProgress
    {
    }
    
    /// <summary>
    /// Backend progress updater instance
    /// </summary>
    internal class BackendProgressUpdater : IBackendProgressUpdaterAndReporter
    {
        /// <summary>
        /// Lock object to provide snapshot-like access to the data
        /// </summary>
        private readonly object m_lock = new object();
        /// <summary>
        /// The current action
        /// </summary>
        private BackendActionType m_action;
        /// <summary>
        /// The current path
        /// </summary>
        private string m_path;
        /// <summary>
        /// The current file size
        /// </summary>
        private long m_size;
        /// <summary>
        /// The current number of transferred bytes
        /// </summary>
        private long m_progress;
        /// <summary>
        /// The time the last action started
        /// </summary>
        private DateTime m_actionStart;
        
        /// <summary>
        /// Register the start of a new action
        /// </summary>
        /// <param name="action">The action that is starting</param>
        /// <param name="path">The path being operated on</param>
        /// <param name="size">The size of the file being transferred</param>
        public void StartAction(BackendActionType action, string path, long size)
        {
            lock(m_lock)
            {
                m_action = action;
                m_path = path;
                m_size = size;
                m_progress = 0;
                m_actionStart = DateTime.Now;                
            }
        }
        
        /// <summary>
        /// Updates the progress
        /// </summary>
        /// <param name="progress">The current number of transferred bytes</param>
        public void UpdateProgress(long progress)
        {
            lock(m_lock)
                m_progress = progress;
        }
        
        /// <summary>
        /// Update with the current action, path, size, progress and bytes_pr_second.
        /// </summary>
        /// <param name="action">The current action</param>
        /// <param name="path">The current path</param>
        /// <param name="size">The current size</param>
        /// <param name="progress">The current number of transferred bytes</param>
        /// <param name="bytes_pr_second">Transfer speed in bytes pr second, -1 for unknown</param>
        public void Update(out BackendActionType action, out string path, out long size, out long progress, out long bytes_pr_second)
        {
            lock(m_lock)
            {
                action = m_action;
                path = m_path;
                size = m_size;
                progress = m_progress;
                
                //TODO: The speed should be more dynamic,
                // so we need a sample window instead of always 
                // calculating from the beginning
                if (m_progress <= 0 || m_size <= 0 || m_actionStart.Ticks == 0)
                    bytes_pr_second = -1;
                else
                    bytes_pr_second = (long)(m_progress / (DateTime.Now - m_actionStart).TotalSeconds);
            }
        }
        
    }
    
    public delegate void PhaseChangedDelegate(OperationPhase phase, OperationPhase previousPhase);
    
    /// <summary>
    /// Operation progress update object.
    /// The engine updates these statistics very often,
    /// so an event based system would take up too many resources.
    /// Instead, this interface allows the client to poll
    /// for updates as often as desired.
    /// </summary>
    public interface IOperationProgress
    {
        /// <summary>
        /// Update the phase, progress, filesprocessed, filesizeprocessed, filecount, filesize and countingfiles.
        /// </summary>
        /// <param name="phase">Phase.</param>
        /// <param name="progress">Progress.</param>
        /// <param name="filesprocessed">Filesprocessed.</param>
        /// <param name="filesizeprocessed">Filesizeprocessed.</param>
        /// <param name="filecount">Filecount.</param>
        /// <param name="filesize">Filesize.</param>
        /// <param name="countingfiles">True if the filecount and filesize is incomplete, false otherwise</param>
        void UpdateOverall(out OperationPhase phase, out float progress, out long filesprocessed, out long filesizeprocessed, out long filecount, out long filesize, out bool countingfiles);
        
        /// <summary>
        /// Update the filename, filesize, and fileoffset.
        /// </summary>
        /// <param name="filename">Filename.</param>
        /// <param name="filesize">Filesize.</param>
        /// <param name="fileoffset">Fileoffset.</param>
        void UpdateFile(out string filename, out long filesize, out long fileoffset);
        
        /// <summary>
        /// Occurs when the phase has changed
        /// </summary>
        event PhaseChangedDelegate PhaseChanged;
    }
    
    /// <summary>
    /// Interface for updating the backend progress
    /// </summary>
    internal interface IOperationProgressUpdater
    {
        void UpdatePhase(OperationPhase phase);
        void UpdateProgress(float progress);
        void StartFile(string filename, long size);
        void UpdateFileProgress(long offset);
        void UpdatefileCount(long filecount, long filesize, bool done);
        void UpdatefilesProcessed(long count, long size);
    }
    
    internal interface IOperationProgressUpdaterAndReporter : IOperationProgressUpdater, IOperationProgress
    {
    }
    
    internal class OperationProgressUpdater : IOperationProgressUpdaterAndReporter
    {
        private readonly object m_lock = new object();
        
        private OperationPhase m_phase;
        private float m_progress;
        private string m_curfilename;
        private long m_curfilesize;
        private long m_curfileoffset;
        
        private long m_filesprocessed;
        private long m_filesizeprocessed;
        private long m_filecount;
        private long m_filesize;
        
        private bool m_countingFiles;
        
        public event PhaseChangedDelegate PhaseChanged;
        
        public void UpdatePhase(OperationPhase phase)
        {
            OperationPhase prev_phase;
            lock(m_lock)
            {
                prev_phase = m_phase;
                m_phase = phase;
                m_curfilename = null;
                m_curfilesize = 0;
                m_curfileoffset = 0;
            }
            
            if (prev_phase != phase && PhaseChanged != null)
                PhaseChanged(phase, prev_phase);
        }
        
        public void UpdateProgress(float progress)
        {
            lock(m_lock)
                m_progress = progress;
        }
        
        public void StartFile(string filename, long size)
        {
            lock(m_lock)
            {
                m_curfilename = filename;
                m_curfilesize = size;
                m_curfileoffset = 0;
            }
        }
        
        public void UpdateFileProgress(long offset)
        {
            lock(m_lock)
                m_curfileoffset = offset;
        }
        
        public void UpdatefileCount(long filecount, long filesize, bool done)
        {
            lock(m_lock)
            {
                m_filecount = filecount;
                m_filesize = filesize;
                m_countingFiles = !done;
            }
        }
        
        public void UpdatefilesProcessed(long count, long size)
        {
            lock(m_lock)
            {
                m_filesprocessed = count;
                m_filesizeprocessed = size;
            }
        }
        
        /// <summary>
        /// Update the phase, progress, filesprocessed, filesizeprocessed, filecount, filesize and countingfiles.
        /// </summary>
        /// <param name="phase">Phase.</param>
        /// <param name="progress">Progress.</param>
        /// <param name="filesprocessed">Filesprocessed.</param>
        /// <param name="filesizeprocessed">Filesizeprocessed.</param>
        /// <param name="filecount">Filecount.</param>
        /// <param name="filesize">Filesize.</param>
        /// <param name="countingfiles">True if the filecount and filesize is incomplete, false otherwise</param>
        public void UpdateOverall(out OperationPhase phase, out float progress, out long filesprocessed, out long filesizeprocessed, out long filecount, out long filesize, out bool countingfiles)
        {
            lock(m_lock)
            {
                phase = m_phase;
                filesize = m_filesize;
                progress = m_progress;
                filesprocessed = m_filesprocessed;
                filesizeprocessed = m_filesizeprocessed;
                filecount = m_filecount;
                countingfiles = m_countingFiles;
            }
        }
        
        /// <summary>
        /// Update the filename, filesize, and fileoffset.
        /// </summary>
        /// <param name="filename">Filename.</param>
        /// <param name="filesize">Filesize.</param>
        /// <param name="fileoffset">Fileoffset.</param>
        public void UpdateFile(out string filename, out long filesize, out long fileoffset)
        {
            lock(m_lock)
            {
                filename = m_curfilename;
                filesize = m_curfilesize;
                fileoffset = m_curfileoffset;
            }
        }
    }
}