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"
21 #include "glibmainloop.h"
25 #include <json-glib/json-glib.h>
30 /********************************************
31 * Example JSON config:
33 * sources: [ path1, path2, path3 ]
34 * sinks: [ path1, path2, path3 ]
37 **********************************************/
39 PluginLoader::PluginLoader(string configFile, AbstractRoutingEngine* re, int argc, char** argv): f_create(NULL), routingEngine(re), mMainLoop(nullptr)
41 DebugOut()<<"Loading config file: "<<configFile<<endl;
43 JsonParser* parser = json_parser_new();
44 GError* error = nullptr;
45 if(!json_parser_load_from_file(parser, configFile.c_str(), &error))
47 DebugOut()<<"Failed to load config: "<<error->message;
48 throw std::runtime_error("Failed to load config");
51 JsonNode* node = json_parser_get_root(parser);
54 throw std::runtime_error("Unable to get JSON root object");
56 JsonReader* reader = json_reader_new(node);
59 throw std::runtime_error("Unable to create JSON reader");
61 DebugOut()<<"Config members: "<<json_reader_count_members(reader)<<endl;
63 if(json_reader_read_member(reader,"mainloop"))
65 /// there is a mainloop entry. Load the plugin:
67 string mainloopstr = json_reader_get_string_value(reader);
69 mMainLoop = loadMainLoop(mainloopstr,argc, argv);
73 DebugOut(0)<<"Failed to load main loop plugin."<<endl;
78 /// there is no mainloop entry, use default glib
79 DebugOut()<<"No mainloop specified in config. Using glib by default."<<endl;
80 mMainLoop = new GlibMainLoop(argc,argv);
83 json_reader_end_member(reader);
85 if(!json_reader_read_member(reader,"sources"))
88 const GError * srcReadError = json_reader_get_error(reader);
90 DebugOut()<<"Error getting sources member: "<<srcReadError->message<<endl;
91 throw std::runtime_error("Error getting sources member");
94 g_assert(json_reader_is_array(reader));
97 for(int i=0; i < json_reader_count_elements(reader); i++)
99 json_reader_read_element(reader,i);
101 gchar** srcMembers = json_reader_list_members(reader);
103 std::map<std::string, std::string> configurationMap;
105 for(int i=0; i< json_reader_count_members(reader); i++)
107 json_reader_read_member(reader,srcMembers[i]);
108 configurationMap[srcMembers[i]] = json_reader_get_string_value(reader);
109 DebugOut()<<"plugin config key: "<<srcMembers[i]<<" value: "<<configurationMap[srcMembers[i]]<<endl;
110 json_reader_end_member(reader);
113 json_reader_read_member(reader, "path");
114 string path = json_reader_get_string_value(reader);
115 json_reader_end_member(reader);
117 AbstractSource* plugin = loadPlugin<AbstractSource*>(path,configurationMap);
119 if(plugin != nullptr)
121 mSources.push_back(plugin);
124 json_reader_end_element(reader);
127 json_reader_end_member(reader);
131 json_reader_read_member(reader,"sinks");
133 for(int i=0; i < json_reader_count_elements(reader); i++)
135 json_reader_read_element(reader,i);
137 gchar** srcMembers = json_reader_list_members(reader);
139 std::map<std::string, std::string> configurationMap;
141 for(int i=0; i< json_reader_count_members(reader); i++)
143 json_reader_read_member(reader,srcMembers[i]);
144 configurationMap[srcMembers[i]] = json_reader_get_string_value(reader);
145 json_reader_end_member(reader);
148 json_reader_read_member(reader, "path");
149 string path = json_reader_get_string_value(reader);
150 json_reader_end_member(reader);
152 AbstractSinkManager* plugin = loadPlugin<AbstractSinkManager*>(path, configurationMap);
154 if(plugin == nullptr)
156 throw std::runtime_error("plugin is not a SinkManager");
159 json_reader_end_element(reader);
162 json_reader_end_member(reader);
164 ///TODO: this will probably explode:
166 if(error) g_error_free(error);
168 g_object_unref(reader);
169 g_object_unref(parser);
173 PluginLoader::~PluginLoader()
175 for(auto itr = mSinks.begin(); itr != mSinks.end(); itr++)
180 for(auto itr = mSources.begin(); itr != mSources.end(); itr++)
186 SinkList PluginLoader::sinks()
191 IMainLoop *PluginLoader::mainloop()
196 SourceList PluginLoader::sources()
203 std::string PluginLoader::errorString()