Android Open Source - PortAuthority Scan Ports Async Task






From Project

Back to project page PortAuthority.

License

The source code is released under:

GNU General Public License

If you think the Android project PortAuthority listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.aaronjwood.portauthority.async;
/*from  w  w w  . jav  a  2  s .c  om*/
import android.os.AsyncTask;
import android.util.Log;

import com.aaronjwood.portauthority.response.HostAsyncResponse;
import com.aaronjwood.portauthority.runnable.ScanPortsRunnable;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ScanPortsAsyncTask extends AsyncTask<Object, Void, Void> {

    private static final String TAG = "ScanPortsAsyncTask";
    private HostAsyncResponse delegate;

    public ScanPortsAsyncTask(HostAsyncResponse delegate) {
        this.delegate = delegate;
    }

    @Override
    protected Void doInBackground(Object... params) {
        final int NUM_THREADS = 500;
        String ip = (String) params[0];
        int startPort = (int) params[1];
        int stopPort = (int) params[2];

        ExecutorService executor = Executors.newCachedThreadPool();

        int chunk = (int) Math.ceil((double) (stopPort - startPort) / NUM_THREADS);
        int previousStart = startPort;
        int previousStop = (startPort - 1)+ chunk;

        for(int i = 0; i < NUM_THREADS; i++) {
            if(previousStop >= stopPort) {
                previousStop = stopPort;
                executor.execute(new ScanPortsRunnable(ip, previousStart, previousStop, delegate));
                break;
            }
            executor.execute(new ScanPortsRunnable(ip, previousStart, previousStop, delegate));
            previousStart = previousStop + 1;
            previousStop = previousStop + chunk;
        }

        executor.shutdown();

        try {
            executor.awaitTermination(10, TimeUnit.MINUTES);
        }
        catch(InterruptedException e) {
            Log.e(TAG, e.getMessage());
        }

        this.delegate.processFinish(true);

        return null;
    }
}




Java Source Code List

com.aaronjwood.portauthority.activity.HostActivity.java
com.aaronjwood.portauthority.activity.MainActivity.java
com.aaronjwood.portauthority.async.GetExternalIpAsyncTask.java
com.aaronjwood.portauthority.async.GetHostnameAsyncTask.java
com.aaronjwood.portauthority.async.ScanHostsAsyncTask.java
com.aaronjwood.portauthority.async.ScanPortsAsyncTask.java
com.aaronjwood.portauthority.network.Discovery.java
com.aaronjwood.portauthority.network.Host.java
com.aaronjwood.portauthority.network.Wireless.java
com.aaronjwood.portauthority.response.HostAsyncResponse.java
com.aaronjwood.portauthority.response.MainAsyncResponse.java
com.aaronjwood.portauthority.runnable.ScanHostsRunnable.java
com.aaronjwood.portauthority.runnable.ScanPortsRunnable.java