Can the server report to client about it`s progress of transferring a file ?

I need to be able to monitor the progress (and present it to the end-user)
of sending a file from Server to client as the client is unable to monitor the progress.
I`m using Wowza media server (rtmp protocol and java server side) and AS3 flash client.
The reason the AS3 client can`t monitor the progress is that
I suffer from FP-1959 Adobe known bug in a slightly different manner then described on the
adobe web page dedicated to it, here:
http://bugs.adobe.com/jira/browse/FP-1959

The AS3 clients connects to a java module on the server (with NetConnection) and asks
to send it a file. The java module sends the requested file as a bytearray.
The AS3 client recieves, BUT it can not present a progress bar of the transfer as
the AS3 Loader class is not accessible while the transfer lasts.
It becomes accessible only when the file is loaded, as described in the  FP-1959 Adobe known bug page.
I have tried transferring this file with amfphp server instead of the Wowza java rtmp server
but the AS3 client bug remains adamant : It can`t be done neither with listening to progressEvent nor with listening to ENTER_FRAME.

As a result of this obstacle I need a whole new way to monitor progress.
Any idea will be very welcome.
Even a general notion may help.
Perhaps there is a way for the server to monitor the progress of sending the file to the client.
perhaps the server can write the progress to some status file and then the client can read it.
perhaps this progress can be polled somehow.

I attached to this message the java server side script responsible for serving the requested file over rtmp.
Many thanks in advance for your efforts.


package com.wowza.wms.security;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import com.wowza.wms.amf.*;
import com.wowza.wms.client.*;
import com.wowza.wms.module.*;
import com.wowza.wms.request.*;

public class ModuleSwfLoader extends ModuleBase {

	public void loadSwf(IClient client, RequestFunction function, AMFDataList params) {
		//getLogger().info("loadSwf");
		String swfName = "";
		byte[] fileBytes = null;
		
		if (getParam(params, PARAM1).getType() == AMFData.DATA_TYPE_STRING) {
			String contentFolder = client.getAppInstance().getStreamStoragePath() + File.separator;
			swfName  = contentFolder + params.getString(PARAM1);
			getLogger().info("swfLocation: " + swfName);
		}
		
		try {
			if (!swfName .isEmpty()) {
				File swf = new File(swfName);
				if (swf.exists() && swf.canRead() && swf.isFile() && swf.length() < Integer.MAX_VALUE) {
					InputStream in = new FileInputStream(swf);
						int length = (int) swf.length();
						fileBytes = new byte[length];

						int offset = 0;
				        int numRead = 0;
				        while (offset < fileBytes.length && (numRead=in.read(fileBytes, offset, fileBytes.length-offset)) >= 0) {
				            offset += numRead;
				        }
				        
				        if (offset < fileBytes.length) {
				            throw new IOException("Could not completely read file "+swf.getName());
				        }
				        
				        in.close();
				}
			}
		} catch (FileNotFoundException e) {
			getLogger().info("file not found");
			e.printStackTrace();
		} catch (IOException e) {
			getLogger().info("IOException");
			e.printStackTrace();
		}
		sendResult(client, params, AMFDataByteArray.wrap(fileBytes));
	}

}

Open in new window

foyshAsked:
Who is Participating?
 
afumedoConnect With a Mentor Commented:

Hi foysh,

Sorry for my late response.

for : "whether I think this is possible to perform in AS3 for client side."

Answer: I gave you a sudo code as I don't program using AS3.

for: "If you should change accordingly the server side Java script "

Answer: of course you will have to change the server code.

- the ( params ) that you pass to the server in the client Network Request can contain, for example, the ( Start ) byte and ( the  Length in bytes ) of the portion of file you are requesting.

the server then should of an FileInputStream on the file specified, Seek to the start byte, Read the amount of bytes requested if available, and then ( sendResult ) with those bytes read.

and as I have already told you in my first comment, client side code should continue to ask the server for (request) the next number of bytes till it receives all the file.

- and If you wonder from where should the client know the length of the file. the answer lies in the first comment. where I told you that your client can send a request with a parameter added that says to the server : Don't send me the file, just tell me the size of it.

then when server code detects that parameter, it should only read the size of the file and ( sendResult )  that file-size to its client.

I hope all things are clear now.

Best wishes,

Ahmed Bedier.
0
 
afumedoCommented:

 Here is some steps you may do:

1- Ask the server for the size of the file: send a Request to the server giving it the ( File Name ) you need and a certain ( parameter ) , and the server should then pass the size of the file to its client.

# IF you can see the currently available bytes transfered to the client
    2- then you now should be able to display the progress ( received / total-size )

# ELSE
    2- select a chunk size ( say 4KB or whatever you see ) and tell the server to give you a portion of the file ( starting from byte X )  with length (chunk-size ) and so, when you receive that chunk you would be able to display a proper progress information.

Psudo code attached.

#ENDIF

Best wishes,

Regards,

Ahmed Bedier
class MyClass
{
    int chunkSize ; //in bytes
    int start = 0 ;
    int totalSize ;

    private void getSWFFile( fileName )
    {
         // send a request for the file size.
    }

    private void on_file_size_received()
    {
        // select a chunk size and request all file-chunks from server
       
        start = 0 
        chunkSize = 4096 ; 
          
        // request first chunk
        sendChunkRequest (start, chunkSize )
    }  

    private void sendChunkRequest ( start, ChunkSize )
    {
        // send request with proper parameters.
    }

    private void on_current_chunk_received()
    {
        // display progress
        int progress = (int)( 100 * ( received / total size ) ) ;

        // if (received != totalSize) 
        //    request the next chunk
        //else
        //    file download complete.
    }
}

Open in new window

0
 
foyshAuthor Commented:
Dear Ahmed Bedier.
First of all I`d like to heartily thank you for your reply as well as deeply apologise for the delay in my response. This Delay is partly due to my attempting to understand the implications of you suggestion.
I must say that if I only understand how to put your idea to actual code this would be a truely perfect solution for me.
For this reason I`d like to ask you whether you think this is possible to perform in AS3 for client side. I don`t really know any command in AS3 that asks for only a portion of a bytearray. Or if this is to be done withany other client sdie language, would you specify which, please ?
I`d also like to ask you whether in order to put your idea to action, I should change accordingly the server side Java script which I attached to my original message.

Many thanks for your attention and assistance !
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
foyshAuthor Commented:
As I could find no EDIT option on the board, I repost the 7th and 8th lines of my previuos message , that contained typo errors:
 
"really know any command in AS3 that asks for only a portion of a bytearray. Or if this is to be done with any other client side language, would you specify which, please ?"

Sorry and thanks.
0
 
foyshAuthor Commented:
Thank you,
Ahmed Bedier, for your assistance, and thanks also for clarifying, your answer has helped me in understanding tha path I should take.
0
 
foyshAuthor Commented:
Many thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.