User Tools

Site Tools


Sidebar

Network and communication

k24:k24.2:k24.2.7:start

24.2.7 Class DownloadManager

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")

24.2.7.1 Properties

The DownloadManager class has the following properties:

PropertyData typeDescription
CountIntegerReturns the number of downloads to be processed.
MaxClientIntegerSets or returns the maximum number of download clients.
ProgressFloatReturns the progress for all downloads as a real number between 0 and 1.
SizeIntegerReturns the number of bytes downloaded for all downloads.
SpeedIntegerReturns the current download speed in bytes/s.
StateIntegerReturns 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:

  • All properties - except MaxClient - can only be read out!
  • The value for the integer property MaxClient may only be between 1 and 32.
  • With the values of the Progress property in the interval [0,1] you can, for example, directly control a progress bar. If the property Progressbar.Label = True, the download progress is also displayed with a percentage.
  • Note: If, for example, the download manager manages 3 download clients and Download1.Status = Download.Ready, Download2.Status = Download.Downloaded and Download3.Status = Download.Finnish, then the highest value is always output as the status → 4.

24.2.7.2 Methods

The DownloadManager class has these four methods:

MethodDescription
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:

  • If the optional parameter 'Key' is not specified in the Add(…) method, then the key is set to Key = CStr($iKey) - starting with “1”. Internally, the integer value $iKey is incremented.
  • For each download, the primary target directory is Temp$().
  • For example, you will find the download file for the 1st download in the temporary directory: /tmp/gambas.1000/7533/1.tmp. The name of the subfolder 7533 corresponds to the process ID of the current Gambas process.
  • Attention: All files located in the directory /tmp/gambas.<UserId>/<ProcessId> are automatically deleted when the Gambas programme is terminated.
  • Therefore, you should copy these loaded files - under their original file name - to a folder of your choice.

24.2.7.3 Events

The DownloadManager class has these events:

EventDescription
ConnectThe event is triggered when the Download Manager has been started and the current client has connected to the server.
EndThe 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.
ProgressThe 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:

  • Connect: Only for the protocol HTTP you can also read out the status.
  • Error (Key): Via the read-out key you can determine which download was faulty.
  • Finish(Key): Which download was finished can be determined by the key.
  • Size(Key): The DownloadManager does not have to be started to determine the file size of a download file! Problems can occur if redirects are involved. Then the size of the download file cannot be determined - the size 0 is returned. However, the download will be carried out!

24.2.7.4 Project

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

Download

Project

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k24/k24.2/k24.2.7/start.txt · Last modified: 16.08.2022 (external edit)

Page Tools