screancasttest.SenderAsyncTask.java Source code

Java tutorial

Introduction

Here is the source code for screancasttest.SenderAsyncTask.java

Source

/*
 * Copyright (c) 2015 Renard Wellnitz.
 *
 *  This file is part of ScreenShare.
 *
 *     Foobar is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     Foobar is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */

package screancasttest;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.example.gemswin.screencastrecevertest.LoginActivity;
import com.example.gemswin.screencastrecevertest.PrefManager;
import com.example.gemswin.screencastrecevertest.ScreenCastLib.VideoChunk;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingDeque;

public class SenderAsyncTask extends AsyncTask<Void, Void, Void> {

    private static final String LOG_TAG = SenderAsyncTask.class.getSimpleName();
    public static List<String> mIp;
    Context context;
    JSONParser jparser1 = new JSONParser();
    List<String> ipArray;
    JSONObject json1;
    public static DataOutputStream[] dataOut;
    private ProgressDialog dialog;
    LinkedBlockingDeque<VideoChunk> mVideoChunks = new LinkedBlockingDeque<VideoChunk>();
    PrefManager pref;

    public void addChunk(VideoChunk chunk) {

        synchronized (mVideoChunks) {

            mVideoChunks.addFirst(chunk);
            if (mVideoChunks.size() > 2) {
                Log.i(LOG_TAG, "Chunks: " + mVideoChunks.size());

            }

        }
    }

    SenderAsyncTask(Context context) {
        this.context = context;

        dialog = new ProgressDialog(context);
        pref = new PrefManager(context);
    }

    public void setDataOutput(DataOutputStream[] d) {

        dataOut = d;

    }

    /** progress dialog to show user that the backup is processing. */
    /** application context. */

    @Override
    protected Void doInBackground(Void... params) {

        Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
        String classget = LoginActivity.studClass;

        List<NameValuePair> params1 = new ArrayList<NameValuePair>();
        params1.add(new BasicNameValuePair("class", classget));

        String url = "http://176.32.230.250/anshuli.com/ScreenCast/getIPs.php";

        json1 = jparser1.makeHttpRequest(url, "POST", params1);

        try {
            // Checking for SUCCESS TAG

            //forjson.clear();

            String heading = "";
            String message = "";
            //Toast.makeText(MainActivity.this, (CharSequence) json, 1).show();
            ipArray = new ArrayList<String>();

            JSONArray account = json1.getJSONArray("IPs");
            for (int i = 0; i < account.length(); i++) {
                json1 = account.getJSONObject(i);

                String IpString = json1.getString("IP");

                if (!IpString.equals("0.0.0.0"))
                    ipArray.add(IpString);
                // forjson.add(Roll+"-"+ NAME);
                //categories_description.add(description);

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        String portget = LoginActivity.portget;
        int port = Integer.parseInt(portget);

        java.net.Socket socket = null;

        //   ip.add("192.168.15.105");
        //   ip.add("192.168.15.103");
        int len = ipArray.size();
        /*   try {
        for(int i=0;i<ip.size();i++)
        socket[i] = new java.net.Socket(ip.get(i),port);
           //   socket1 = new java.net.Socket("192.168.15.105", port);
         // connect to server
           } catch (IOException e) {
        e.printStackTrace();
           }*/
        dataOut = new DataOutputStream[len];
        try {
            for (int i = 0; i < len; i++) {
                socket = new java.net.Socket(ipArray.get(i), port);
                dataOut[i] = new DataOutputStream(socket.getOutputStream());
            }

            //   dataOut1 = new DataOutputStream(socket1.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

        while (!isCancelled()) {

            VideoChunk chunk = null;
            try {
                //Log.d(LOG_TAG, "waiting for data to send");
                chunk = mVideoChunks.takeLast();
                //Log.d(LOG_TAG,"got data. writing to socket");
                int length = chunk.getData().length;
                for (int i = 0; i < ipArray.size(); i++) {

                    dataOut[i].writeInt(length);
                    dataOut[i].writeInt(chunk.getFlags());
                    dataOut[i].writeLong(chunk.getTimeUs());
                    dataOut[i].write(chunk.getData());
                    dataOut[i].flush();

                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                continue;
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        return null;
    }

}