The class DownloadManager (gb.net.curl) implements a manager that processes a list of downloads. You add to it a list of URLs whose content is to be downloaded.
The download manager processes the downloads transparently in the background, triggering events so that you always know what is happening.
This is how you create a DownloadManager and add two download addresses, for example:
Private $hManager As DownloadManager $hManager = New DownloadManager As "DLManager" ' "DLManager" is the Event-Name $hManager.MaxClient = 2 $hManager.Add("https://www.gambas-buch.de/lib/exe/fetch.php?media=24:24.1:24.1.4:dnsclient.tar.gz") $hManager.Add("https://www.gambas-buch.de/lib/exe/fetch.php?media=24:24.4.1:smtp.client-0.6.1.tar.gz")
The DownloadManager class has the following properties:
Property | Data type | Description |
---|---|---|
Count | Integer | Returns the number of downloads to be processed. |
MaxClient | Integer | Sets or returns the maximum number of download clients. |
Progress | Float | Returns the progress for all downloads as a real number between 0 and 1. |
Size | Integer | Returns the number of bytes downloaded for all downloads. |
Speed | Integer | Returns the current download speed in bytes/s. |
State | Integer | Returns the status of the DownloadManager. The values are NotReady (0), Ready (1), Downloading (2), Finnish (3) or Error (4). |
Table 24.2.7.1.1 : Properties of the DownloadManager class.
Notes:
The DownloadManager class has these four methods:
Method | Description |
---|---|
Add ( URL As String [ , Key As String ] ) | Adds a URL to the list of downloads to be processed. The parameter Key is optional. |
Clear() | Clears the list (data type Collection) of all downloads to be processed by the manager. |
Start() | Starts the DownloadManager. |
Stop() | Stops the DownloadManager. |
Table 24.2.7.2.1 : Methods of the class DownloadManager
Notes:
The DownloadManager class has these events:
Event | Description |
---|---|
Connect | The event is triggered when the Download Manager has been started and the current client has connected to the server. |
End | The event is triggered when all downloads have been processed. |
Error ( Key As String ) | The event is triggered when an error occurs during a download. |
Finish ( Key As String ) | The event is triggered when a download has finished. |
Progress | The event is triggered when data is loaded. |
Size ( Key As String ) | The event is triggered each time the download manager reads the size of the download file to be loaded with DownloadManager[Key].Size. |
Table 24.2.7.3.1 : Events of the class DownloadManager
Notes:
In the DLManager project, a predefined list of 3 downloads is managed by the DownloadManager. After the programme is started, it first checks whether a connection to the Internet can be established. Then all three downloads are executed. The download progress is visually displayed for each download (→ 69%) and subsuming (→ 35%):
Figure 24.2.7.4.1: GUI DownloadManager
After the end of all downloads, the files stored in the default base directory /tmp/gambas.<UserId>/<ProcessId> are copied with their original file names to the directory /home/username/Downloads/DLD (backup):
Figure 24.2.7.4.2: Contents of the download folder DLD
You can specify the target directory in the source code or freely in the dialogue. In the project the second option was not used.
The source code is specified in full:
' Gambas class file Public hDownloadManager As New DownloadManager Public sDownloadFile As New String[] Private $iTotalSize As Integer Private $iCurrentKey As Integer Private $aURL As String[] Private $aOriginalFilenames As String[] Private $TempDirectory As String Private $AverageSpeed As Integer Public Sub Form_Open() Dim sURL As String FMain.Resizable = False MCheck.Check_Network() $aURL = New String[] hDownloadManager = New DownloadManager As "DLM" $iCurrentKey = 1 btnBackup.Enabled = False '-- Begin: Definition of the download URLs -------------------------------------------------------- sURL = "https://www.gambas-buch.de/lib/exe/fetch.php?media=8:8.0:k8.0.pdf" $aURL.Add(sURL) hDownloadManager.Add(sURL) sURL = "https://www.gambas-buch.de/lib/exe/fetch.php?media=test1.zip" $aURL.Add(sURL) hDownloadManager.Add(sURL) sUrl = "https://www.gambas-buch.de/lib/exe/fetch.php?media=9:9.0:k9.0.pdf" $aURL.Add(sURL) hDownloadManager.Add(sURL) '-- End: Definition of the download URLs ---------------------------------------------------------- Select hDownloadManager.Count Case 1 tblTotalSize.Text = "Größe der Download-Datei:" Case 2 tblTotalSize.Text = "Größe der beiden Download-Dateien:" Case 3 tblTotalSize.Text = "Größe aller " & Str(hDownloadManager.Count) & " Download-Dateien:" End Select End Public Sub btnStart_Click() $AverageSpeed = 0 hDownloadManager.Start() End Public Sub btnBackup_Click() Backup() End Public Sub DLM_Progress() Timer1.Start() ProgressBarCurrent.Value = hDownloadManager[$iCurrentKey].Current/ hDownloadManager[$iCurrentKey].Size ProgressBarTotal.Value = hDownloadManager.Progress SetStatus(hDownloadManager.State) lblCurrentDLSize.text = "Download Aktuell ( Key = " & $iCurrentKey & " )" End Public Sub DLM_Error(Key As String) If Not hDownloadManager[Key].ErrorText Then Print "No Error!" Else Print "Error = "; hDownloadManager[Key].ErrorText; " bei "; hDownloadManager[Key].Url hDownloadManager.Stop() hDownloadManager.State Endif End Public Sub DLM_End() SetStatus(hDownloadManager.State) hDownloadManager.Stop() btnBackup.Enabled = True Timer1.Stop() End Public Sub DLM_Finish(Key As String) Inc $iCurrentKey $TempDirectory = Split(hDownloadManager[Key].Path, "/")[3] Try ProgressBarCurrent.Value = hDownloadManager[Key].Current / hDownloadManager[Key].Size End Public Sub DLM_Size(Key As String) $iTotalSize += hDownloadManager[Key].Size lblTotalSize.Text = Format($iTotalSize, "###,###,##0") End Public Sub Timer1_Timer() If $AverageSpeed = 0 Then $AverageSpeed = hDownloadManager.Speed 'Initial speed Else $AverageSpeed = 0.9 * $AverageSpeed + 0.1 * hDownloadManager.Speed 'Floating average speed Endif lblTotalDLSize.text = "Download Total ( Speed = " & Format($AverageSpeed / 1024, "0,000") & " kB/s )" End Private Sub RenameFiles(Source As String) Dim sFilter, i As Integer Dim sFile, sPattern, sURL, sFilename, sLastSubFolder As String Dim aDir As String[] $aOriginalFilenames = New String[] aDir = New String[] For Each sURL In $aURL sLastSubFolder = Split(sUrl, "/").Last If String.InStr(sLastSubFolder, "?") <> 0 Or String.InStr(sLastSubFolder, "=") <> 0 Or String.InStr(sLastSubFolder, ":") <> 0 Then sFilename = Split(sLastSubFolder, "?,=,:", "", False, False).Last $aOriginalFilenames.Add(sFilename) Else $aOriginalFilenames.Add(sLastSubFolder) Endif Next sPattern = "*.tmp" sFilter = gb.File i = 0 For Each sFile In Dir(Source, sPattern, sFilter).Sort(0) Move Source &/ sFile To Source &/ $aOriginalFilenames[i] Inc i Next End Private Sub CopyDir(Source As String, Destination As String) Dim sFile, sPattern As String Dim sFilter As Integer Dim FileInfo As Stat sPattern = "*.*" sFilter = gb.File If Not Exist(Destination) Then Mkdir Destination For Each sFile In Dir(Source, sPattern, sFilter) FileInfo = Stat(Source &/ sFile) '-- The content of the error text file (with the original extension) is 'Not Found' '-- if the download file was cannot be found. OR 'Bad request' If FileInfo.Size = 9 Then Message.Info("<b>The file " & sFile & " was not found!</b><hr>The content of the saved error text file (with the original extension) is 'Not Found', if the download file was cannot be found.") Endif '-- The content of the error text file (with the original extension) is 'Bad request' '-- if the download file was cannot be found. If FileInfo.Size = 11 Then Message.Info("<b>The file " & sFile & " was not found!</b><hr>The content of the saved error text file (with the original extension) is 'Bad request', if the download file was cannot be found.") Endif If Not Exist(Destination &/ sFile) Then Copy Source &/ sFile To Destination &/ sFile Endif Next End Private Sub Backup() Dim sSource, sDestination As String sSource = "/tmp/gambas." & System.User.Id &/ $TempDirectory sDestination = System.User.Home &/ "Downloads" &/ "DLD" RenameFiles(sSource) CopyDir(sSource, sDestination) Print "DONE!" End Private Function SetStatus(iStatus As Integer) Select iStatus Case Download.NotReady lblStatus.Text = "Download_NotReady" Case Download.Ready lblStatus.Text = "Download_Ready" Case Download.Downloading lblStatus.Text = "Download_Downloading" Case Download.Finish lblStatus.Text = "Download_Finish" Case Download.Error lblStatus.Text = "Download_Error" End Select End Public Sub Form_Close() hDownloadManager.Stop() End
Project