- Add status to main page in the Handler

- Convert service status bar updater from a thread to a Handler
- Clean up status bar
- Null out router context on shutdown
- Switch back to shared clients
This commit is contained in:
zzz
2011-06-17 22:05:20 +00:00
parent 3a7f4331f0
commit 05cdb11c55
4 changed files with 119 additions and 61 deletions

View File

@ -33,5 +33,11 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Stop router" android:text="Stop router"
/> />
<TextView
android:id="@+id/main_status_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
</LinearLayout> </LinearLayout>

View File

@ -4,6 +4,10 @@ logger.displayOnScreen=true
logger.logFileSize=64k logger.logFileSize=64k
logger.logRotationLimit=2 logger.logRotationLimit=2
logger.minimumOnScreenLevel=WARN logger.minimumOnScreenLevel=WARN
logger.record.net.i2p.crypto=WARN
logger.record.net.i2p.router.InNetMessagePool=WARN
logger.record.net.i2p.router.Router=INFO
logger.record.net.i2p.router.Shitlist=WARN
logger.record.net.i2p.router.networkdb=WARN logger.record.net.i2p.router.networkdb=WARN
logger.record.net.i2p.router.peermanager=WARN logger.record.net.i2p.router.peermanager=WARN
logger.record.net.i2p.router.peermanager.ProfileOrganizer=WARN logger.record.net.i2p.router.peermanager.ProfileOrganizer=WARN
@ -11,7 +15,8 @@ logger.record.net.i2p.router.transport=WARN
logger.record.net.i2p.router.transport.FIFOBandwidthRefiller=ERROR logger.record.net.i2p.router.transport.FIFOBandwidthRefiller=ERROR
logger.record.net.i2p.router.tunnel=WARN logger.record.net.i2p.router.tunnel=WARN
logger.record.net.i2p.stat.Rate=ERROR logger.record.net.i2p.stat.Rate=ERROR
logger.record.net.i2p.util.I2PThread=ERROR
logger.record.net.i2p.util.LogManager=WARN logger.record.net.i2p.util.LogManager=WARN
logger.record.net.i2p.util.LogWriter=WARN logger.record.net.i2p.util.LogWriter=WARN
logger.record.net.i2p.util.NativeBigInteger=DEBUG logger.record.net.i2p.util.NativeBigInteger=DEBUG
logger.record.net.org.cybergarage.util.debug=DEBUG logger.record.net.org.cybergarage.util.Debug=DEBUG

View File

@ -7,12 +7,17 @@ import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.TextView; import android.widget.TextView;
import java.text.DecimalFormat;
import net.i2p.android.router.R; import net.i2p.android.router.R;
import net.i2p.data.DataHelper;
import net.i2p.router.RouterContext;
public class MainActivity extends I2PActivityBase { public class MainActivity extends I2PActivityBase {
private Handler _handler; private Handler _handler;
private Runnable _updater; private Runnable _updater;
private int _counter;
/** Called when the activity is first created. */ /** Called when the activity is first created. */
@Override @Override
@ -74,11 +79,14 @@ public class MainActivity extends I2PActivityBase {
{ {
super.onResume(); super.onResume();
updateVisibility(); updateVisibility();
updateStatus();
} }
private class Updater implements Runnable { private class Updater implements Runnable {
public void run() { public void run() {
updateVisibility(); updateVisibility();
if (++_counter % 3 == 0)
updateStatus();
_handler.postDelayed(this, 2500); _handler.postDelayed(this, 2500);
} }
} }
@ -92,4 +100,51 @@ public class MainActivity extends I2PActivityBase {
Button stop = (Button) findViewById(R.id.router_stop_button); Button stop = (Button) findViewById(R.id.router_stop_button);
stop.setVisibility(showStop ? View.VISIBLE : View.INVISIBLE); stop.setVisibility(showStop ? View.VISIBLE : View.INVISIBLE);
} }
private void updateStatus() {
RouterContext ctx = getRouterContext();
TextView tv = (TextView) findViewById(R.id.main_status_text);
if (ctx != null) {
int active = ctx.commSystem().countActivePeers();
int known = Math.max(ctx.netDb().getKnownRouters() - 1, 0);
int inEx = ctx.tunnelManager().getFreeTunnelCount();
int outEx = ctx.tunnelManager().getOutboundTunnelCount();
int inCl = ctx.tunnelManager().getInboundClientTunnelCount();
int outCl = ctx.tunnelManager().getOutboundClientTunnelCount();
//int part = _context.tunnelManager().getParticipatingCount();
double dLag = ctx.statManager().getRate("jobQueue.jobLag").getRate(60000).getAverageValue();
String jobLag = DataHelper.formatDuration((long) dLag);
String msgDelay = DataHelper.formatDuration(ctx.throttle().getMessageDelay());
String uptime = DataHelper.formatDuration(ctx.router().getUptime());
//String tunnelStatus = _context.throttle().getTunnelStatus();
double inBW = ctx.bandwidthLimiter().getReceiveBps() / 1024;
double outBW = ctx.bandwidthLimiter().getSendBps() / 1024;
// control total width
DecimalFormat fmt;
if (inBW >= 1000 || outBW >= 1000)
fmt = new DecimalFormat("#0");
else if (inBW >= 100 || outBW >= 100)
fmt = new DecimalFormat("#0.0");
else
fmt = new DecimalFormat("#0.00");
String status =
"Router status: " +
" Peers " + active + '/' + known +
"; Expl. Tunnels " + inEx + '/' + outEx +
"; Client Tunnels " + inCl + '/' + outCl;
//" Pt " + part +
String details =
"; BW " + fmt.format(inBW) + '/' + fmt.format(outBW) + "K" +
"; Job Lag " + jobLag +
"; Msg Delay " + msgDelay +
"; Up " + uptime;
tv.setText(status + details);
tv.setVisibility(View.VISIBLE);
} else {
tv.setVisibility(View.INVISIBLE);
}
}
} }

View File

@ -5,6 +5,7 @@ import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import java.io.File; import java.io.File;
@ -34,11 +35,12 @@ public class RouterService extends Service {
//private String _apkPath; //private String _apkPath;
private State _state = State.INIT; private State _state = State.INIT;
private Thread _starterThread; private Thread _starterThread;
private Thread _statusThread;
private StatusBar _statusBar; private StatusBar _statusBar;
private I2PReceiver _receiver; private I2PReceiver _receiver;
private IBinder _binder; private IBinder _binder;
private final Object _stateLock = new Object(); private final Object _stateLock = new Object();
private Handler _handler;
private Runnable _updater;
private static final String MARKER = "************************************** "; private static final String MARKER = "************************************** ";
@ -55,6 +57,8 @@ public class RouterService extends Service {
//_apkPath = init.getAPKPath(); //_apkPath = init.getAPKPath();
_statusBar = new StatusBar(this); _statusBar = new StatusBar(this);
_binder = new RouterBinder(this); _binder = new RouterBinder(this);
_handler = new Handler();
_updater = new Updater();
} }
@Override @Override
@ -78,6 +82,9 @@ public class RouterService extends Service {
_starterThread.start(); _starterThread.start();
} }
} }
_handler.removeCallbacks(_updater);
_handler.postDelayed(_updater, 50);
//return START_STICKY; //return START_STICKY;
return START_NOT_STICKY; return START_NOT_STICKY;
} }
@ -130,8 +137,6 @@ public class RouterService extends Service {
_context.router().setKillVMOnEnd(false); _context.router().setKillVMOnEnd(false);
Job loadJob = new LoadClientsJob(_context); Job loadJob = new LoadClientsJob(_context);
_context.jobQueue().addJob(loadJob); _context.jobQueue().addJob(loadJob);
_statusThread = new Thread(new StatusThread());
_statusThread.start();
_context.addShutdownTask(new ShutdownHook()); _context.addShutdownTask(new ShutdownHook());
_starterThread = null; _starterThread = null;
} }
@ -139,64 +144,49 @@ public class RouterService extends Service {
} }
} }
/** TODO change to a handler */ private class Updater implements Runnable {
private class StatusThread implements Runnable {
public void run() { public void run() {
System.err.println(MARKER + this + " status thread started" + RouterContext ctx = _context;
" Current state is: " + _state); if (ctx != null && _state == State.RUNNING) {
try { Router router = ctx.router();
Thread.sleep(5*1000); if (router.isAlive())
} catch (InterruptedException ie) {} updateStatus(ctx);
Router router = _context.router();
while (_state == State.RUNNING && router.isAlive()) {
int active = _context.commSystem().countActivePeers();
int known = Math.max(_context.netDb().getKnownRouters() - 1, 0);
int inEx = _context.tunnelManager().getFreeTunnelCount();
int outEx = _context.tunnelManager().getOutboundTunnelCount();
int inCl = _context.tunnelManager().getInboundClientTunnelCount();
int outCl = _context.tunnelManager().getOutboundClientTunnelCount();
//int part = _context.tunnelManager().getParticipatingCount();
double dLag = _context.statManager().getRate("jobQueue.jobLag").getRate(60000).getAverageValue();
String jobLag = DataHelper.formatDuration((long) dLag);
String msgDelay = DataHelper.formatDuration(_context.throttle().getMessageDelay());
String uptime = DataHelper.formatDuration(router.getUptime());
//String tunnelStatus = _context.throttle().getTunnelStatus();
double inBW = _context.bandwidthLimiter().getReceiveBps() / 1024;
double outBW = _context.bandwidthLimiter().getSendBps() / 1024;
// control total width
DecimalFormat fmt;
if (inBW >= 1000 || outBW >= 1000)
fmt = new DecimalFormat("#0");
else if (inBW >= 100 || outBW >= 100)
fmt = new DecimalFormat("#0.0");
else
fmt = new DecimalFormat("#0.00");
String status =
"I2P " +
" Pr " + active + '/' + known +
" Ex " + inEx + '/' + outEx +
" Cl " + inCl + '/' + outCl;
//" Pt " + part +
String details =
"BW " + fmt.format(inBW) + '/' + fmt.format(outBW) + "K" +
" Lg " + jobLag +
" Dy " + msgDelay +
" Up " + uptime;
_statusBar.update(status, details);
try {
Thread.sleep(15*1000);
} catch (InterruptedException ie) {
break;
}
} }
System.err.println(MARKER + this + " status thread finished" + _handler.postDelayed(this, 15*1000);
" Current state is: " + _state);
} }
} }
private void updateStatus(RouterContext ctx) {
int active = ctx.commSystem().countActivePeers();
int known = Math.max(ctx.netDb().getKnownRouters() - 1, 0);
int inEx = ctx.tunnelManager().getFreeTunnelCount();
int outEx = ctx.tunnelManager().getOutboundTunnelCount();
int inCl = ctx.tunnelManager().getInboundClientTunnelCount();
int outCl = ctx.tunnelManager().getOutboundClientTunnelCount();
String uptime = DataHelper.formatDuration(ctx.router().getUptime());
double inBW = ctx.bandwidthLimiter().getReceiveBps() / 1024;
double outBW = ctx.bandwidthLimiter().getSendBps() / 1024;
// control total width
DecimalFormat fmt;
if (inBW >= 1000 || outBW >= 1000)
fmt = new DecimalFormat("#0");
else if (inBW >= 100 || outBW >= 100)
fmt = new DecimalFormat("#0.0");
else
fmt = new DecimalFormat("#0.00");
String status =
"I2P " +
active + '/' + known + " peers connected";
String details =
fmt.format(inBW) + '/' + fmt.format(outBW) + " KBps" +
"; Expl " + inEx + '/' + outEx +
"; Client " + inCl + '/' + outCl;
_statusBar.update(status, details);
}
@Override @Override
public IBinder onBind(Intent intent) public IBinder onBind(Intent intent)
{ {
@ -289,6 +279,7 @@ public class RouterService extends Service {
System.err.println("onDestroy called" + System.err.println("onDestroy called" +
" Current state is: " + _state); " Current state is: " + _state);
_handler.removeCallbacks(_updater);
_statusBar.off(this); _statusBar.off(this);
I2PReceiver rcvr = _receiver; I2PReceiver rcvr = _receiver;
@ -328,8 +319,9 @@ public class RouterService extends Service {
public void run() { public void run() {
System.err.println(MARKER + this + " stopper thread" + System.err.println(MARKER + this + " stopper thread" +
" Current state is: " + _state); " Current state is: " + _state);
if (_context != null) RouterContext ctx = _context;
_context.router().shutdown(Router.EXIT_HARD); if (ctx != null)
ctx.router().shutdown(Router.EXIT_HARD);
_statusBar.off(RouterService.this); _statusBar.off(RouterService.this);
System.err.println("********** Router shutdown complete"); System.err.println("********** Router shutdown complete");
synchronized (_stateLock) { synchronized (_stateLock) {
@ -356,6 +348,8 @@ public class RouterService extends Service {
} }
} }
synchronized (_stateLock) { synchronized (_stateLock) {
// null out to release the memory
_context = null;
if (_state == State.WAITING || _state == State.STARTING) if (_state == State.WAITING || _state == State.STARTING)
_starterThread.interrupt(); _starterThread.interrupt();
if (_state == State.MANUAL_STOPPING) { if (_state == State.MANUAL_STOPPING) {
@ -368,8 +362,6 @@ public class RouterService extends Service {
} else if (_state == State.STARTING || _state == State.RUNNING || } else if (_state == State.STARTING || _state == State.RUNNING ||
_state == State.STOPPING) { _state == State.STOPPING) {
_state = State.STOPPED; _state = State.STOPPED;
if (_statusThread != null)
_statusThread.interrupt();
stopSelf(); stopSelf();
} }
} }