Commit 3dd82ad7 authored by Brendan Long's avatar Brendan Long

Watch the config file with IONotify.

parent 271a5182
internal class RUI.ConfigFileReader {
public class RUI.ConfigFileReader {
public string root_device_xml { get; private set; }
public string service_directory { get; private set; }
public RemoteUI[] remoteUIs { get; private set; }
public void parse_config_file(string config_file) throws Error {
private string config_file;
private FileMonitor? file_monitor = null;
public signal void remote_uis_changed();
public ConfigFileReader(string config_file) {
this.config_file = config_file;
}
private bool check_required_members(string type, Json.Object obj,
string[] required_members) {
var missing_required = false;
foreach (string member in required_members) {
if (!obj.has_member(member)) {
stderr.printf("Ignoring %s with missing required attribute \"%s\".\n",
type, member);
missing_required = true;
}
}
return !missing_required;
}
public void parse_config_file() throws Error {
var parser = new Json.Parser();
parser.load_from_file(config_file);
......@@ -12,14 +34,12 @@ internal class RUI.ConfigFileReader {
if (object == null) {
throw new RUIError.BAD_CONFIG("Config file has no root object.\n");
}
root_device_xml = object.get_string_member("root-device-xml");
if (root_device_xml == null) {
throw new RUIError.BAD_CONFIG("Missing \"root-device-xml\"");
string[] required_members = {"root-device-xml", "service-directory"};
if (!check_required_members("config file", object, required_members)) {
throw new RUIError.BAD_CONFIG("Missing \"root-device-xml\" or \"service-directory\"");
}
root_device_xml = object.get_string_member("root-device-xml");
service_directory = object.get_string_member("service-directory");
if (service_directory == null) {
throw new RUIError.BAD_CONFIG("Missing \"service-directory\"");
}
if (!Path.is_absolute(service_directory)) {
service_directory = Path.build_filename(Path.get_dirname(config_file), service_directory);
}
......@@ -31,7 +51,7 @@ internal class RUI.ConfigFileReader {
stderr.printf("Ignoring non-object member of uis array.\n");
continue;
}
string[] required_members = {"id", "name", "protocols"};
required_members = {"id", "name", "protocols"};
if (!check_required_members("UI", ui_node, required_members)) {
continue;
}
......@@ -113,5 +133,31 @@ internal class RUI.ConfigFileReader {
remoteUIs += remoteUI;
}
this.remoteUIs = remoteUIs;
remote_uis_changed();
}
public void watch_config_file() throws Error {
if (file_monitor != null) {
file_monitor.cancel();
}
var file = File.new_for_path(config_file);
stdout.printf("Watching %s\n", file.get_path());
file_monitor = file.monitor(FileMonitorFlags.NONE);
file_monitor.changed.connect(on_config_file_changed);
parse_config_file();
}
private void on_config_file_changed(File file, File? other_file,
FileMonitorEvent event) {
if (event != FileMonitorEvent.CHANGED && event != FileMonitorEvent.CREATED) {
return;
}
try {
parse_config_file();
} catch (Error e) {
stderr.printf(
"Error parsing changed config file: %s. Continuing to use the old config file.\n",
e.message);
}
}
}
......@@ -35,19 +35,6 @@ static const OptionEntry[] options = {
{ null }
};
internal static bool check_required_members(string type, Json.Object obj,
string[] required_members) {
var missing_required = false;
foreach (string member in required_members) {
if (!obj.has_member(member)) {
stderr.printf("Ignoring %s with missing required attribute \"%s\".\n",
type, member);
missing_required = true;
}
}
return !missing_required;
}
internal static int main(string[] args) {
try {
var opt_context = new OptionContext("UPnP RemoteUIServer");
......@@ -65,15 +52,15 @@ internal static int main(string[] args) {
}
RUI.ConfigFileReader config;
try {
config = new RUI.ConfigFileReader();
config.parse_config_file(config_file);
config = new RUI.ConfigFileReader(config_file);
config.watch_config_file();
} catch (Error e) {
stderr.printf("Error reading config file %s.\n", config_file);
stderr.printf("Error reading config file %s: %s\n", config_file,
e.message);
return 3;
}
try {
RUI.RemoteUIServer server = new RUI.RemoteUIServer(
config.root_device_xml, config.service_directory,config.remoteUIs);
RUI.RemoteUIServer server = new RUI.RemoteUIServer(config);
server.start();
return 0;
} catch (Error e) {
......
......@@ -37,11 +37,15 @@ public class RUI.RemoteUIServer {
GUPnP.RootDevice root_device;
RemoteUI[] remoteUIs;
public RemoteUIServer(string root_device_xml, string service_directory,
RemoteUI[] remoteUIs) {
this.root_device_xml = root_device_xml;
this.service_directory = service_directory;
this.remoteUIs = remoteUIs;
public RemoteUIServer(ConfigFileReader config) {
this.root_device_xml = config.root_device_xml;
this.service_directory = config.service_directory;
this.remoteUIs = config.remoteUIs;
config.remote_uis_changed.connect(on_remote_uis_changed);
}
private void on_remote_uis_changed(ConfigFileReader config) {
this.remoteUIs = config.remoteUIs;
}
public void start() throws Error {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment