Asynchronous Downloads

Discuss any general programming issues here
Post Reply
djrikki
Posts: 682
Joined: Wed Apr 06, 2011 12:26 am

Asynchronous Downloads

Post by djrikki »

Hi Andreas,

Is it possible within the current implementation to have asynchronous downloads using DownloadFile()?

If yes, how would this best be approached.

Thanks.
Evolve - Rapid GUI Development tool for MUI Royale and RapaGUI
http://myevolve.wordpress.com
User avatar
airsoftsoftwair
Posts: 5446
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Asynchronous Downloads

Post by airsoftsoftwair »

That's currently not possible. You can implement it manually, though, using OpenConnection() et. al.
djrikki
Posts: 682
Joined: Wed Apr 06, 2011 12:26 am

Re: Asynchronous Downloads

Post by djrikki »

Thought that would be the case.

I'd have to work out how to monitor the download to show a progress bar... have you any idea how this can be achieved through the HTTP protocol?

In fact I already know the total size of each file that can be downloaded, but how much has been downloaded during the connection is something else I need to establish.

Research required.
Evolve - Rapid GUI Development tool for MUI Royale and RapaGUI
http://myevolve.wordpress.com
djrikki
Posts: 682
Joined: Wed Apr 06, 2011 12:26 am

URGENT: Re: Asynchronous Downloads

Post by djrikki »

URGENT Request, can anyone help get this working asap before the weekend is over?

This will run as is.

Code: Select all

Global base
base = {}

Function base:DownloadFile(server$, url$, downloadpath$, filename$)

	OpenConnection(1, server$, 80)

	SendData(1, "GET " .. url$ .. " HTTP/1.0\r\n\r\n")
	data$, count, done = ReceiveData(1, #RECEIVEALL)

	; Redirect
	If FindStr(data$,"302 Found") > -1
	    CloseConnection(1)
	    redirection$ = MidStr(data$,FindStr(data$,"Location: "),StrLen(data$))
	    redirection$ = MidStr(redirection$,10,FindStr(redirection$,"\n")-11)
	    
	    server$ = ReplaceStr(redirection$, "http://", "")
	    server$ = MidStr(server$,0,FindStr(server$,"/"))
	    
	    OpenConnection(1, server$, 80)
	    SendData(1, "GET " .. redirection$ .. " HTTP/1.0\r\n\r\n")
	    
		data$ = nil
	 
		; Skip HTTP Headers
	    While data$ = nil Or FindStr(data$,"Content-Type:") = -1
	    	data$, count, done = ReceiveData(1, #RECEIVELINE)
	   	Wend
	   	data$, count, done = ReceiveData(1, #RECEIVELINE) ; Skip blank line
	    
	    data$, count, done = ReceiveData(1, #RECEIVEALL, False) ; Read file data
	    
	ElseIf FindStr(data,"200 OK") > -1   
	    	    
	EndIf
	
	; Not Found
	If FindStr(data$,"404 Found") > -1
	    CloseConnection(1)
	    Return(False)
	; Found - Download it
	Else
        data$, count, done = ReceiveData(1, #RECEIVEALL, False)

	    While done = False
	        debugprint(done)
	    Wend
		CloseConnection(1)
		OpenFile(1,downloadpath$ .. filename$, #MODE_WRITE)
		WriteString(1, data$)
		CloseFile(1)
	    Return(True)
	EndIf
EndFunction
    
success = base:DownloadFile("www.os4depot.net","http://www.os4depot.net/share/game/board/africa.lha","RAM:","africa.lha")

If success = True
	debugprint("success")
Else
    debugprint("failure")
EndIf
Problem: Done never equals True so gets stuck in the loop.
Evolve - Rapid GUI Development tool for MUI Royale and RapaGUI
http://myevolve.wordpress.com
djrikki
Posts: 682
Joined: Wed Apr 06, 2011 12:26 am

CORRECTION:Re: Asynchronous Downloads

Post by djrikki »

Actually, correction made, however still no luck:

Code: Select all

Global base
base = {}

Function base:DownloadFile(server$, url$, downloadpath$, filename$)
    
    Local data$, count, done

	OpenConnection(1, server$, 80)

	SendData(1, "GET " .. url$ .. " HTTP/1.0\r\n\r\n")
	data$, count, done = ReceiveData(1, #RECEIVEALL)

	; Redirect
	If FindStr(data$,"302 Found") > -1
	    CloseConnection(1)
	    redirection$ = MidStr(data$,FindStr(data$,"Location: "),StrLen(data$))
	    redirection$ = MidStr(redirection$,10,FindStr(redirection$,"\n")-11)
	    
	    server$ = ReplaceStr(redirection$, "http://", "")
	    server$ = MidStr(server$,0,FindStr(server$,"/"))
	    
	    OpenConnection(1, server$, 80)
	    SendData(1, "GET " .. redirection$ .. " HTTP/1.0\r\n\r\n")
	    
		data$ = nil
	 
		; Skip HTTP Headers
	    While data$ = nil Or FindStr(data$,"Content-Type:") = -1
	    	data$, count, done = ReceiveData(1, #RECEIVELINE)
	   	Wend
	   	data$, count, done = ReceiveData(1, #RECEIVELINE) ; Skip blank line
	    
	    data$, count, done = ReceiveData(1, #RECEIVEALL, False) ; Read file data
	    
	ElseIf FindStr(data,"200 OK") > -1   
	    	    
	EndIf
	
	; Not Found
	If FindStr(data$,"404 Found") > -1
	    CloseConnection(1)
	    Return(False)
	    
	; Found - Download it
	Else
	    While done = False
	        debugprint(done)
	    Wend
		CloseConnection(1)
		OpenFile(1,downloadpath$ .. filename$, #MODE_WRITE)
		WriteString(1, data$)
		CloseFile(1)
	    Return(True)
	EndIf
EndFunction
    
success = base:DownloadFile("www.os4depot.net","http://www.os4depot.net/share/game/board/africa.lha","RAM:","africa.lha")

If success = True
	debugprint("success")
Else
    debugprint("failure")
EndIf
Evolve - Rapid GUI Development tool for MUI Royale and RapaGUI
http://myevolve.wordpress.com
djrikki
Posts: 682
Joined: Wed Apr 06, 2011 12:26 am

Re: Asynchronous Downloads

Post by djrikki »

Another approach...

Code: Select all

Global base, data$, count, done
base = {}

Function base:DownloadFile(server$, url$, downloadpath$, filename$)
    
	OpenConnection(1, server$, 80)

	SendData(1, "GET " .. url$ .. " HTTP/1.0\r\n\r\n")
	data$, count, done = ReceiveData(1, #RECEIVEALL)

	; Redirect
	If FindStr(data$,"302 Found") > -1
	    CloseConnection(1)
	    redirection$ = MidStr(data$,FindStr(data$,"Location: "),StrLen(data$))
	    redirection$ = MidStr(redirection$,10,FindStr(redirection$,"\n")-11)
	    
	    server$ = ReplaceStr(redirection$, "http://", "")
	    server$ = MidStr(server$,0,FindStr(server$,"/"))
	    
	    OpenConnection(1, server$, 80)
	    SendData(1, "GET " .. redirection$ .. " HTTP/1.0\r\n\r\n")
	    
		data$ = nil
	 
		; Skip HTTP Headers
	    While data$ = nil Or FindStr(data$,"Content-Type:") = -1
	    	data$, count, done = ReceiveData(1, #RECEIVELINE)
	   	Wend
	   	data$, count, done = ReceiveData(1, #RECEIVELINE) ; Skip blank line
	    
	    data$, count, done = ReceiveData(1, #RECEIVEALL, False) ; Read file data
	    
	ElseIf FindStr(data,"200 OK") > -1   
	    	    
	EndIf

	; Not Found
	If FindStr(data$,"404 Found") > -1
	    Return(False)
	    
	; Found - Download it
	Else
		OpenFile(1,downloadpath$ .. filename$, #MODE_WRITE)
	    Return(True)
	EndIf
EndFunction
    
success = base:DownloadFile("www.os4depot.net","http://www.os4depot.net/share/game/board/africa.lha","RAM:","africa.lha")

Function base:BuildFile(msg)
	Switch msg.id
        Case 1:
            debugprint("write the bloody thing!")
	EndSwitch
EndFunction
    
Function base_BuildFile(msg) base:BuildFile(msg) EndFunction
    
Function Escape(msg)	
	If success = True
    	WriteString(1,data$)
    	CloseConnection(1)
		CloseFile(1)
		End()
	Else
	    ; do something
	EndIf    
Endfunction

InstallEventHandler({OnReceiveData = base_BuildFile, OnKeyDown = Escape})

Repeat
    WaitEvent()
Forever
This time the OnReceiveData event is only called once, whatever I try I cannot get it to write more than a partial file.
Evolve - Rapid GUI Development tool for MUI Royale and RapaGUI
http://myevolve.wordpress.com
User avatar
airsoftsoftwair
Posts: 5446
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Asynchronous Downloads

Post by airsoftsoftwair »

Sorry, I don't have the time to help with larger code snippets. Also, my knowledge about HTTP communication has almost completely vanished. I wrote the DownloadFile() implementation back in May 2010 so I'm not of much help here. Try Google and Telnet to see what's going on there :)
Post Reply