MLab/NDT: Hook test to wizard

Make helper the session scope, not handler
Reduce whitespace in formhandler.jsi
Notes on why FormHandler can't be session scope
Ajax still TODO
This commit is contained in:
zzz
2018-11-15 14:24:46 +00:00
parent 6462e2a292
commit dc343b05a2
5 changed files with 213 additions and 141 deletions

View File

@ -18,6 +18,8 @@ import net.i2p.util.Log;
* The form is "processed" after the properties are set and the first output
* property is retrieved - either getAll(), getNotices() or getErrors().
*
* This Handler will only process a single POST. The jsp bean must be declared scope=request.
*
*/
public abstract class FormHandler {
protected RouterContext _context;

View File

@ -2,9 +2,6 @@ package net.i2p.router.web.helpers;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import com.vuze.plugins.mlab.MLabRunner;
import net.i2p.router.Router;
import net.i2p.router.transport.FIFOBandwidthRefiller;
@ -12,28 +9,35 @@ import net.i2p.router.web.FormHandler;
/**
* The new user wizard.
* This bean has SESSION scope so the results may be retrieved.
* All necessary methods are synchronized.
*
* This handler has request scope (as all FormHandlers must), so it controls
* the test via the helper, which has session scope.
*
* @since 0.9.38
*/
public class WizardHandler extends FormHandler {
// session scope, but it's an underlying singleton
private MLabRunner _mlab;
// session scope
private TestListener _listener;
private WizardHelper _helper;
/**
* Bind the helper (scope session) to this handler (scope request)
*/
public void setWizardHelper(WizardHelper helper) {
_helper = helper;
}
@Override
public void setContextId(String contextId) {
super.setContextId(contextId);
_mlab = MLabRunner.getInstance(_context);
}
@Override
protected void processForm() {
{
String page = getJettyString("page");
System.out.println("Action: " + _action + " page: " + page);
}
if (_action == null)
return;
if (getJettyString("cancelbw") != null) {
cancelNDT();
return;
}
if (getJettyString("next") == null)
return;
if (_action.equals("blah")) {
@ -44,7 +48,9 @@ public class WizardHandler extends FormHandler {
// Saved in CSSHelper, assume success
addFormNoticeNoEscape(_t("Console language saved."));
}
if ("6".equals(page)) {
if ("4".equals(page)) {
startNDT();
} else if ("6".equals(page)) {
Map<String, String> changes = new HashMap<String, String>();
boolean updated = updateRates(changes);
if (updated) {
@ -116,120 +122,25 @@ public class WizardHandler extends FormHandler {
return updated;
}
public synchronized boolean isNDTComplete() {
return _listener != null && _listener.isComplete();
}
public synchronized boolean isNDTRunning() {
return _listener != null && !_listener.isComplete();
}
/**
* @return status string or null
*/
public synchronized String getCompletionStatus() {
return _listener != null ? _listener.getSummary() : null;
}
/**
* @return status string or null
*/
public synchronized String getDetailStatus() {
return _listener != null ? _listener.getDetail() : null;
}
/**
* @return bytes per second or 0
*/
public long getUpBandwidth() {
return getLongResult("up");
}
/**
* @return bytes per second or 0
*/
public long getDownBandwidth() {
return getLongResult("down");
}
public synchronized long getLongResult(String key) {
if (_listener != null) {
Map<String, Object> results = _listener.getResults();
if (results != null) {
Long v = (Long) results.get(key);
if (v != null)
return v.longValue();
}
}
return 0;
}
/** start the test */
public synchronized void startNDT() {
if (_mlab.isRunning() || _listener != null && !_listener.isComplete()) {
addFormError(_t("Bandwidth test is already running"));
return;
}
_listener = new TestListener();
MLabRunner.ToolRun runner = _mlab.runNDT(_listener);
if (runner != null) {
private void startNDT() {
if (_helper == null) {
addFormError("Bad state for test");
} else if (_helper.startNDT()) {
addFormNotice(_t("Started bandwidth test"));
} else {
Map<String, Object> map = new HashMap<String, Object>(2);
_listener.complete(map);
addFormError(_t("Bandwidth test is already running"));
}
}
/** cancel the test */
public synchronized void cancelNDT() {
synchronized(WizardHandler.class) {
if (!_mlab.isRunning()) {
addFormError(_t("Bandwidth test was not running"));
return;
}
/****
TODO
if (runner != null)
addFormNotice(_t("Started bandwidth test"));
else
addFormError(_t("Bandwidth test is already running"));
****/
private void cancelNDT() {
if (_helper == null) {
addFormError("Bad state for test");
} else if (_helper.cancelNDT()) {
addFormNotice(_t("Candelled bandwidth test"));
} else {
addFormError(_t("Bandwidth test was not running"));
}
}
/** test results */
private static class TestListener implements MLabRunner.ToolListener {
private String _summary, _detail;
private Map<String, Object> _results;
public synchronized void reportSummary(String str) {
_summary = str;
}
public synchronized void reportDetail(String str) {
_detail = str;
}
public synchronized void complete(Map<String, Object> results) {
_results = results;
}
public synchronized boolean isComplete() {
return _results != null;
}
public synchronized String getSummary() {
return _summary;
}
public synchronized String getDetail() {
return _detail;
}
public synchronized Map<String, Object> getResults() {
return _results;
}
}
}

View File

@ -1,17 +1,174 @@
package net.i2p.router.web.helpers;
import java.util.HashMap;
import java.util.Map;
import com.vuze.plugins.mlab.MLabRunner;
import net.i2p.data.DataHelper;
import net.i2p.router.web.HelperBase;
/**
* The new user wizard.
*
* This bean has SESSION scope so the results may be retrieved.
* All necessary methods are synchronized.
*
* @since 0.9.38
*/
public class WizardHelper extends HelperBase {
public static final String PROP_COMPLETE = "routerconsole.welcomeWizardComplete";
// session scope, but it's an underlying singleton
private MLabRunner _mlab;
// session scope
private TestListener _listener;
private MLabRunner.ToolRun _runner;
/**
* Overriden to only do this once.
*/
@Override
public void setContextId(String contextId) {
if (_context == null) {
super.setContextId(contextId);
_mlab = MLabRunner.getInstance(_context);
}
}
public void complete() {
_context.router().saveConfig(PROP_COMPLETE, "true");
}
public synchronized boolean isNDTComplete() {
return _listener != null && _listener.isComplete();
}
public synchronized boolean isNDTRunning() {
return _listener != null && !_listener.isComplete();
}
/**
* @return HTML-escaped status string or ""
*/
public synchronized String getCompletionStatus() {
String rv = "";
if (_listener != null) {
String s = _listener.getSummary();
if (s != null)
s = DataHelper.escapeHTML(s);
}
return rv;
}
/**
* @return HTML-escaped status string or ""
*/
public synchronized String getDetailStatus() {
String rv = "";
if (_listener != null) {
String s = _listener.getDetail();
if (s != null)
s = DataHelper.escapeHTML(s);
}
return rv;
}
/**
* @return bytes per second or 0
*/
public long getUpBandwidth() {
return getLongResult("up");
}
/**
* @return bytes per second or 0
*/
public long getDownBandwidth() {
return getLongResult("down");
}
public synchronized long getLongResult(String key) {
if (_listener != null) {
Map<String, Object> results = _listener.getResults();
if (results != null) {
Long v = (Long) results.get(key);
if (v != null)
return v.longValue();
}
}
return 0;
}
/**
* Start the test. Called from the Handler.
* @return success
*/
synchronized boolean startNDT() {
if (_mlab.isRunning() || _listener != null && !_listener.isComplete()) {
return false;
}
_listener = new TestListener();
_runner = _mlab.runNDT(_listener);
if (_runner != null) {
return true;
} else {
Map<String, Object> map = new HashMap<String, Object>(2);
_listener.complete(map);
return false;
}
}
/**
* Cancel the test. Called from the Handler.
* @return success
*/
synchronized boolean cancelNDT() {
if (!_mlab.isRunning()) {
return false;
}
if (_runner != null) {
_runner.cancel();
_runner = null;
return true;
} else {
return false;
}
}
/** test results */
private static class TestListener implements MLabRunner.ToolListener {
private String _summary, _detail;
private Map<String, Object> _results;
public synchronized void reportSummary(String str) {
_summary = str;
}
public synchronized void reportDetail(String str) {
_detail = str;
}
public synchronized void complete(Map<String, Object> results) {
_results = results;
}
public synchronized boolean isComplete() {
return _results != null;
}
public synchronized String getSummary() {
return _summary;
}
public synchronized String getDetail() {
return _detail;
}
public synchronized Map<String, Object> getResults() {
return _results;
}
}
}