118c2e0f499286be842b42fce1588c18035c7448
[profile/ivi/automotive-message-broker.git] / ambd / pluginloader.cpp
1 /*
2 Copyright (C) 2012 Intel Corporation
3
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.
8
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.
13
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
17 */
18
19
20 #include "pluginloader.h"
21 #include <iostream>
22 #include <stdexcept>
23 #include <json-glib/json-glib.h>
24
25 using namespace std;
26
27 /********************************************
28  * Example JSON config:
29  * {
30  *      sources: [ path1, path2, path3 ]
31  *      sinks: [ path1, path2, path3 ]
32  * }
33  * 
34 **********************************************/
35
36 PluginLoader::PluginLoader(string configFile): f_create(NULL)
37 {
38         DebugOut()<<"Loading config file: "<<configFile<<endl;
39         
40         JsonParser* parser = json_parser_new();
41         GError* error = nullptr;
42         if(!json_parser_load_from_file(parser, configFile.c_str(), &error))
43         {
44                 DebugOut()<<"Failed to load config: "<<error->message;
45                 throw std::runtime_error("Failed to load config");
46         }
47         
48         JsonReader* reader = json_reader_new(json_parser_get_root(parser));
49         
50         if(reader == nullptr)
51                 throw std::runtime_error("Unable to create JSON reader");
52         
53         json_reader_read_member(reader,"sources");
54         
55         JsonNode* sourcesNode = json_reader_get_value(reader);
56         
57         if(sourcesNode != nullptr)
58         {
59
60                 JsonArray* sourcesArray = json_node_get_array(sourcesNode);
61
62                 int sourcesLength = json_array_get_length(sourcesArray);
63
64                 for(int i=0; i < sourcesLength; i++)
65                 {
66                         string path = json_array_get_string_element(sourcesArray,i);
67                         AbstractSource* plugin = loadPlugin<AbstractSource*>(path);
68                         
69                         if(plugin != nullptr)
70                                 mSources.push_back(plugin);
71                 }
72                         
73                 json_reader_end_member(reader);
74                 g_object_unref(sourcesArray);
75         }
76         
77         else 
78         {
79                 DebugOut()<<"Config contains no sources."<<endl;
80         }
81         
82         ///read the sinks:
83                 
84         json_reader_read_member(reader,"sinks");
85         
86         JsonNode* sinksNode = json_reader_get_value(reader);
87         
88         if(sinksNode != nullptr)
89         {
90         
91                 JsonArray* sinksArray = json_node_get_array(sinksNode);
92                 
93                 int sinksLength = json_array_get_length(sinksArray);
94                 
95                 for(int i=0; i < sinksLength; i++)
96                 {
97                         string path = json_array_get_string_element(sinksArray,i);
98                         AbstractSink* plugin = loadPlugin<AbstractSink*>(path);
99                         
100                         if(plugin != nullptr)
101                                 mSinks.push_back(plugin);
102                 }
103                 
104                 json_reader_end_member(reader);
105                 g_object_unref(sinksArray);
106         }
107         
108         ///TODO: this will probably explode:
109         
110         g_error_free(error);
111         g_object_unref(reader);
112         g_object_unref(parser);
113         g_object_unref(sourcesNode);
114         g_object_unref(sinksNode);
115         
116 }
117
118 void PluginLoader::setSinkCreatedCb(SinkSignal cb)
119 {
120         sinkCreatedCb = cb;
121 }
122
123 void PluginLoader::setSinkRemovedCb(SinkSignal cb)
124 {
125         sinkRemovedCb = cb;
126 }
127
128 SinkList PluginLoader::sinks()
129 {
130         return mSinks;
131 }
132
133 SourceList PluginLoader::sources()
134 {
135         return mSources;
136 }
137
138
139
140 std::string PluginLoader::errorString()
141 {
142         return mErrorString;
143 }
144