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 <boost/concept_check.hpp>
26 //#include <json-glib/json-glib.h>
31 /********************************************
32 * Example JSON config:
34 * sources: [ path1, path2, path3 ]
35 * sinks: [ path1, path2, path3 ]
38 **********************************************/
40 std::string get_file_contents(const char *filename)
42 //FILE *in = fopen(filename,"r");
44 std::ifstream in(filename, std::ios::in);
54 PluginLoader::PluginLoader(string configFile, AbstractRoutingEngine* re, int argc, char** argv): f_create(NULL), routingEngine(re), mMainLoop(nullptr)
57 DebugOut()<<"Loading config file: "<<configFile<<endl;
58 json_object *rootobject;
59 json_tokener *tokener = json_tokener_new();
60 std::string configBuffer = get_file_contents(configFile.c_str());
61 if(configBuffer == "")
63 throw std::runtime_error("No config or config empty");
65 enum json_tokener_error err;
68 rootobject = json_tokener_parse_ex(tokener, configBuffer.c_str(),configBuffer.length());
69 } while ((err = json_tokener_get_error(tokener)) == json_tokener_continue);
70 if (err != json_tokener_success)
72 fprintf(stderr, "Error: %s\n", json_tokener_error_desc(err));
73 // Handle errors, as appropriate for your application.
75 if (tokener->char_offset < configFile.length()) // XXX shouldn't access internal fields
77 // Handle extra characters after parsed object as desired.
78 // e.g. issue an error, parse another object from that point, etc...
81 //DebugOut()<<"Config members: "<<json_reader_count_members(reader)<<endl;
82 json_object *mainloopobject = json_object_object_get(rootobject,"mainloop");
85 /// there is a mainloop entry. Load the plugin:
87 string mainloopstr = string(json_object_get_string(mainloopobject));
89 mMainLoop = loadMainLoop(mainloopstr,argc, argv);
93 DebugOut(0)<<"Failed to load main loop plugin."<<endl;
98 /// there is no mainloop entry, use default glib
99 DebugOut()<<"No mainloop specified in config. Using glib by default."<<endl;
100 mMainLoop = new GlibMainLoop(argc,argv);
103 json_object *sourcesobject = json_object_object_get(rootobject,"sources");
107 DebugOut()<<"Error getting sources member: "<<endl;
108 throw std::runtime_error("Error getting sources member");
111 //g_assert(json_reader_is_array(reader));
112 g_assert(json_object_get_type(sourcesobject)==json_type_array);
115 array_list *sourceslist = json_object_get_array(sourcesobject);
118 DebugOut() << "Error getting source list" << endl;
119 throw std::runtime_error("Error getting sources list");
122 for(int i=0; i < array_list_length(sourceslist); i++)
124 json_object *obj = (json_object*)array_list_get_idx(sourceslist,i); //This is an object
126 std::map<std::string, std::string> configurationMap;
127 json_object_object_foreach(obj, key, val)
129 string valstr = json_object_get_string(val);
130 DebugOut() << "plugin config key: " << key << "value:" << valstr << endl;
131 configurationMap[key] = valstr;
134 string path = configurationMap["path"];
136 AbstractSource* plugin = loadPlugin<AbstractSource*>(path,configurationMap);
138 if(plugin != nullptr)
140 mSources.push_back(plugin);
143 DebugOut() << "Trying to free list" << endl;
144 //array_list_free(sourceslist);
145 DebugOut() << "Trying to free obj" << endl;
146 json_object_put(sourcesobject);
147 DebugOut() << "Done first" << endl;
150 json_object *sinksobject = json_object_object_get(rootobject,"sinks");
154 DebugOut() << "Error getting sink object" << endl;
155 throw std::runtime_error("Error getting sink object");
160 array_list *sinkslist = json_object_get_array(sinksobject);
165 DebugOut() << "Error getting sink list" << endl;
166 throw std::runtime_error("Error getting sink list");
170 for(int i=0; i < array_list_length(sinkslist); i++)
172 json_object *obj = (json_object*)array_list_get_idx(sinkslist,i);
174 std::map<std::string, std::string> configurationMap;
176 json_object_object_foreach(obj, key, val)
178 string valstr = json_object_get_string(val);
179 DebugOut() << "plugin config key: " << key << "value:" << valstr << endl;
180 configurationMap[key] = valstr;
184 string path = configurationMap["path"];
186 AbstractSinkManager* plugin = loadPlugin<AbstractSinkManager*>(path, configurationMap);
188 if(plugin == nullptr)
190 throw std::runtime_error("plugin is not a SinkManager");
194 DebugOut() << "Trying to free obj" << endl;
195 json_object_put(sinksobject);
196 DebugOut() << "Done" << endl;
199 PluginLoader::~PluginLoader()
203 IMainLoop *PluginLoader::mainloop()
208 SourceList PluginLoader::sources()
215 std::string PluginLoader::errorString()