diff --git a/res/layout/main.xml b/res/layout/main.xml index 63329e86d028f6dcde0fd3b4c0a5def9e0d5a7c7..cf9ce895c3e4ad8a0784c4bc71ab086ed407e656 100644 --- a/res/layout/main.xml +++ b/res/layout/main.xml @@ -33,5 +33,11 @@ android:layout_height="wrap_content" android:text="Stop router" /> +<TextView + android:id="@+id/main_status_text" + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:text="" + /> </LinearLayout> diff --git a/res/raw/logger_config b/res/raw/logger_config index e87fa2b5d62488267708f0a208f7bf743bce228f..f73f9459b54f8154b9558ec320bde786457cb06d 100644 --- a/res/raw/logger_config +++ b/res/raw/logger_config @@ -4,6 +4,10 @@ logger.displayOnScreen=true logger.logFileSize=64k logger.logRotationLimit=2 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.peermanager=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.tunnel=WARN 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.LogWriter=WARN logger.record.net.i2p.util.NativeBigInteger=DEBUG -logger.record.net.org.cybergarage.util.debug=DEBUG +logger.record.net.org.cybergarage.util.Debug=DEBUG diff --git a/src/net/i2p/android/router/activity/MainActivity.java b/src/net/i2p/android/router/activity/MainActivity.java index e0eb59dbce9dfe17ded81f49136857c757958172..0162cb7a4d03da336a7b7b7443fec793db952e3f 100644 --- a/src/net/i2p/android/router/activity/MainActivity.java +++ b/src/net/i2p/android/router/activity/MainActivity.java @@ -7,12 +7,17 @@ import android.view.View; import android.widget.Button; import android.widget.TextView; +import java.text.DecimalFormat; + import net.i2p.android.router.R; +import net.i2p.data.DataHelper; +import net.i2p.router.RouterContext; public class MainActivity extends I2PActivityBase { private Handler _handler; private Runnable _updater; + private int _counter; /** Called when the activity is first created. */ @Override @@ -74,11 +79,14 @@ public class MainActivity extends I2PActivityBase { { super.onResume(); updateVisibility(); + updateStatus(); } private class Updater implements Runnable { public void run() { updateVisibility(); + if (++_counter % 3 == 0) + updateStatus(); _handler.postDelayed(this, 2500); } } @@ -92,4 +100,51 @@ public class MainActivity extends I2PActivityBase { Button stop = (Button) findViewById(R.id.router_stop_button); 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); + } + } } diff --git a/src/net/i2p/android/router/service/RouterService.java b/src/net/i2p/android/router/service/RouterService.java index ae3f830ace8ccf7a3f2088ba0ed7759df8af0858..d59893c608aec36851bb98168ba3e411d0ac4eb9 100644 --- a/src/net/i2p/android/router/service/RouterService.java +++ b/src/net/i2p/android/router/service/RouterService.java @@ -5,6 +5,7 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; +import android.os.Handler; import android.os.IBinder; import java.io.File; @@ -34,11 +35,12 @@ public class RouterService extends Service { //private String _apkPath; private State _state = State.INIT; private Thread _starterThread; - private Thread _statusThread; private StatusBar _statusBar; private I2PReceiver _receiver; private IBinder _binder; private final Object _stateLock = new Object(); + private Handler _handler; + private Runnable _updater; private static final String MARKER = "************************************** "; @@ -55,6 +57,8 @@ public class RouterService extends Service { //_apkPath = init.getAPKPath(); _statusBar = new StatusBar(this); _binder = new RouterBinder(this); + _handler = new Handler(); + _updater = new Updater(); } @Override @@ -78,6 +82,9 @@ public class RouterService extends Service { _starterThread.start(); } } + _handler.removeCallbacks(_updater); + _handler.postDelayed(_updater, 50); + //return START_STICKY; return START_NOT_STICKY; } @@ -130,8 +137,6 @@ public class RouterService extends Service { _context.router().setKillVMOnEnd(false); Job loadJob = new LoadClientsJob(_context); _context.jobQueue().addJob(loadJob); - _statusThread = new Thread(new StatusThread()); - _statusThread.start(); _context.addShutdownTask(new ShutdownHook()); _starterThread = null; } @@ -139,64 +144,49 @@ public class RouterService extends Service { } } - /** TODO change to a handler */ - private class StatusThread implements Runnable { + private class Updater implements Runnable { public void run() { - System.err.println(MARKER + this + " status thread started" + - " Current state is: " + _state); - try { - Thread.sleep(5*1000); - } catch (InterruptedException ie) {} - 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; - } + RouterContext ctx = _context; + if (ctx != null && _state == State.RUNNING) { + Router router = ctx.router(); + if (router.isAlive()) + updateStatus(ctx); } - System.err.println(MARKER + this + " status thread finished" + - " Current state is: " + _state); + _handler.postDelayed(this, 15*1000); } } + 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 public IBinder onBind(Intent intent) { @@ -289,6 +279,7 @@ public class RouterService extends Service { System.err.println("onDestroy called" + " Current state is: " + _state); + _handler.removeCallbacks(_updater); _statusBar.off(this); I2PReceiver rcvr = _receiver; @@ -328,8 +319,9 @@ public class RouterService extends Service { public void run() { System.err.println(MARKER + this + " stopper thread" + " Current state is: " + _state); - if (_context != null) - _context.router().shutdown(Router.EXIT_HARD); + RouterContext ctx = _context; + if (ctx != null) + ctx.router().shutdown(Router.EXIT_HARD); _statusBar.off(RouterService.this); System.err.println("********** Router shutdown complete"); synchronized (_stateLock) { @@ -356,6 +348,8 @@ public class RouterService extends Service { } } synchronized (_stateLock) { + // null out to release the memory + _context = null; if (_state == State.WAITING || _state == State.STARTING) _starterThread.interrupt(); if (_state == State.MANUAL_STOPPING) { @@ -368,8 +362,6 @@ public class RouterService extends Service { } else if (_state == State.STARTING || _state == State.RUNNING || _state == State.STOPPING) { _state = State.STOPPED; - if (_statusThread != null) - _statusThread.interrupt(); stopSelf(); } }