softwarecontainer  0.18.0-739e8d7 2017-05-04
softwarecontaineragent_dbus_stub.cpp
1 #include "softwarecontaineragent_dbus_stub.h"
2 #include "softwarecontainererror.h"
3 
4 Glib::ustring interfaceXml0 = R"XML_DELIMITER(
5 <?xml version="1.0" encoding="UTF-8" ?>
6 <node name="/com/pelagicore/SoftwareContainer">
7  <interface name="com.pelagicore.SoftwareContainerAgent">
8  <method name="List">
9  <arg direction="out" type="ai" name="containers" />
10  </method>
11 
12  <method name="Create">
13  <arg direction="in" type="s" name="config" />
14  <arg direction="out" type="i" name="containerID" />
15  </method>
16 
17  <method name="Execute">
18  <arg direction="in" type="i" name="containerID" />
19  <arg direction="in" type="s" name="commandLine" />
20  <arg direction="in" type="s" name="workingDirectory" />
21  <arg direction="in" type="s" name="outputFile" />
22  <arg direction="in" type="a{ss}" name="env" />
23  <arg direction="out" type="i" name="pid" />
24  </method>
25 
26  <method name="Suspend">
27  <arg direction="in" type="i" name="containerID" />
28  </method>
29 
30  <method name="Resume">
31  <arg direction="in" type="i" name="containerID" />
32  </method>
33 
34  <method name="Destroy">
35  <arg direction="in" type="i" name="containerID" />
36  </method>
37 
38  <method name="BindMount">
39  <arg direction="in" type="i" name="containerID" />
40  <arg direction="in" type="s" name="pathInHost" />
41  <arg direction="in" type="s" name="pathInContainer" />
42  <arg direction="in" type="b" name="readOnly" />
43  </method>
44 
45  <method name="ListCapabilities">
46  <arg direction="out" type="as" name="capabilities" />
47  </method>
48 
49  <method name="SetCapabilities">
50  <arg direction="in" type="i" name="containerID" />
51  <arg direction="in" type="as" name="capabilities" />
52  </method>
53 
54  <signal name="ProcessStateChanged">
55  <arg direction="out" type="i" name="containerID"/>
56  <arg direction="out" type="u" name="processID"/>
57  <arg direction="out" type="b" name="isRunning"/>
58  <arg direction="out" type="u" name="exitCode"/>
59  </signal>
60 
61  </interface>
62 </node>
63 )XML_DELIMITER";
64 
65 com::pelagicore::SoftwareContainerAgent::SoftwareContainerAgent() :
66  connectionId(0),
67  registeredId(0),
68  m_objectPath("/com/pelagicore/SoftwareContainerAgent"),
69  m_interfaceName("com.pelagicore.SoftwareContainerAgent")
70 {
71  ProcessStateChanged_signal.connect(
72  sigc::mem_fun(this, &SoftwareContainerAgent::ProcessStateChanged_emitter)
73  );
74 }
75 
76 void com::pelagicore::SoftwareContainerAgent::connect(
77  Gio::DBus::BusType busType,
78  std::string name)
79 {
80  try {
81  introspection_data = Gio::DBus::NodeInfo::create_for_xml(interfaceXml0);
82  } catch(const Glib::Error& ex) {
83  g_warning("Unable to create introspection data: %s \n", ex.what().c_str());
84  }
85 
86  connectionId = Gio::DBus::own_name(
87  busType,
88  name,
89  sigc::mem_fun(this, &SoftwareContainerAgent::on_bus_acquired),
90  sigc::mem_fun(this, &SoftwareContainerAgent::on_name_acquired),
91  sigc::mem_fun(this, &SoftwareContainerAgent::on_name_lost)
92  );
93 }
94 
95 void com::pelagicore::SoftwareContainerAgent::on_method_call(const Glib::RefPtr<Gio::DBus::Connection>& /* connection */,
96  const Glib::ustring& /* sender */,
97  const Glib::ustring& /* object_path */,
98  const Glib::ustring& /* interface_name */,
99  const Glib::ustring& method_name,
100  const Glib::VariantContainerBase& parameters,
101  const Glib::RefPtr<Gio::DBus::MethodInvocation>& invocation)
102 {
103  try {
104  if (method_name.compare("List") == 0) {
105  List(
107  }
108 
109  if (method_name.compare("Create") == 0) {
110  Glib::Variant<Glib::ustring > base_config;
111  parameters.get_child(base_config, 0);
112  Glib::ustring p_config;
113  p_config = base_config.get();
114 
115  Create(
116  Glib::ustring(p_config),
118  }
119 
120  if (method_name.compare("Execute") == 0) {
121  Glib::Variant<gint32 > base_containerID;
122  parameters.get_child(base_containerID, 0);
123  gint32 p_containerID;
124  p_containerID = base_containerID.get();
125 
126  Glib::Variant<Glib::ustring > base_commandLine;
127  parameters.get_child(base_commandLine, 1);
128  Glib::ustring p_commandLine;
129  p_commandLine = base_commandLine.get();
130 
131  Glib::Variant<Glib::ustring > base_workingDirectory;
132  parameters.get_child(base_workingDirectory, 2);
133  Glib::ustring p_workingDirectory;
134  p_workingDirectory = base_workingDirectory.get();
135 
136  Glib::Variant<Glib::ustring > base_outputFile;
137  parameters.get_child(base_outputFile, 3);
138  Glib::ustring p_outputFile;
139  p_outputFile = base_outputFile.get();
140 
141  Glib::Variant<std::map<Glib::ustring, Glib::ustring> > base_env;
142  parameters.get_child(base_env, 4);
143  std::map<Glib::ustring, Glib::ustring> p_env;
144  p_env = base_env.get();
145 
146  Execute(
147  (p_containerID),
148  Glib::ustring(p_commandLine),
149  Glib::ustring(p_workingDirectory),
150  Glib::ustring(p_outputFile),
151  SoftwareContainerAgentCommon::glibStringMapToStdStringMap(p_env),
153  }
154 
155  if (method_name.compare("Suspend") == 0) {
156  Glib::Variant<gint32 > base_containerID;
157  parameters.get_child(base_containerID, 0);
158  gint32 p_containerID;
159  p_containerID = base_containerID.get();
160 
161  Suspend(
162  (p_containerID),
164  }
165 
166  if (method_name.compare("Resume") == 0) {
167  Glib::Variant<gint32 > base_containerID;
168  parameters.get_child(base_containerID, 0);
169  gint32 p_containerID;
170  p_containerID = base_containerID.get();
171 
172  Resume(
173  (p_containerID),
175  }
176 
177  if (method_name.compare("Destroy") == 0) {
178  Glib::Variant<gint32 > base_containerID;
179  parameters.get_child(base_containerID, 0);
180  gint32 p_containerID;
181  p_containerID = base_containerID.get();
182 
183  Destroy(
184  (p_containerID),
186  }
187 
188  if (method_name.compare("BindMount") == 0) {
189  Glib::Variant<gint32 > base_containerID;
190  parameters.get_child(base_containerID, 0);
191  gint32 p_containerID;
192  p_containerID = base_containerID.get();
193 
194  Glib::Variant<Glib::ustring > base_pathInHost;
195  parameters.get_child(base_pathInHost, 1);
196  Glib::ustring p_pathInHost;
197  p_pathInHost = base_pathInHost.get();
198 
199  Glib::Variant<Glib::ustring > base_pathInContainer;
200  parameters.get_child(base_pathInContainer, 2);
201  Glib::ustring p_pathInContainer;
202  p_pathInContainer = base_pathInContainer.get();
203 
204  Glib::Variant<bool > base_readOnly;
205  parameters.get_child(base_readOnly, 3);
206  bool p_readOnly;
207  p_readOnly = base_readOnly.get();
208 
209  BindMount(
210  (p_containerID),
211  Glib::ustring(p_pathInHost),
212  Glib::ustring(p_pathInContainer),
213  (p_readOnly),
215  }
216 
217  if (method_name.compare("ListCapabilities") == 0) {
218  ListCapabilities(
220  }
221 
222  if (method_name.compare("SetCapabilities") == 0) {
223  Glib::Variant<gint32 > base_containerID;
224  parameters.get_child(base_containerID, 0);
225  gint32 p_containerID;
226  p_containerID = base_containerID.get();
227 
228  Glib::Variant<std::vector<Glib::ustring> > base_capabilities;
229  parameters.get_child(base_capabilities, 1);
230  std::vector<Glib::ustring> p_capabilities;
231  p_capabilities = base_capabilities.get();
232 
233  SetCapabilities(
234  (p_containerID),
235  SoftwareContainerAgentCommon::glibStringVecToStdStringVec(p_capabilities),
237  }
239  SoftwareContainerAgentMessageHelper msg(invocation);
240  std::string errorMessage(err.what());
241  msg.returnError(errorMessage);
242  }
243 }
244 
245 void com::pelagicore::SoftwareContainerAgent::on_interface_get_property(
246  Glib::VariantBase& /* property */,
247  const Glib::RefPtr<Gio::DBus::Connection>& /* connection */,
248  const Glib::ustring& /* sender */,
249  const Glib::ustring& /* object_path */,
250  const Glib::ustring& /* interface_name */,
251  const Glib::ustring& /* property_name */)
252 {
253 }
254 
255 bool com::pelagicore::SoftwareContainerAgent::on_interface_set_property(
256  const Glib::RefPtr<Gio::DBus::Connection>& /* connection */,
257  const Glib::ustring& /* sender */,
258  const Glib::ustring& /* object_path */,
259  const Glib::ustring& /* interface_name */,
260  const Glib::ustring& /* property_name */,
261  const Glib::VariantBase& /* value */)
262 {
263  return true;
264 }
265 
266 void com::pelagicore::SoftwareContainerAgent::ProcessStateChanged_emitter(
267  gint32 containerID,
268  guint32 processID,
269  bool isRunning,
270  guint32 exitCode)
271 {
272  std::vector<Glib::VariantBase> paramsList;
273  paramsList.push_back(Glib::Variant<gint32 >::create((containerID)));;
274  paramsList.push_back(Glib::Variant<guint32 >::create((processID)));;
275  paramsList.push_back(Glib::Variant<bool >::create((isRunning)));;
276  paramsList.push_back(Glib::Variant<guint32 >::create((exitCode)));;
277 
278  m_connection->emit_signal(
279  "/com/pelagicore/SoftwareContainerAgent",
280  "com.pelagicore.SoftwareContainerAgent",
281  "ProcessStateChanged",
282  Glib::ustring(),
283  Glib::Variant<std::vector<Glib::VariantBase> >::create_tuple(paramsList));
284 }
285 
286 void com::pelagicore::SoftwareContainerAgent::on_bus_acquired(
287  const Glib::RefPtr<Gio::DBus::Connection>& connection,
288  const Glib::ustring& /* name */)
289 {
290  Gio::DBus::InterfaceVTable *interface_vtable =
291  new Gio::DBus::InterfaceVTable(
292  sigc::mem_fun(this, &SoftwareContainerAgent::on_method_call),
293  sigc::mem_fun(this, &SoftwareContainerAgent::on_interface_get_property),
294  sigc::mem_fun(this, &SoftwareContainerAgent::on_interface_set_property));
295  try {
296  registeredId = connection->register_object(m_objectPath,
297  introspection_data->lookup_interface("com.pelagicore.SoftwareContainerAgent"),
298  *interface_vtable);
299  m_connection = connection;
300  } catch(const Glib::Error& ex) {
301  object_not_registered.emit("Registration of object failed");
302  }
303 }
304 
305 void com::pelagicore::SoftwareContainerAgent::on_name_acquired(
306  const Glib::RefPtr<Gio::DBus::Connection>& /* connection */,
307  const Glib::ustring &name)
308 {
309  name_acquired.emit(name);
310 }
311 
312 void com::pelagicore::SoftwareContainerAgent::on_name_lost(
313  const Glib::RefPtr<Gio::DBus::Connection> &connection,
314  const Glib::ustring &name)
315 {
316  if (!connection) {
317  name_lost.emit("Unable to connect to the bus (is a bus running?)");
318  } else {
319  name_lost.emit("Unable to acquire the name "
320  + name + " on the bus (is another agent running?)");
321  }
322 }
323 
324 bool com::pelagicore::SoftwareContainerAgent::emitSignal(const std::string& propName, Glib::VariantBase& value) {
325  std::map<Glib::ustring, Glib::VariantBase> changedProps;
326  std::vector<Glib::ustring> changedPropsNoValue;
327 
328  changedProps[propName] = value;
329 
330  Glib::Variant<std::map<Glib::ustring, Glib::VariantBase> > changedPropsVar = Glib::Variant<std::map <Glib::ustring, Glib::VariantBase> >::create (changedProps);
331  Glib::Variant<std::vector<Glib::ustring> > changedPropsNoValueVar = Glib::Variant<std::vector<Glib::ustring> >::create(changedPropsNoValue);
332  std::vector<Glib::VariantBase> ps;
333  ps.push_back(Glib::Variant<Glib::ustring>::create(m_interfaceName));
334  ps.push_back(changedPropsVar);
335  ps.push_back(changedPropsNoValueVar);
336  Glib::VariantContainerBase propertiesChangedVariant = Glib::Variant<std::vector<Glib::VariantBase> >::create_tuple(ps);
337 
338  m_connection->emit_signal(
339  m_objectPath,
340  "org.freedesktop.DBus.Properties",
341  "PropertiesChanged",
342  Glib::ustring(),
343  propertiesChangedVariant);
344 
345  return true;
346 }