* ClientAppManager: Add method to look up clients by class and args

* Console: Implement stopping of clients using the ClientApp interface
            (ticket #347)
This commit is contained in:
zzz
2013-04-16 14:59:18 +00:00
parent 7d0f626fd5
commit a3aee79e9c
7 changed files with 137 additions and 30 deletions

View File

@ -11,9 +11,12 @@ import java.util.Map;
import java.util.Properties;
import java.util.Set;
import net.i2p.app.ClientApp;
import net.i2p.app.ClientAppState;
import net.i2p.router.client.ClientManagerFacadeImpl;
import net.i2p.router.startup.ClientAppConfig;
import net.i2p.router.startup.LoadClientAppsJob;
import net.i2p.router.startup.RouterAppManager;
import net.i2p.router.update.ConsoleUpdateManager;
import static net.i2p.update.UpdateType.*;
@ -214,10 +217,12 @@ public class ConfigClientsHandler extends FormHandler {
ClientAppConfig.writeClientAppConfig(_context, clients);
addFormNotice(_("Client configuration saved successfully"));
addFormNotice(_("Restart required to take effect"));
//addFormNotice(_("Restart required to take effect"));
}
// STUB for stopClient, not completed yet.
/**
* @since Implemented in 0.9.6 using ClientAppManager
*/
private void stopClient(int i) {
List<ClientAppConfig> clients = ClientAppConfig.getClientApps(_context);
if (i >= clients.size()) {
@ -225,10 +230,23 @@ public class ConfigClientsHandler extends FormHandler {
return;
}
ClientAppConfig ca = clients.get(i);
//
// What do we do here?
//
addFormNotice(_("Client") + ' ' + _(ca.clientName) + ' ' + _("stopped") + '.');
ClientApp clientApp = _context.clientAppManager().getClientApp(ca.className, LoadClientAppsJob.parseArgs(ca.args));
if (clientApp != null && clientApp.getState() == ClientAppState.RUNNING) {
try {
// todo parseArgs(ca.stopArgs) ?
clientApp.shutdown(null);
addFormNotice(_("Client {0} stopped", ca.clientName));
// Give a chance for status to update
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {}
} catch (Throwable t) {
addFormError("Cannot stop client " + ca.className + ": " + t);
_log.error("Error stopping client " + ca.className, t);
}
} else {
addFormError("Cannot stop client " + i + ": " + ca.className);
}
}
private void startClient(int i) {
@ -239,7 +257,11 @@ public class ConfigClientsHandler extends FormHandler {
}
ClientAppConfig ca = clients.get(i);
LoadClientAppsJob.runClient(ca.className, ca.clientName, LoadClientAppsJob.parseArgs(ca.args), _context, _log);
addFormNotice(_("Client") + ' ' + _(ca.clientName) + ' ' + _("started") + '.');
addFormNotice(_("Client {0} started", ca.clientName));
// Give a chance for status to update
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {}
}
private void deleteClient(int i) {
@ -250,7 +272,7 @@ public class ConfigClientsHandler extends FormHandler {
}
ClientAppConfig ca = clients.remove(i);
ClientAppConfig.writeClientAppConfig(_context, clients);
addFormNotice(_("Client") + ' ' + _(ca.clientName) + ' ' + _("deleted") + '.');
addFormNotice(_("Client {0} deleted", ca.clientName));
}
private void saveWebAppChanges() {

View File

@ -9,9 +9,13 @@ import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
import net.i2p.app.ClientApp;
import net.i2p.app.ClientAppState;
import net.i2p.data.DataHelper;
import net.i2p.router.client.ClientManagerFacadeImpl;
import net.i2p.router.startup.ClientAppConfig;
import net.i2p.router.startup.LoadClientAppsJob;
import net.i2p.router.startup.RouterAppManager;
import net.i2p.util.Addresses;
public class ConfigClientsHelper extends HelperBase {
@ -94,17 +98,33 @@ public class ConfigClientsHelper extends HelperBase {
List<ClientAppConfig> clients = ClientAppConfig.getClientApps(_context);
for (int cur = 0; cur < clients.size(); cur++) {
ClientAppConfig ca = clients.get(cur);
renderForm(buf, ""+cur, ca.clientName, false, !ca.disabled,
boolean isConsole = ca.className.equals("net.i2p.router.web.RouterConsoleRunner");
boolean showStart;
boolean showStop;
if (isConsole) {
showStart = false;
showStop = false;
} else {
ClientApp clientApp = _context.clientAppManager().getClientApp(ca.className, LoadClientAppsJob.parseArgs(ca.args));
showStart = clientApp == null;
showStop = clientApp != null && clientApp.getState() == ClientAppState.RUNNING;
}
renderForm(buf, ""+cur, ca.clientName,
// urlify, enabled
false, !ca.disabled,
// read only
// dangerous, but allow editing the console args too
//"webConsole".equals(ca.clientName) || "Web console".equals(ca.clientName),
false,
// description, edit
ca.className + ((ca.args != null) ? " " + ca.args : ""), (""+cur).equals(_edit),
true, false,
// Enable this one and comment out the false below once the stub is filled in.
//!ca.disabled && !("webConsole".equals(ca.clientName) || "Web console".equals(ca.clientName)),
false,
true, ca.disabled);
// show edit button, show update button
// Don't allow edit if it's running, or else we would lose the "handle" to the ClientApp to stop it.
!showStop, false,
// show stop button
showStop,
// show delete button, show start button
true, showStart);
}
if ("new".equals(_edit))
@ -258,10 +278,10 @@ public class ConfigClientsHelper extends HelperBase {
if (showStartButton && (!ro) && !edit) {
buf.append("<button type=\"submit\" class=\"Xaccept\" name=\"action\" value=\"Start ").append(index).append("\" >" + _("Start") + "<span class=hide> ").append(index).append("</span></button>");
}
if (showEditButton && (!edit) && !ro)
buf.append("<button type=\"submit\" class=\"Xadd\" name=\"edit\" value=\"Edit ").append(index).append("\" >" + _("Edit") + "<span class=hide> ").append(index).append("</span></button>");
if (showStopButton && (!edit))
buf.append("<button type=\"submit\" class=\"Xstop\" name=\"action\" value=\"Stop ").append(index).append("\" >" + _("Stop") + "<span class=hide> ").append(index).append("</span></button>");
if (showEditButton && (!edit) && !ro)
buf.append("<button type=\"submit\" class=\"Xadd\" name=\"edit\" value=\"Edit ").append(index).append("\" >" + _("Edit") + "<span class=hide> ").append(index).append("</span></button>");
if (showUpdateButton && (!edit) && !ro) {
buf.append("<button type=\"submit\" class=\"Xcheck\" name=\"action\" value=\"Check ").append(index).append("\" >" + _("Check for updates") + "<span class=hide> ").append(index).append("</span></button>");
buf.append("<button type=\"submit\" class=\"Xdownload\" name=\"action\" value=\"Update ").append(index).append("\" >" + _("Update") + "<span class=hide> ").append(index).append("</span></button>");