-
-
Save daviddoria/3eb8db4632d63d036086 to your computer and use it in GitHub Desktop.
ManetStarter with Latches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package arl.activityselector; | |
import java.util.HashSet; | |
import java.util.TimerTask; | |
import java.util.concurrent.Callable; | |
import java.util.concurrent.CountDownLatch; | |
import java.util.concurrent.ExecutionException; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.FutureTask; | |
import java.util.concurrent.TimeUnit; | |
import org.span.service.ManetHelper; | |
import org.span.service.ManetObserver; | |
import org.span.service.core.ManetService.AdhocStateEnum; | |
import org.span.service.routing.Node; | |
import org.span.service.system.ManetConfig; | |
import android.app.Activity; | |
import android.app.ProgressDialog; | |
import android.content.Context; | |
import android.util.Log; | |
/** | |
* This class is designed to be run once at the beginning of an application that | |
* needs ad-hoc networking. | |
*/ | |
public class ManetStarter | |
{ | |
private static ManetStarter instance = null; | |
protected ManetStarter() | |
{ | |
Log.w("ManetStarter", "Non-singleton instantiation is not allowed."); | |
} | |
public static ManetStarter createInstance(Activity activity) | |
{ | |
if (instance == null) | |
{ | |
instance = new ManetStarter(activity); | |
} | |
else | |
{ | |
Log.w("ManetStarter.createInstance()", | |
"Cannot create instance, an instance already exists."); | |
} | |
return instance; | |
} | |
public static ManetStarter getInstance() | |
{ | |
if (instance == null) | |
{ | |
Log.w("ManetStarter.getInstance()", "Instance is null..."); | |
} | |
return instance; | |
} | |
private ManetHelper mManetHelper = null; | |
public ManetHelper getManetHelper() | |
{ | |
return mManetHelper; | |
} | |
Activity mActivity = null; | |
Context mContext = null; | |
boolean mStarted = false; | |
// ManetStarter(Activity activity) | |
// { | |
// mManetHelper = new ManetHelper(activity); | |
// mActivity = activity; | |
// } | |
protected ManetStarter(Activity activity) | |
{ | |
mManetHelper = ManetHelper.createInstance(activity | |
.getApplicationContext()); | |
mActivity = activity; | |
} | |
/** | |
* This class is designed to display a message if the run() function is ever | |
* executed. That is, we use a timeoutTimer.schedule(timeoutTask, timeout); | |
* and if 'timeout' time has elapsed before the timer is canceled, the | |
* message is displayed. | |
*/ | |
class TimeoutTask extends TimerTask | |
{ | |
private String mMessage; | |
public void setMessage(final String message) | |
{ | |
mMessage = message; | |
} | |
@Override | |
public void run() | |
{ | |
Log.i("TimeoutTask:run", mMessage); | |
} | |
} | |
public class ConnectToServiceCallable implements Callable<Boolean>, | |
ManetObserver | |
{ | |
CountDownLatch serviceLatch = new CountDownLatch(1); | |
@Override | |
public Boolean call() throws Exception | |
{ | |
Log.i("ConnectToServiceCallable.call", | |
"ConnectToServiceCallable.call()..."); | |
mActivity.runOnUiThread(new Runnable() | |
{ | |
public void run() | |
{ | |
mManetHelper.connectToService(); | |
} | |
}); | |
boolean finishedBeforeTimeout = serviceLatch.await(3, | |
TimeUnit.SECONDS); | |
return finishedBeforeTimeout; | |
} | |
@Override | |
public void onServiceConnected() | |
{ | |
Log.i("ConnectToServiceCallable.onServiceConnected", | |
"ConnectToServiceCallable.onServiceConnected"); | |
serviceLatch.countDown(); | |
} | |
@Override | |
public void onServiceDisconnected() | |
{ | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onServiceStarted() | |
{ | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onServiceStopped() | |
{ | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onAdhocStateUpdated(AdhocStateEnum state, String info) | |
{ | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onConfigUpdated(ManetConfig manetcfg) | |
{ | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onPeersUpdated(HashSet<Node> peers) | |
{ | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onRoutingInfoUpdated(String info) | |
{ | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onError(String error) | |
{ | |
// TODO Auto-generated method stub | |
} | |
} | |
public class StartAdhocCallable implements Callable<Boolean>, ManetObserver | |
{ | |
CountDownLatch adhocLatch = new CountDownLatch(1); | |
// This is used to effectively block the UI (by displaying a dialog) | |
// without | |
// actually blocking the UI thread. | |
ProgressDialog progressDialog = null; | |
@Override | |
public Boolean call() throws Exception | |
{ | |
Log.i("StartAdhocCallable.call", "StartAdhocCallable.call()..."); | |
mManetHelper.sendStartAdhocCommand(); | |
mActivity.runOnUiThread(new Runnable() | |
{ | |
public void run() | |
{ | |
progressDialog = ProgressDialog.show(mActivity, "", | |
"Ensuring ad-hoc mode is started. Please wait...", | |
true); | |
progressDialog.show(); | |
} | |
}); | |
boolean startedAdhocBeforeTimeout = adhocLatch.await(20, | |
TimeUnit.SECONDS); | |
if (progressDialog != null) | |
{ | |
progressDialog.dismiss(); | |
} | |
return startedAdhocBeforeTimeout; | |
} | |
@Override | |
public void onServiceConnected() | |
{ | |
// Required by ManetObserver interface | |
} | |
@Override | |
public void onServiceDisconnected() | |
{ | |
// Required by ManetObserver interface | |
} | |
@Override | |
public void onServiceStarted() | |
{ | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onServiceStopped() | |
{ | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onAdhocStateUpdated(AdhocStateEnum state, String info) | |
{ | |
if (state == AdhocStateEnum.STARTED) | |
{ | |
Log.i("WorkerThread:onAdhocStateUpdated", "Adhoc is started!"); | |
mStarted = true; | |
adhocLatch.countDown(); | |
} | |
else | |
{ | |
Log.i("WorkerThread:onAdhocStateUpdated", | |
"Adhoc is not started!"); | |
} | |
} | |
@Override | |
public void onConfigUpdated(ManetConfig manetcfg) | |
{ | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onPeersUpdated(HashSet<Node> peers) | |
{ | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onRoutingInfoUpdated(String info) | |
{ | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onError(String error) | |
{ | |
// TODO Auto-generated method stub | |
} | |
} | |
public void PrepareNetwork() | |
{ | |
Thread thread = new Thread() | |
{ | |
@Override | |
public void run() | |
{ | |
doPrepareNetwork(); | |
} | |
}; | |
thread.start(); | |
} | |
public void doPrepareNetwork() | |
{ | |
Log.i("ManetStarter.PrepareNetwork()", | |
"Preparing network (connecting to service and starting adhoc)..."); | |
ConnectToServiceCallable connectToServiceCallable = new ConnectToServiceCallable(); | |
mManetHelper.registerObserver(connectToServiceCallable); | |
FutureTask<Boolean> serviceFutureTask = new FutureTask<Boolean>( | |
connectToServiceCallable); | |
ExecutorService serviceExecutor = Executors.newFixedThreadPool(1); | |
serviceExecutor.execute(serviceFutureTask); | |
try | |
{ | |
Log.i("PrepareNetwork", "Blocking waiting for serviceFutureTask..."); | |
boolean serviceStarted = serviceFutureTask.get(); | |
if (serviceStarted) | |
{ | |
Log.i("PrepareNetwork", "Service started, starting adhoc..."); | |
StartAdhocCallable startAdhocCallable = new StartAdhocCallable(); | |
mManetHelper.registerObserver(startAdhocCallable); | |
FutureTask<Boolean> adhocFutureTask = new FutureTask<Boolean>( | |
startAdhocCallable); | |
ExecutorService adhocExecutor = Executors.newFixedThreadPool(1); | |
adhocExecutor.execute(adhocFutureTask); | |
boolean adhocStarted = adhocFutureTask.get(); | |
if (adhocStarted) | |
{ | |
Log.i("ManetStarter.PrepareNetwork", | |
"Adhoc started successfully."); | |
} | |
else | |
{ | |
Log.i("ManetStarter.PrepareNetwork", | |
"Failed to start adhoc mode!"); | |
} | |
} | |
else | |
{ | |
Log.i("PrepareNetwork", "Service failed to start!"); | |
} | |
} | |
catch (InterruptedException e1) | |
{ | |
// TODO Auto-generated catch block | |
e1.printStackTrace(); | |
} | |
catch (ExecutionException e1) | |
{ | |
// TODO Auto-generated catch block | |
e1.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment