toad wrote:
> Why should a client care about WithDefaults ?
>
This way the client knows what all the settings are set to without
having to know anything special about Fred's current thoughts on what
the defaults are.
> Also, clients should set one or two settings to change, not the whole
> lot; there may be other changes going on in parallel.
>
I believe the approach I used will set anywhere from one to all of the
settings at once, depending on the needs of the client. I tested it by
changing my node's name without providing any other items.
> On Sun, Oct 29, 2006 at 11:34:28PM +0000, zothar at freenetproject.org wrote:
>
>> Author: zothar
>> Date: 2006-10-29 23:34:22 +0000 (Sun, 29 Oct 2006)
>> New Revision: 10745
>>
>> Added:
>> trunk/freenet/src/freenet/node/fcp/ConfigData.java
>> trunk/freenet/src/freenet/node/fcp/GetConfig.java
>> trunk/freenet/src/freenet/node/fcp/ModifyConfig.java
>> Modified:
>> trunk/freenet/src/freenet/config/FilePersistentConfig.java
>> trunk/freenet/src/freenet/config/SubConfig.java
>> trunk/freenet/src/freenet/node/fcp/FCPMessage.java
>> Log:
>> Added node configuration getting and modifying via FCP.
>>
>> Modified: trunk/freenet/src/freenet/config/FilePersistentConfig.java
>> ===================================================================
>> --- trunk/freenet/src/freenet/config/FilePersistentConfig.java
>> 2006-10-29 22:30:31 UTC (rev 10744)
>> +++ trunk/freenet/src/freenet/config/FilePersistentConfig.java
>> 2006-10-29 23:34:22 UTC (rev 10745)
>> @@ -170,14 +170,18 @@
>> }
>> }
>>
>> - private synchronized SimpleFieldSet exportFieldSet() {
>> + private SimpleFieldSet exportFieldSet() {
>> + return exportFieldSet(false);
>> + }
>> +
>> + public synchronized SimpleFieldSet exportFieldSet(boolean withDefaults)
>> {
>> SimpleFieldSet fs = new SimpleFieldSet();
>> SubConfig[] configs;
>> synchronized(this) {
>> configs = (SubConfig[])
>> configsByPrefix.values().toArray(new SubConfig[configsByPrefix.size()]);
>> }
>> for(int i=0;i<configs.length;i++) {
>> - SimpleFieldSet scfs = configs[i].exportFieldSet();
>> + SimpleFieldSet scfs =
>> configs[i].exportFieldSet(withDefaults);
>> fs.tput(configs[i].prefix, scfs);
>> }
>> return fs;
>>
>> Modified: trunk/freenet/src/freenet/config/SubConfig.java
>> ===================================================================
>> --- trunk/freenet/src/freenet/config/SubConfig.java 2006-10-29 22:30:31 UTC
>> (rev 10744)
>> +++ trunk/freenet/src/freenet/config/SubConfig.java 2006-10-29 23:34:22 UTC
>> (rev 10745)
>> @@ -181,6 +181,10 @@
>> }
>>
>> public SimpleFieldSet exportFieldSet() {
>> + return exportFieldSet(false);
>> + }
>> +
>> + public SimpleFieldSet exportFieldSet(boolean withDefaults) {
>> SimpleFieldSet fs = new SimpleFieldSet();
>> Set entrySet = map.entrySet();
>> Iterator i = entrySet.iterator();
>> @@ -188,7 +192,7 @@
>> Map.Entry entry = (Map.Entry) i.next();
>> String key = (String) entry.getKey();
>> Option o = (Option) entry.getValue();
>> - if(o.isDefault() && !o.forceWrite) continue;
>> + if(!withDefaults && o.isDefault() && !o.forceWrite)
>> continue;
>> fs.put(key, o.getValueString());
>> }
>> return fs;
>>
>> Added: trunk/freenet/src/freenet/node/fcp/ConfigData.java
>> ===================================================================
>> --- trunk/freenet/src/freenet/node/fcp/ConfigData.java 2006-10-29
>> 22:30:31 UTC (rev 10744)
>> +++ trunk/freenet/src/freenet/node/fcp/ConfigData.java 2006-10-29
>> 23:34:22 UTC (rev 10745)
>> @@ -0,0 +1,40 @@
>> +/* This code is part of Freenet. It is distributed under the GNU General
>> + * Public License, version 2 (or at your option any later version). See
>> + * http://www.gnu.org/ for further details of the GPL. */
>> +package freenet.node.fcp;
>> +
>> +import freenet.node.Node;
>> +import freenet.node.PeerNode;
>> +import freenet.support.SimpleFieldSet;
>> +
>> +public class ConfigData extends FCPMessage {
>> + static final String name = "ConfigData";
>> +
>> + final Node node;
>> + final boolean withDefaults;
>> +
>> + public ConfigData(Node node, boolean withDefaults) {
>> + this.node = node;
>> + this.withDefaults = withDefaults;
>> + }
>> +
>> + public SimpleFieldSet getFieldSet() {
>> + SimpleFieldSet fs = new SimpleFieldSet();
>> + if(withDefaults) {
>> + fs = node.config.exportFieldSet(true);
>> + } else {
>> + fs = node.config.exportFieldSet(false);
>> + }
>> + return fs;
>> + }
>> +
>> + public String getName() {
>> + return name;
>> + }
>> +
>> + public void run(FCPConnectionHandler handler, Node node)
>> + throws MessageInvalidException {
>> + throw new
>> MessageInvalidException(ProtocolErrorMessage.INVALID_MESSAGE, "ConfigData
>> goes from server to client not the other way around", null);
>> + }
>> +
>> +}
>>
>> Modified: trunk/freenet/src/freenet/node/fcp/FCPMessage.java
>> ===================================================================
>> --- trunk/freenet/src/freenet/node/fcp/FCPMessage.java 2006-10-29
>> 22:30:31 UTC (rev 10744)
>> +++ trunk/freenet/src/freenet/node/fcp/FCPMessage.java 2006-10-29
>> 23:34:22 UTC (rev 10745)
>> @@ -44,6 +44,8 @@
>> return new ClientPutMessage(fs);
>> if(name.equals(GenerateSSKMessage.name))
>> return new GenerateSSKMessage(fs);
>> + if(name.equals(GetConfig.name))
>> + return new GetConfig(fs);
>> if(name.equals(GetNode.name))
>> return new GetNode(fs);
>> if(name.equals(GetRequestStatusMessage.name))
>> @@ -52,6 +54,8 @@
>> return new ListPeersMessage(fs);
>> if(name.equals(ListPersistentRequestsMessage.name))
>> return new ListPersistentRequestsMessage(fs);
>> + if(name.equals(ModifyConfig.name))
>> + return new ModifyConfig(fs);
>> if(name.equals(ModifyPeer.name))
>> return new ModifyPeer(fs);
>> if(name.equals(ModifyPersistentRequest.name))
>>
>> Added: trunk/freenet/src/freenet/node/fcp/GetConfig.java
>> ===================================================================
>> --- trunk/freenet/src/freenet/node/fcp/GetConfig.java 2006-10-29
>> 22:30:31 UTC (rev 10744)
>> +++ trunk/freenet/src/freenet/node/fcp/GetConfig.java 2006-10-29
>> 23:34:22 UTC (rev 10745)
>> @@ -0,0 +1,33 @@
>> +/* This code is part of Freenet. It is distributed under the GNU General
>> + * Public License, version 2 (or at your option any later version). See
>> + * http://www.gnu.org/ for further details of the GPL. */
>> +package freenet.node.fcp;
>> +
>> +import freenet.node.Node;
>> +import freenet.node.PeerNode;
>> +import freenet.support.Fields;
>> +import freenet.support.SimpleFieldSet;
>> +
>> +public class GetConfig extends FCPMessage {
>> +
>> + final boolean withDefaults;
>> + static final String name = "GetConfig";
>> +
>> + public GetConfig(SimpleFieldSet fs) {
>> + withDefaults = Fields.stringToBool(fs.get("WithDefaults"),
>> false);
>> + }
>> +
>> + public SimpleFieldSet getFieldSet() {
>> + return new SimpleFieldSet();
>> + }
>> +
>> + public String getName() {
>> + return name;
>> + }
>> +
>> + public void run(FCPConnectionHandler handler, Node node)
>> + throws MessageInvalidException {
>> + handler.outputHandler.queue(new ConfigData(node, true));
>> + }
>> +
>> +}
>>
>> Added: trunk/freenet/src/freenet/node/fcp/ModifyConfig.java
>> ===================================================================
>> --- trunk/freenet/src/freenet/node/fcp/ModifyConfig.java 2006-10-29
>> 22:30:31 UTC (rev 10744)
>> +++ trunk/freenet/src/freenet/node/fcp/ModifyConfig.java 2006-10-29
>> 23:34:22 UTC (rev 10745)
>> @@ -0,0 +1,67 @@
>> +/* This code is part of Freenet. It is distributed under the GNU General
>> + * Public License, version 2 (or at your option any later version). See
>> + * http://www.gnu.org/ for further details of the GPL. */
>> +package freenet.node.fcp;
>> +
>> +import freenet.config.Config;
>> +import freenet.config.Option;
>> +import freenet.config.SubConfig;
>> +import freenet.node.Node;
>> +import freenet.node.NodeClientCore;
>> +import freenet.node.PeerNode;
>> +import freenet.support.Fields;
>> +import freenet.support.Logger;
>> +import freenet.support.SimpleFieldSet;
>> +
>> +public class ModifyConfig extends FCPMessage {
>> +
>> + static final String name = "ModifyConfig";
>> +
>> + final SimpleFieldSet fs;
>> +
>> + public ModifyConfig(SimpleFieldSet fs) {
>> + this.fs = fs;
>> + }
>> +
>> + public SimpleFieldSet getFieldSet() {
>> + return new SimpleFieldSet();
>> + }
>> +
>> + public String getName() {
>> + return name;
>> + }
>> +
>> + public void run(FCPConnectionHandler handler, Node node) throws
>> MessageInvalidException {
>> + Config config = node.config;
>> + SubConfig[] sc = config.getConfigs();
>> +
>> + boolean logMINOR = Logger.shouldLog(Logger.MINOR, this);
>> +
>> + for(int i=0; i<sc.length ; i++){
>> + Option[] o = sc[i].getOptions();
>> + String prefix = new String(sc[i].getPrefix());
>> + String configName;
>> +
>> + for(int j=0; j<o.length; j++){
>> + configName=o[j].getName();
>> + if(logMINOR) Logger.minor(this, "Setting
>> "+prefix+"."+configName);
>> +
>> + // we ignore unreconized parameters
>> + if(fs.get(prefix+"."+configName) != null) {
>> +
>> if(!(o[j].getValueString().equals(fs.get(prefix+"."+configName)))){
>> + if(logMINOR) Logger.minor(this,
>> "Setting "+prefix+"."+configName+" to "+fs.get(prefix+"."+configName));
>> + try{
>> +
>> o[j].setValue(fs.get(prefix+"."+configName));
>> + }catch(Exception e){
>> + // Bad values silently
>> fail from an FCP perspective, but the FCP client can tell if a change took
>> by comparing ConfigData messages before and after
>> + Logger.error(this,
>> "Caught "+e, e);
>> + }
>> + }
>> + }
>> + }
>> + }
>> + node.clientCore.storeConfig();
>> + handler.outputHandler.queue(new ConfigData(node, true));
>> + }
>> +
>> +}
>>