Skip to content

Commit

Permalink
Use JSON for config file
Browse files Browse the repository at this point in the history
  • Loading branch information
lavajuno committed Jan 15, 2024
1 parent 5fed6dd commit e574d50
Show file tree
Hide file tree
Showing 22 changed files with 868 additions and 749 deletions.
38 changes: 38 additions & 0 deletions config/config-readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--- Instructions for configuring MirrorLog ---
MirrorLog's configuration is stored in "config/mirrorlog.conf.json".
Available configuration options are listed below:

"server":
"threads" (int):
- How many connections should the server be able to simultaneously handle?
- Having a lot of threads will slightly increase resource usage, but
having too few threads will cause connections to be rejected.

"port" (int):
- Which port should the server listen on?

"timeout" (int):
- How long should the server wait before disconnecting inactive clients? (integer)
- This duration is measured in milliseconds. (15 min is 900000 ms)
- Note that if a client disconnects or a socket error occurs, the connection
will be automatically terminated, so this only handles inactive clients.

"restricted" (boolean):
- Should the server ignore requests from unknown addresses?

"allowed_addresses" (list of strings):
- If 'restricted' is true, which addresses should we allow connections from?

"output":
"component_pad" (int):
- What length should component names be padded up to?
- This makes the log more readable, provided most component names are under this length.

"log_to_file" (boolean):
- Should the server log to files as well as the console?

"file_duration" (int):
- How often (in hours) should the server create a new log file?

"file_history" (int):
- How many old log files should the server retain?
18 changes: 18 additions & 0 deletions config/mirrorlog.conf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"revision": 0,
"server": {
"threads": 32,
"port": 4001,
"timeout": 1800000,
"restricted": false,
"allowed_addresses": [
"127.0.0.1"
]
},
"output": {
"component_pad": 24,
"log_to_file": true,
"file_duration": 24,
"file_history": 10
}
}
86 changes: 0 additions & 86 deletions config/mirrorlog.conf.yml

This file was deleted.

75 changes: 75 additions & 0 deletions src/main/java/org/lavajuno/lucidjson/Json.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* LucidJSON v0.0.1 - Experimental
*/

package org.lavajuno.lucidjson;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.List;
import java.util.Scanner;

/**
* Provides functionality for serializing/deserializing JSON to/from Strings and files.
*/
@SuppressWarnings("unused")
public class Json {
/**
* Deserializes JSON object from a String.
* @param text Input string
* @return Deserialized JSON object
* @throws ParseException If parsing fails
*/
public static JsonObject read(String text) throws ParseException {
return new JsonObject(text.replace("\n", ""));
}

/**
* Deserializes a JSON object from a list of lines (Strings).
* @param lines Input lines
* @return Deserialized JSON object
* @throws ParseException If parsing fails
*/
public static JsonObject read(List<String> lines) throws ParseException {
StringBuilder sb = new StringBuilder();
for(String i : lines) { sb.append(i); }
return read(sb.toString());
}

/**
* Deserializes a JSON object from a file.
* @param file_path Path to the input file
* @return Deserialized JSON object
* @throws IOException If reading the file fails
* @throws ParseException If parsing fails
*/
public static JsonObject readFile(String file_path) throws IOException, ParseException {
Scanner file = new Scanner(new FileInputStream(file_path));
StringBuilder lines = new StringBuilder();
while(file.hasNextLine()) { lines.append(file.nextLine()); }
file.close();
return read(lines.toString());
}

/**
* Serializes a JSON object to a String.
* @param e Input JSON object
* @return String containing the serialized JSON object
*/
public static String write(JsonObject e) { return e.toString(0); }

/**
* Serializes a JSON object to a file.
* @param e Input JSON object
* @param file_path Path to the target file
* @throws IOException If writing to the file fails
*/
public static void writeFile(JsonObject e, String file_path) throws IOException {
PrintWriter file = new PrintWriter(new FileOutputStream(file_path));
file.print(e.toString(0));
file.close();
}
}
125 changes: 125 additions & 0 deletions src/main/java/org/lavajuno/lucidjson/JsonArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package org.lavajuno.lucidjson;

import java.text.ParseException;
import java.util.Vector;

/**
* Represents a JSON array.
* Provides functionality for accessing and modifying its values.
*/
@SuppressWarnings("unused")
public class JsonArray extends JsonEntity {
private final Vector<JsonEntity> values;

/**
* Constructs an empty JsonArray.
*/
public JsonArray() { values = new Vector<>(); }

/**
* Constructs a JsonArray from the given vector of elements.
* @param values Values to initialize array with
*/
public JsonArray(Vector<JsonEntity> values) { this.values = values; }

/**
* Constructs a JsonArray by parsing the input.
* @param text JSON to parse
* @throws ParseException If an error is encountered while parsing the input
*/
protected JsonArray(String text) throws ParseException {
values = parseValues(text.strip());
}

/**
* @param text JSON to parse
* @return Vector created from the input
* @throws ParseException If an error is encountered while parsing the input
*/
private static Vector<JsonEntity> parseValues(String text) throws ParseException {
Vector<JsonEntity> values = new Vector<>();
Vector<String> raw_values = splitValues(text);
for(String i : raw_values) {
if(!i.isEmpty()) { values.add(parseEntity(i.strip())); }
}
return values;
}

/**
* @param index Index of the target JsonEntity
* @return JsonEntity at the given index (null if it does not exist)
*/
public JsonEntity get(int index) {
try {
return values.get(index);
} catch(ArrayIndexOutOfBoundsException e) {
return null;
}
}

/**
* @param index Index of the target JsonEntity
* @param value New value for the target JsonEntity
*/
public void set(int index, JsonEntity value) { values.set(index, value); }

/**
* @param value JsonEntity to be added to this JsonArray
*/
public void add(JsonEntity value) { values.add(value); }

/**
* @param index Index of the JsonEntity to remove
*/
public void remove(int index) { values.remove(index); }

/**
* Clears this JsonArray
*/
public void clear() { values.clear(); }

/**
* @return The number of entities contained by this JsonArray
*/
public int size() { return values.size(); }

/**
* @return This JsonArray's elements
*/
public Vector<JsonEntity> getValues() { return values; }

/**
* Serializes this JsonArray to a String, with indentation and newlines.
* @param indent Indent of this JsonEntity (0)
* @return Returns this JsonEntity as a string.
*/
@Override
protected String toString(int indent) {
StringBuilder sb = new StringBuilder();
String pad_elem = " ".repeat(indent + 4);
String pad_close = " ".repeat(indent);
sb.append("[\n");
for(int i = 0; i < values.size() - 1; i++) {
sb.append(pad_elem).append(values.get(i).toString(indent + 4)).append(",\n");
}
if(!values.isEmpty()) {
sb.append(pad_elem).append(values.get(values.size() - 1)).append("\n");
}
sb.append(pad_close).append("]");
return sb.toString();
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
for(int i = 0; i < values.size() - 1; i++) {
sb.append(values.get(i)).append(",\n");
}
if(!values.isEmpty()) {
sb.append(values.get(values.size() - 1));
}
sb.append("]");
return sb.toString();
}
}
Loading

0 comments on commit e574d50

Please sign in to comment.