-
-
Save daviddoria/e34c6ee2c4b5447a8800 to your computer and use it in GitHub Desktop.
A "one step" way to start Manet Manager
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.Timer; | |
import java.util.TimerTask; | |
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); | |
} | |
} | |
/** | |
* This class tries to connect to the MANET Manager service. It it can | |
* connect to the service, it uses StartAdhocThread to try to start adhoc | |
* mode. Both of these steps are protected by timeout timers to prevent | |
* infinite loops if something goes wrong. | |
*/ | |
public class ConnectToServiceAndStartAdhocThread extends Thread implements | |
ManetObserver | |
{ | |
// This timer is used to declare connection impossible if a connection | |
// has not been made | |
// after a timeout period has elapsed. | |
Timer timeoutTimer = new Timer(); | |
int timeout = 2000; // milliseconds | |
// This thread attempts to start adhoc mode once the connection to the | |
// service is verified. | |
StartAdhocThread startAdhocThread = new StartAdhocThread(); | |
@Override | |
public void run() | |
{ | |
// For some reason the connection to the service is not valid until | |
// after a second or so | |
// Log.i("ConnectToServiceAndStartAdhocThread:run", | |
// "Checking status immediately..."); | |
// CheckStatus(); | |
// Can't do this - | |
// "RuntimeException: Can't create handler inside thread that has not called Looper.prepare()" | |
// mManetHelper.connectToService(); | |
mActivity.runOnUiThread(new Runnable() | |
{ | |
public void run() | |
{ | |
mManetHelper.connectToService(); | |
} | |
}); | |
TimeoutTask timeoutTask = new TimeoutTask(); | |
timeoutTask | |
.setMessage("Unable to connect to service before timeout!"); | |
timeoutTimer.schedule(timeoutTask, timeout); | |
} | |
/** | |
* If we connected to the service successfully, then try to start adhoc | |
* mode. | |
*/ | |
@Override | |
public void onServiceConnected() | |
{ | |
Log.i("ConnectToServiceAndStartAdhocThread", "onServiceConnected()"); | |
if (mStarted == true) | |
{ | |
return; | |
} | |
timeoutTimer.cancel(); | |
Log.i("ConnectToServiceAndStartAdhocThread:run", | |
"Connected to service, so starting adhoc..."); | |
// isAlive may return a false negative if not enough time has | |
// elapsed since the thread was started | |
// if(!startAdhocThread.isAlive()) | |
// { | |
// startAdhocThread.start(); | |
// } | |
// Potentially use this: if(mythread.getState() == Thead.State.NEW) | |
// instead | |
try | |
{ | |
startAdhocThread.start(); | |
} | |
catch (Exception e) | |
{ | |
// do nothing (the thread was probably already started) | |
} | |
} | |
@Override | |
public void onServiceDisconnected() | |
{ | |
// Required by ManetObserver interface | |
} | |
@Override | |
public void onServiceStarted() | |
{ | |
// Required by ManetObserver interface | |
} | |
@Override | |
public void onServiceStopped() | |
{ | |
// Required by ManetObserver interface | |
} | |
@Override | |
public void onAdhocStateUpdated(AdhocStateEnum state, String info) | |
{ | |
// Required by ManetObserver interface | |
} | |
@Override | |
public void onConfigUpdated(ManetConfig manetcfg) | |
{ | |
// Required by ManetObserver interface | |
} | |
@Override | |
public void onPeersUpdated(HashSet<Node> peers) | |
{ | |
// Required by ManetObserver interface | |
} | |
@Override | |
public void onRoutingInfoUpdated(String info) | |
{ | |
// Required by ManetObserver interface | |
} | |
@Override | |
public void onError(String error) | |
{ | |
// Required by ManetObserver interface | |
} | |
} | |
/** | |
* This class starts a progress dialog and stops it after either successful | |
* starting of adhoc mode or a timeout. | |
*/ | |
public class StartAdhocThread extends Thread implements ManetObserver | |
{ | |
// This is used to effectively block the UI (by displaying a dialog) | |
// without | |
// actually blocking the UI thread. | |
ProgressDialog progressDialog = null; | |
// This timer is used to declare that starting adhoc mode has failed if | |
// it is not started | |
// before a timeout period has elapsed. | |
Timer timeoutTimer = new Timer(); | |
int timeout = 2000; // milliseconds | |
@Override | |
public void run() | |
{ | |
mActivity.runOnUiThread(new Runnable() | |
{ | |
public void run() | |
{ | |
progressDialog = ProgressDialog.show(mActivity, "", | |
"Ensuring ad-hoc mode is started. Please wait...", | |
true); | |
progressDialog.show(); | |
} | |
}); | |
mManetHelper.sendStartAdhocCommand(); | |
TimeoutTask timeoutTask = new TimeoutTask(); | |
timeoutTask | |
.setMessage("Unable to start adhoc mode before timeout!"); | |
timeoutTimer.schedule(timeoutTask, timeout); | |
} | |
@Override | |
public void onServiceConnected() | |
{ | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onServiceDisconnected() | |
{ | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onServiceStarted() | |
{ | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onServiceStopped() | |
{ | |
// Required by ManetObserver interface | |
} | |
@Override | |
public void onAdhocStateUpdated(AdhocStateEnum state, String info) | |
{ | |
if (state == AdhocStateEnum.STARTED) | |
{ | |
Log.i("WorkerThread:onAdhocStateUpdated", "Adhoc is started!"); | |
mStarted = true; | |
timeoutTimer.cancel(); | |
} | |
else | |
{ | |
Log.i("WorkerThread:onAdhocStateUpdated", | |
"Adhoc is not started!"); | |
} | |
if (progressDialog != null) | |
{ | |
progressDialog.dismiss(); | |
} | |
// No matter what happens, disconnect from the service | |
// Log.i("ManetStarter.StartAdhocThread.onAdhocStateUpdated", | |
// "Disconnecting from service..."); | |
// mManetHelper.disconnectFromService(); | |
} | |
@Override | |
public void onConfigUpdated(ManetConfig manetcfg) | |
{ | |
// Required by ManetObserver interface | |
} | |
@Override | |
public void onPeersUpdated(HashSet<Node> peers) | |
{ | |
// Required by ManetObserver interface | |
} | |
@Override | |
public void onRoutingInfoUpdated(String info) | |
{ | |
// Required by ManetObserver interface | |
} | |
@Override | |
public void onError(String error) | |
{ | |
// Required by ManetObserver interface | |
} | |
}; // end StartAdhocThread class | |
public void PrepareNetwork() | |
{ | |
Log.i("ManetStarter.PrepareNetwork()", | |
"Preparing network (connecting to service and starting adhoc)..."); | |
ConnectToServiceAndStartAdhocThread connectToServiceAndStartAdhocThread = new ConnectToServiceAndStartAdhocThread(); | |
mManetHelper.registerObserver(connectToServiceAndStartAdhocThread); | |
// We have to register this here as well or sometimes we get | |
// ConcurrentModificationException from | |
// ManetHelper.ManetServiceConnection.onServiceConnected | |
mManetHelper | |
.registerObserver(connectToServiceAndStartAdhocThread.startAdhocThread); | |
connectToServiceAndStartAdhocThread.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment