summaryrefslogtreecommitdiff
path: root/ambd/pluginloader.cpp
blob: 6d81e1c1e766176bcb4b9d6e9958b06228b2f1cf (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
/*
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 <json.h>
#include <iostream>
#include <stdexcept>
#include <boost/concept_check.hpp>
//#include <json-glib/json-glib.h>


using namespace std;

/********************************************
 * Example JSON config:
 * {
 * 	sources: [ path1, path2, path3 ]
 * 	sinks: [ path1, path2, path3 ]
 * }
 * 
**********************************************/

std::string get_file_contents(const char *filename)
{
  //FILE *in = fopen(filename,"r");
  
  std::ifstream in(filename, std::ios::in);
  std::string output;
  std::string line;
  while(in.good())
  {
    getline(in,line);
    output.append(line);
  }
  return output;
}
PluginLoader::PluginLoader(string configFile, AbstractRoutingEngine* re, int argc, char** argv): f_create(NULL), routingEngine(re), mMainLoop(nullptr)
{
  
	DebugOut()<<"Loading config file: "<<configFile<<endl;
	json_object *rootobject;
	json_tokener *tokener = json_tokener_new();
	std::string configBuffer = get_file_contents(configFile.c_str());
	if(configBuffer == "")
	{
		throw std::runtime_error("No config or config empty");
	}
	enum json_tokener_error err;
	do
	{
		rootobject = json_tokener_parse_ex(tokener, configBuffer.c_str(),configBuffer.length());
	} while ((err = json_tokener_get_error(tokener)) == json_tokener_continue);
	if (err != json_tokener_success)
	{
		fprintf(stderr, "Error: %s\n", json_tokener_error_desc(err));
		// Handle errors, as appropriate for your application.
	}
	if (tokener->char_offset < configFile.length()) // XXX shouldn't access internal fields
	{
		// Handle extra characters after parsed object as desired.
		// e.g. issue an error, parse another object from that point, etc...
	}
	
	//DebugOut()<<"Config members: "<<json_reader_count_members(reader)<<endl;
	json_object *mainloopobject = json_object_object_get(rootobject,"mainloop");
	if (mainloopobject)
	{
		/// there is a mainloop entry.  Load the plugin:

		string mainloopstr = string(json_object_get_string(mainloopobject));

		mMainLoop = loadMainLoop(mainloopstr,argc, argv);

		if(!mMainLoop)
		{
			DebugOut(0)<<"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);
	}
	
	json_object *sourcesobject = json_object_object_get(rootobject,"sources");

	if(!sourcesobject)
	{
		DebugOut()<<"Error getting sources member: "<<endl;
		throw std::runtime_error("Error getting sources member");
	}
	
	//g_assert(json_reader_is_array(reader));
	g_assert(json_object_get_type(sourcesobject)==json_type_array);
	
	
	array_list *sourceslist = json_object_get_array(sourcesobject);
	if (!sourceslist)
	{
	  DebugOut() << "Error getting source list" << endl;
	  throw std::runtime_error("Error getting sources list");
	}
	
	for(int i=0; i < array_list_length(sourceslist); i++)
	{
		json_object *obj = (json_object*)array_list_get_idx(sourceslist,i); //This is an object
		
		std::map<std::string, std::string> configurationMap;
		json_object_object_foreach(obj, key, val)
		{
			string valstr = json_object_get_string(val);
			DebugOut() << "plugin config key: " << key << "value:" << valstr << endl;
			configurationMap[key] = valstr;
		}
		json_object *pathobject = json_object_object_get(obj,"path");
 		string path = string(json_object_get_string(pathobject));

		AbstractSource* plugin = loadPlugin<AbstractSource*>(path,configurationMap);
		
		if(plugin != nullptr)
		{
			mSources.push_back(plugin);
		}
		json_object_put(pathobject);
	}
	DebugOut() << "Trying to free list" << endl;
	array_list_free(sourceslist);
	DebugOut() << "Trying to free obj" << endl;
	//json_object_put(sourcesobject);
	DebugOut() << "Done first" << endl;
	///read the sinks:
	
	json_object *sinksobject = json_object_object_get(rootobject,"sinks");
	
	if (!sinksobject)
	{
	  DebugOut() << "Error getting sink object" << endl;
	  throw std::runtime_error("Error getting sink object");
	}
	
	
	
	array_list *sinkslist = json_object_get_array(sinksobject);
	
	
	if (!sinkslist)
	{
	  DebugOut() << "Error getting sink list" << endl;
	  throw std::runtime_error("Error getting sink list");
	}
	
	
	for(int i=0; i < array_list_length(sinkslist); i++)
	{
		json_object *obj = (json_object*)array_list_get_idx(sinkslist,i);

		std::map<std::string, std::string> configurationMap;

		json_object_object_foreach(obj, key, val)
		{
			string valstr = json_object_get_string(val);
			DebugOut() << "plugin config key: " << key << "value:" << valstr << endl;
			configurationMap[key] = valstr;
		}

		
		string path = configurationMap["path"];

		AbstractSinkManager* plugin = loadPlugin<AbstractSinkManager*>(path, configurationMap);

		if(plugin == nullptr)
		{
			throw std::runtime_error("plugin is not a SinkManager");
		}
	}

	DebugOut() << "Trying to free obj" << endl;
	json_object_put(sinksobject);
	DebugOut() << "Done" << endl;
		
	
	///TODO: this will probably explode:
	
	//if(error) g_error_free(error);
	
	//g_object_unref(reader);
	//g_object_unref(parser);
	//*/
}

PluginLoader::~PluginLoader()
{
}

SinkList PluginLoader::sinks()
{
	return mSinks;
}

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

SourceList PluginLoader::sources()
{
	return mSources;
}



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