1

What is best solution for file transfer using alljoyn?

asked 2015-07-22 18:18:13 -0700

Hyosang gravatar image

Gets simple data using simpleservice works good. But, I need file transfer(reliable) and streaming video (unreliable is acceptable).

So I coded a service refers RawService Sample in Core SDK. but it occurs IOException.

java.io.IOException: write failed: EAGAIN (Try again)

other forum, EAGAIN occurs when write() method with no data, but I check write data larger than 0.

Is not suitable RawService to file transfer? Am I use traditional socket communication?

---------- SENDER CODE --------------

    private class SenderThread extends Thread {
    @Override
    public void run() {
        Mutable.IntegerValue sockFd = new Mutable.IntegerValue();
        Status status = mBus.getSessionFd(mSessionId, sockFd);

        Log.d("TEST", "getSessionFd :: " + status);

        if(status == Status.OK) {
            BufferedOutputStream bos = null;
            FileInputStream fis = null;

            try {
                FileDescriptor fd = Common.createFd(sockFd);

                bos = new BufferedOutputStream(new FileOutputStream(fd));

                fis = new FileInputStream(mLocalpath);

                byte[] buf = new byte[1024];
                int nRead;

                //signer, file size
                File f = new File(mLocalpath);
                String header = String.format("%s%020d", Define.FILE_TRANSFER_SIGNER, f.length());
                long tosend = f.length();

                bos.write(header.getBytes("US-ASCII"));

                while ((nRead = fis.read(buf)) > 0) {
                        bos.write(buf, 0, nRead);
                }

                Log.d("TEST", "Send completed");

                bos.close();
            }catch(FileNotFoundException e) {
                Log.w("TEST", Log.getStackTraceString(e));
            }catch(IOException e) {
                Log.w("TEST", Log.getStackTraceString(e));
                Log.d("TEST", e.getMessage());
            }finally {
                if(fis != null) { try { fis.close(); }catch(IOException e) { } }
                if(bos != null) { try { bos.close(); }catch(IOException e) { } }

                //alljoyn service close
                release();
            }
        }

    }
}

------------------ RECEIVER CODE --------------------

    private class Downloader extends Thread {
    @Override
    public void run() {
        SessionOpts sessionOpts = new SessionOpts();
        sessionOpts.traffic = SessionOpts.TRAFFIC_RAW_RELIABLE;
        Mutable.IntegerValue sessionId = new Mutable.IntegerValue();

        Status status = mBus.joinSession(Define.KNOWN_NAME_FILETRANS, mContactPort, sessionId, sessionOpts, new SessionListener() {
            @Override
            public void sessionLost(int sessionId, int reason) {
                Log.d("TEST", "Session Lost");
            }
        });
        Log.d("TEST", "Join session :: " + status);

        Mutable.IntegerValue sockFd = new Mutable.IntegerValue();
        status = mBus.getSessionFd(sessionId.value, sockFd);
        Log.d("TEST", "getSessionFd :: " + status);

        if(status != Status.OK) {
            return;
        }

        FileDescriptor fd = Common.createFd(sockFd);

        try {
            File f = new File(mSavePath);
            Log.d("TEST", "Save to = " + mSavePath);
            f.getParentFile().mkdirs();

            FileInputStream fis = new FileInputStream(fd);
            FileOutputStream fos = new FileOutputStream(mSavePath);
            byte[] buf = new byte[1024];
            int nRead;
            long filesize = 0;
            long nTotalRead = 0;

            //read signer and file size
            nRead = Define.FILE_TRANSFER_SIGNER.length() + 20;
            while(true) {
                if(fis.available() >= nRead) {
                    fis.read(buf, 0, nRead);

                    String token = new String(buf, 0, Define.FILE_TRANSFER_SIGNER.length());
                    Log.d("TEST", "FileTransfer signer = " + token);

                    token = new String(buf, Define.FILE_TRANSFER_SIGNER.length(), 20);

                    try {
                        filesize = Long.parseLong(token, 10);
                    }catch(NumberFormatException e) {
                        Log.w("TEST", Log.getStackTraceString(e));
                    }

                    Log.d("TEST", "File size = " + filesize);

                    break;
                }else {
                    try {
                        Thread.sleep(300);
                        Log.d("TEST", "Wait");
                    }catch(InterruptedException e) {
                        Log.d("TEST", Log.getStackTraceString(e));
                    }
                }
            }

            while(true) {
                if(fis.available() > 0) {
                    nRead = fis.read(buf);
                    fos.write(buf, 0, nRead);
                    nTotalRead += nRead;

                    Log.d("TEST", String.format("Downloading %d/%d", nTotalRead, filesize));

                    if(nTotalRead >= filesize) break;
                }else {
                    Log.d("TEST", "Waiting...");
                    try { Thread.sleep(500); } catch(InterruptedException e) { }
                }
            }

            fos.close();

            Log.d("TEST", "Download completed");

            //TODO CLOSE
        }catch(IOException e) {
            Log.w("TEST", Log.getStackTraceString(e));
        }

    }
}
edit retag flag offensive close merge delete

Comments

File download shouldnt be that difficult, since you can convert any file to binary representation, serve it to clients as byte-array and reconvert it back to the right format. Take a look at samples. Alljoyn has an Audio Streaming API, but it seems that this API yet only has been developed for C++.

mayo ( 2015-07-24 01:46:31 -0700 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-09-02 09:15:10 -0700

marcello gravatar image

There are examples of how to do file transfer using AllJoyn in the services/filetransfer modules. They are not actively maintained, but should server a good representation of how one might define interfaces and build code to move data from one place to another. For audio there is the audio services in multimedia/audio.

edit flag offensive delete publish link more
Login/Signup to Answer

Question Tools

Follow
1 follower

Stats

Asked: 2015-07-22 18:18:13 -0700

Seen: 290 times

Last updated: Sep 02 '15