summaryrefslogtreecommitdiff
path: root/ambd/pluginloader.cpp
blob: 6727413f60efa31bca42b7ab1b6761dde1541e26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
Copyright (C) 2012 Intel Corporation

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/


#include "pluginloader.h"
#include "glibmainloop.h"
#include "core.h"

#include <gio/gio.h>
#include <picojson.h>

#include <iostream>
#include <fstream>
#include <stdexcept>
#include <boost/concept_check.hpp>

std::string get_file_contents(const char *filename)
{
	std::ifstream in(filename, std::ios::in);
	if(in.fail())
	{
		DebugOut(DebugOut::Error) << "Failed to open file '" << filename << "':' " << strerror(errno) << endl;
		return "";
	}

	std::string output;
	std::string line;
	while(in.good())
	{
		getline(in,line);
		output.append(line);
	}
	return output;
}

PluginLoader::PluginLoader(string configFile, int argc, char** argv): f_create(NULL), routingEngine(nullptr), mMainLoop(nullptr)
{
	DebugOut()<<"Loading config file: "<<configFile<<endl;
	std::string configBuffer = get_file_contents(configFile.c_str());

	std::string picojsonerr = "";
	picojson::value v;
	picojson::parse(v, configBuffer.begin(), configBuffer.end(), &picojsonerr);

	if(!picojsonerr.empty())
	{
		DebugOut(DebugOut::Error) << "Failed to parse main config! " << picojsonerr << endl;
		throw std::runtime_error("Error parsing config");
	}

	if(v.contains("routingEngine"))
	{
		string restr = v.get("routingEngine").to_str();

		routingEngine = loadRoutingEngine(restr);

		if(!routingEngine)
		{
			DebugOut(DebugOut::Warning)<<"Failed to load routing engine plugin: "<<restr<<endl;
		}
	}

	if(!routingEngine)
	{
		/// there is no mainloop entry, use default glib
		DebugOut()<<"No routing engine specified in config.  Using built-in 'core' routing engine by default."<<endl;

		/// core wants some specific configuration settings:
		std::map<std::string,std::string> settings;


		for (auto q : {"lowPriorityQueueSize", "normalPriorityQueueSize", "highPriorityQueueSize"})
		{
			if (v.contains(q))
			{
				string restr = v.get(q).to_str();
				settings[q] = restr;
			}
		}

		routingEngine = new Core(settings);
	}


	if(v.contains("plugins"))
	{
		std::string pluginsPath = v.get("plugins").to_str();
		scanPluginDir(pluginsPath);
	}

	if(v.contains("mainloop"))
	{
		/// there is a mainloop entry.  Load the plugin:

		string mainloopstr = v.get("mainloop").to_str();

		mMainLoop = loadMainLoop(mainloopstr, argc, argv);

		if(!mMainLoop)
		{
			DebugOut(DebugOut::Warning)<<"Failed to load main loop plugin."<<endl;
		}
	}
	else if(!mMainLoop)
	{
		/// there is no mainloop entry, use default glib
		DebugOut()<<"No mainloop specified in config.  Using glib by default."<<endl;
		mMainLoop = new GlibMainLoop(argc, argv);
	}


	for (auto q : {"sources", "sinks"})
	{
		if(v.contains("sources"))
		{
			picojson::array list = v.get(q).get<picojson::array>();
			if (!list.size())
			{
				DebugOut() << "Error getting list for " << q << endl;
			}

			for(auto src : list)
			{
				std::map<std::string, std::string> configurationMap;
				for( auto obj : src.get<picojson::object>())
				{
					string valstr = obj.second.to_str();
					string key = obj.first;

					DebugOut() << "plugin config key: " << key << "value:" << valstr << endl;

					configurationMap[key] = valstr;
				}

				string path = configurationMap["path"];

				if(!loadPlugin(path, configurationMap))
					DebugOut(DebugOut::Warning) << "Failed to load plugin: " << path <<endl;
			}

		}
	}

	Core* core = static_cast<Core*>(routingEngine);
	if( core != nullptr )
	{
		core->inspectSupported();
	}
}

PluginLoader::~PluginLoader()
{
	for(auto handle : openHandles)
		dlclose(handle);
}

IMainLoop *PluginLoader::mainloop()
{
	return mMainLoop;
}

std::string PluginLoader::errorString()
{
	return mErrorString;
}

void PluginLoader::scanPluginDir(const std::string & dir)
{
	DebugOut() << "Scanning plugin directory: " << dir << endl;

	auto pluginsDirectory = amb::make_gobject(g_file_new_for_path(dir.c_str()));

	GError* enumerateError = nullptr;

	auto enumerator = amb::make_gobject(g_file_enumerate_children(pluginsDirectory.get(), G_FILE_ATTRIBUTE_ID_FILE,
																  G_FILE_QUERY_INFO_NONE, nullptr,
																  &enumerateError));
	auto enumerateErrorPtr = amb::make_super(enumerateError);

	if(enumerateErrorPtr)
	{
		DebugOut(DebugOut::Error) << "Scanning plugin directory: " << enumerateErrorPtr->message << endl;
		return;
	}

	GError* errorGetFile = nullptr;
	while(auto pluginConfig = amb::make_gobject(g_file_enumerator_next_file(enumerator.get(), nullptr, &errorGetFile)))
	{
		std::string name = g_file_info_get_name(pluginConfig.get());

		DebugOut() << "Found file: " << name << endl;
		std::string fullpath = dir + (boost::algorithm::ends_with(dir, "/") ? "":"/") + name;
		std::string data = get_file_contents(fullpath.c_str());

		DebugOut() << "data: " << data << endl;

		if(!readPluginConfig(data))
		{
			DebugOut(DebugOut::Error) << "Reading contentds of file: " << name << endl;
		}
	}

	auto errorGetFilePtr = amb::make_super(errorGetFile);

	if(errorGetFilePtr)
	{
		DebugOut(DebugOut::Error) << "enumerating file: " << errorGetFilePtr->message << endl;
		return;
	}
}

bool PluginLoader::readPluginConfig(const string &configData)
{
	picojson::value v;
	std::string err;

	picojson::parse(v, configData.begin(), configData.end(), &err);

	if (!err.empty())
	{
		DebugOut(DebugOut::Error) << err << endl;
		return false;
	}

	std::string pluginName;
	if(v.contains("name"))
	{
		pluginName = v.get("name").to_str();
	}

	std::string pluginPath;
	if(v.contains("path"))
	{
		pluginPath = v.get("path").to_str();
	}
	else
	{
		DebugOut(DebugOut::Error) << "config missing 'path'." << endl;
		return false;
	}

	bool enabled = false;
	if(v.contains("enabled"))
	{
		enabled = v.get("enabled").get<bool>();
	}
	else
	{
		DebugOut(DebugOut::Error) << "config missing 'enabled'." << endl;
		return false;
	}

	DebugOut() << "Plugin: " << pluginName << endl;
	DebugOut() << "Path: " << pluginPath << endl;
	DebugOut() << "Enabled: " << enabled << endl;

	if(enabled)
	{
		std::map<std::string, std::string> otherConfig;

		picojson::object obj = v.get<picojson::object>();
		for(auto itr : obj)
		{
			otherConfig[itr.first] = itr.second.to_str();
		}

		loadPlugin(pluginPath, otherConfig);
	}

	return true;
}