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

AsyncDownloader.cs « Main « Library « Duplicati - github.com/duplicati/duplicati.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 550b00feb64b08d799dd5b5108e6726dc5fb1d47 (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Duplicati.Library.Main.Database;

namespace Duplicati.Library.Main
{
	internal interface IAsyncDownloadedFile : IRemoteVolume
	{
		Library.Utility.TempFile TempFile { get; }
	}

    internal class AsyncDownloader : IEnumerable<IAsyncDownloadedFile>
    {
        private class AsyncDownloaderEnumerator : IEnumerator<IAsyncDownloadedFile>
        {
        	private class AsyncDownloadedFile : IAsyncDownloadedFile
        	{
        		private Exception m_exception;
        		private Library.Utility.TempFile m_file;
        		
        		public string Name { get; private set; }
        		public string Hash { get; private set; }
        		public long Size { get; private set; }
        		
        		public void DisposeTempFile()
				{
					if (m_file != null)
						try { m_file.Dispose(); }
						finally { m_file = null; }
				}
				

        		public Library.Utility.TempFile TempFile
        		{
        			get
        			{
        				if (m_exception != null)
        					throw m_exception;

        				return m_file;
        			}
        		}
        		
        		public AsyncDownloadedFile(string name, string hash, long size, Library.Utility.TempFile tempfile, Exception exception)
        		{
        			this.Name = name;
        			this.Hash = hash;
        			this.Size = size;
        			m_exception = exception;
        			m_file = tempfile;
        		}
        	}
        
            private IList<IRemoteVolume> m_volumes;
            private BackendManager.IDownloadWaitHandle m_handle;
            private BackendManager m_backend;
            private int m_index;
            private AsyncDownloadedFile m_current;

            public AsyncDownloaderEnumerator(IList<IRemoteVolume> volumes, BackendManager backend)
            {
                m_volumes = volumes;
                m_backend = backend;
                m_index = 0;
            }

            public IAsyncDownloadedFile Current
            {
                get { return m_current; }
            }

            public void Dispose()
            {
                if (m_current != null)
                {
                    m_current.DisposeTempFile();
                    m_current = null;
                }
            }

            object System.Collections.IEnumerator.Current
            {
                get { return this.Current; }
            }

            public bool MoveNext()
			{
				if (m_current != null)
				{
					m_current.DisposeTempFile();
					m_current = null;
				}

				if (m_index >= m_volumes.Count)
					return false;

				if (m_handle == null)
					m_handle = m_backend.GetAsync(m_volumes[m_index].Name, m_volumes[m_index].Size, m_volumes[m_index].Hash);
                
				string hash = null;
				long size = -1;
				Library.Utility.TempFile file = null;
				Exception exception = null;
				try
				{
					file = m_handle.Wait(out hash, out size);
	                
				}
				catch (Exception ex)
				{
					exception = ex;
				}
				
                m_current = new AsyncDownloadedFile(m_volumes[m_index].Name, hash, size, file, exception);
                m_handle = null;

                m_index++;
                if (m_index < m_volumes.Count)
                    m_handle = m_backend.GetAsync(m_volumes[m_index].Name, m_volumes[m_index].Size, m_volumes[m_index].Hash);

                return true;
            }

            public void Reset()
            {
                throw new NotSupportedException("Cannot reset " + this.GetType().FullName);
            }
        }

        private IList<IRemoteVolume> m_volumes;
        private BackendManager m_backend;

        public AsyncDownloader(IList<IRemoteVolume> volumes, BackendManager backend)
        {
            m_volumes = volumes;
            m_backend = backend;
        }

        public IEnumerator<IAsyncDownloadedFile> GetEnumerator()
        {
            return new AsyncDownloaderEnumerator(m_volumes, m_backend);
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
    }
}