simple wait for network thread. still need to hook up the binder

This commit is contained in:
zzz
2011-06-11 21:02:39 +00:00
parent a9a3fbd5da
commit 3f62eded02
2 changed files with 47 additions and 8 deletions

View File

@ -46,6 +46,11 @@ public class I2PReceiver extends BroadcastReceiver {
} }
} }
public boolean isConnected() {
NetworkInfo current = getInfo();
return current != null && current.isConnected();
}
private NetworkInfo getInfo() { private NetworkInfo getInfo() {
ConnectivityManager cm = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE); ConnectivityManager cm = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo current = cm.getActiveNetworkInfo(); NetworkInfo current = cm.getActiveNetworkInfo();

View File

@ -24,7 +24,7 @@ import net.i2p.util.NativeBigInteger;
* Runs the router * Runs the router
*/ */
public class RouterService extends Service { public class RouterService extends Service {
private enum State {INIT, STARTING, RUNNING, STOPPING, STOPPED} private enum State {INIT, WAITING, STARTING, RUNNING, STOPPING, STOPPED}
private RouterContext _context; private RouterContext _context;
private String _myDir; private String _myDir;
@ -33,7 +33,7 @@ public class RouterService extends Service {
private Thread _starterThread; private Thread _starterThread;
private Thread _statusThread; private Thread _statusThread;
private StatusBar _statusBar; private StatusBar _statusBar;
private BroadcastReceiver _receiver; private I2PReceiver _receiver;
private final Object _stateLock = new Object(); private final Object _stateLock = new Object();
private static final String MARKER = "************************************** "; private static final String MARKER = "************************************** ";
@ -58,16 +58,50 @@ public class RouterService extends Service {
synchronized (_stateLock) { synchronized (_stateLock) {
if (_state != State.INIT) if (_state != State.INIT)
return START_STICKY; return START_STICKY;
_statusBar.update("I2P is starting up");
_state = State.STARTING;
_starterThread = new Thread(new Starter());
_starterThread.start();
_receiver = new I2PReceiver(this); _receiver = new I2PReceiver(this);
if (_receiver.isConnected()) {
_statusBar.update("I2P is starting up");
_state = State.STARTING;
_starterThread = new Thread(new Starter());
_starterThread.start();
} else {
_statusBar.update("I2P is waiting for a network connection");
_state = State.WAITING;
_starterThread = new Thread(new Waiter());
_starterThread.start();
}
} }
return START_STICKY; return START_STICKY;
} }
/** maybe this goes away when the receiver can bind to us */
private class Waiter implements Runnable {
public void run() {
System.err.println(MARKER + this + " waiter thread" +
"Current state is: " + _state);
while (_state == State.WAITING) {
try {
Thread.sleep(30*1000);
} catch (InterruptedException ie) {
break;
}
if (_receiver.isConnected()) {
synchronized (_stateLock) {
if (_state != State.WAITING)
break;
_statusBar.update("Network connected, I2P is starting up");
_state = State.STARTING;
_starterThread = new Thread(new Starter());
_starterThread.start();
}
break;
}
}
System.err.println("waiter finished");
}
}
private class Starter implements Runnable { private class Starter implements Runnable {
public void run() { public void run() {
System.err.println(MARKER + this + " starter thread" + System.err.println(MARKER + this + " starter thread" +
@ -104,7 +138,7 @@ public class RouterService extends Service {
Router router = _context.router(); Router router = _context.router();
while (_state == State.RUNNING && router.isAlive()) { while (_state == State.RUNNING && router.isAlive()) {
try { try {
Thread.sleep(5000); Thread.sleep(15*1000);
} catch (InterruptedException ie) { } catch (InterruptedException ie) {
break; break;
} }