-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from freenet-mobile/node-config
Refactor install class into NodeController and Config classes
- Loading branch information
Showing
11 changed files
with
722 additions
and
287 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package org.freenetproject.mobile; | ||
|
||
import org.apache.commons.lang3.*; | ||
|
||
import java.io.*; | ||
import java.nio.file.*; | ||
import java.util.*; | ||
|
||
/** | ||
* Small abstraction over java.util.Properties class. | ||
*/ | ||
class Config { | ||
private final String properties = "freenet.ini"; | ||
private Properties config = new Properties(); | ||
|
||
/** | ||
* Set a given value under key. | ||
* | ||
* @param key Key name. | ||
* @param val Value. | ||
*/ | ||
public void set(String key, String val) { | ||
config.setProperty(key, val); | ||
} | ||
|
||
/** | ||
* Gets the value for configuration key, defaults to "" | ||
* if the configuration key is not present. | ||
* | ||
* @param key Configuration key. | ||
* @return Value. | ||
*/ | ||
public String get(String key) { | ||
return config.getProperty(key, ""); | ||
} | ||
|
||
/** | ||
* Gets the value for configuration key, defaults to defaultValue | ||
* if the configuration key is not present. | ||
* | ||
* @param key Configuration key. | ||
* @return Value. | ||
*/ | ||
public String get(String key, String defaultValue) { | ||
return config.getProperty(key, defaultValue); | ||
} | ||
|
||
/** | ||
* Loads node configuration or creates one under path with default values. | ||
* | ||
* @param path Path where the node is located. | ||
*/ | ||
public void loadOrDefault(Path path) throws IOException { | ||
Path config = Paths.get(path.toString(), properties); | ||
|
||
if (Files.exists(config)) { | ||
InputStream is = new FileInputStream(config.toFile()); | ||
this.config = new Properties(); | ||
this.config.load(is); | ||
return; | ||
} | ||
|
||
this.config = getDefaultConfig(path); | ||
persist(); | ||
} | ||
|
||
/** | ||
* Stores the current configuration to filesystem. | ||
* | ||
* This method saves the configuration options without using the Properties#store method. | ||
* Thus avoiding the escaping of : (used for IPv6 for example) and other especial characters | ||
* that freenet configuration contains. | ||
*/ | ||
public void persist() throws IOException { | ||
File dest = Paths.get(this.config.getProperty("node.install.cfgDir"), properties).toFile(); | ||
FileWriter writer = new FileWriter(dest); | ||
this.config.forEach((k, v) -> { | ||
try { | ||
writer.write(String.format("%s=%s%n", k, v)); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
}); | ||
writer.write(String.format("%s%n", "End")); | ||
writer.close(); | ||
} | ||
|
||
/** | ||
* Gets the default configuration and update the paths before returning it. | ||
* | ||
* @param path Node path. | ||
* @return Configured node. | ||
*/ | ||
private Properties getDefaultConfig(Path path) throws IOException { | ||
InputStream defaultConfig = getClass() | ||
.getClassLoader() | ||
.getResourceAsStream( | ||
Paths.get("defaults", properties).toString() | ||
); | ||
|
||
Properties config = new Properties(); | ||
config.load(defaultConfig); | ||
String dir = path.toString(); | ||
config.setProperty("node.install.cfgDir", dir); | ||
config.setProperty("node.install.userDir", dir); | ||
config.setProperty("node.install.nodeDir", dir); | ||
config.setProperty("node.install.runDir", dir); | ||
config.setProperty("node.install.storeDir", dir + "/pathstore"); | ||
config.setProperty("node.install.tempDir", dir + "/temp"); | ||
config.setProperty("node.install.pluginDir", dir + "/plugins"); | ||
config.setProperty("node.install.pluginStoresDir", dir + "/plugin-path"); | ||
config.setProperty("node.install.persistentTempDir", dir + "/persistent-temp"); | ||
|
||
config.setProperty("node.masterKeyFile", dir + "/master.keys"); | ||
config.setProperty("node.downloadsDir", dir + "/downloads"); | ||
|
||
config.setProperty("ssl.sslKeyStorePass", RandomStringUtils.randomAscii(64)); | ||
config.setProperty("ssl.sslKeyPass", RandomStringUtils.randomAscii(64)); | ||
|
||
config.setProperty("logger.dirname", dir + "/logs"); | ||
|
||
return config; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.freenetproject.mobile; | ||
|
||
import net.pterodactylus.fcp.*; | ||
import trikita.log.*; | ||
|
||
import java.io.*; | ||
import java.net.*; | ||
|
||
class Connector { | ||
private static final String DEFAULT_HOST = "127.0.0.1"; | ||
private static final int DEFAULT_PORT = 9481; | ||
private final FcpConnection fcpConnection; | ||
|
||
public Connector(FcpConnection fcpConnection) { | ||
this.fcpConnection = fcpConnection; | ||
} | ||
|
||
public Connector(String host, int port) throws UnknownHostException { | ||
this(new FcpConnection(host, port)); | ||
} | ||
|
||
public Connector() throws UnknownHostException { | ||
this(DEFAULT_HOST, DEFAULT_PORT); | ||
} | ||
|
||
public void connect() throws IOException { | ||
try { | ||
fcpConnection.connect(); | ||
fcpConnection.sendMessage(new ClientHello("freenet-mobile")); | ||
} catch (Exception e) { | ||
Log.i("Freenet", "Failed to connect through FCP. Node shutdown or wrong port: " + e.getMessage()); | ||
throw e; | ||
} | ||
} | ||
|
||
public void modifyConfiguration(String key, String value) throws IOException { | ||
ModifyConfig modifyConfig = new ModifyConfig("identifier"); | ||
modifyConfig.setOption(key, value); | ||
fcpConnection.sendMessage(modifyConfig); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.