2 Copyright (C) 2012 Intel Corporation
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "pluginloader.h"
23 #include <json-glib/json-glib.h>
27 /********************************************
28 * Example JSON config:
30 * sources: [ path1, path2, path3 ]
31 * sinks: [ path1, path2, path3 ]
34 **********************************************/
36 PluginLoader::PluginLoader(string configFile): f_create(NULL)
38 DebugOut()<<"Loading config file: "<<configFile<<endl;
40 JsonParser* parser = json_parser_new();
41 GError* error = nullptr;
42 if(!json_parser_load_from_file(parser, configFile.c_str(), &error))
44 DebugOut()<<"Failed to load config: "<<error->message;
45 throw std::runtime_error("Failed to load config");
48 JsonReader* reader = json_reader_new(json_parser_get_root(parser));
51 throw std::runtime_error("Unable to create JSON reader");
53 json_reader_read_member(reader,"sources");
55 JsonNode* sourcesNode = json_reader_get_value(reader);
57 if(sourcesNode != nullptr)
60 JsonArray* sourcesArray = json_node_get_array(sourcesNode);
62 int sourcesLength = json_array_get_length(sourcesArray);
64 for(int i=0; i < sourcesLength; i++)
66 string path = json_array_get_string_element(sourcesArray,i);
67 AbstractSource* plugin = loadPlugin<AbstractSource*>(path);
70 mSources.push_back(plugin);
73 json_reader_end_member(reader);
74 g_object_unref(sourcesArray);
79 DebugOut()<<"Config contains no sources."<<endl;
84 json_reader_read_member(reader,"sinks");
86 JsonNode* sinksNode = json_reader_get_value(reader);
88 if(sinksNode != nullptr)
91 JsonArray* sinksArray = json_node_get_array(sinksNode);
93 int sinksLength = json_array_get_length(sinksArray);
95 for(int i=0; i < sinksLength; i++)
97 string path = json_array_get_string_element(sinksArray,i);
98 AbstractSink* plugin = loadPlugin<AbstractSink*>(path);
100 if(plugin != nullptr)
101 mSinks.push_back(plugin);
104 json_reader_end_member(reader);
105 g_object_unref(sinksArray);
108 ///TODO: this will probably explode:
111 g_object_unref(reader);
112 g_object_unref(parser);
113 g_object_unref(sourcesNode);
114 g_object_unref(sinksNode);
118 void PluginLoader::setSinkCreatedCb(SinkSignal cb)
123 void PluginLoader::setSinkRemovedCb(SinkSignal cb)
128 SinkList PluginLoader::sinks()
133 SourceList PluginLoader::sources()
140 std::string PluginLoader::errorString()