2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file method_proxy.h
18 * @author Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
23 #ifndef DPL_DBUS_METHOD_PROXY_H
24 #define DPL_DBUS_METHOD_PROXY_H
26 #include <type_traits>
31 #include <dpl/log/log.h>
32 #include <dpl/assert.h>
33 #include <dpl/dbus/exception.h>
34 #include <dpl/dbus/dbus_server_serialization.h>
35 #include <dpl/dbus/dbus_server_deserialization.h>
43 * Represents a remote method.
45 template<typename Result, typename ...Args>
51 g_object_unref(m_connection);
55 * Invokes remote method.
57 * @param args Input arguments for remote method.
58 * @return Value returned by remote method.
59 * @throw DBus::InvalidArgumentException If invalid argument(s) supplied.
60 * @throw DBus::ConnectionClosedException If connection is closed.
61 * @throw DBus::Exception If some other error occurs.
63 Result operator()(const Args&... args)
65 return invoke(args...);
69 friend class ObjectProxy;
71 MethodProxy(GDBusConnection* connection,
72 const std::string& serviceName,
73 const std::string& objectPath,
74 const std::string& interfaceName,
75 const std::string& methodName)
76 : m_connection(connection),
77 m_serviceName(serviceName),
78 m_objectPath(objectPath),
79 m_interfaceName(interfaceName),
80 m_methodName(methodName)
82 Assert(m_connection && "Connection is not set.");
84 g_object_ref(m_connection);
88 * @remarks Making it a template with parameter set by default to class
89 * template parameter to overload on return type by utilizing
92 template<typename R = Result>
93 typename std::enable_if<!std::is_void<R>::value, R>::type
94 invoke(const Args&... args)
96 GVariant* parameters = serialize(args...);
98 GVariant* invokeResult = invokeSync(parameters);
102 ServerDeserialization::deserialize(invokeResult, &result);
104 g_variant_unref(invokeResult);
110 * @remarks Void return type overload.
112 template<typename R = Result>
113 typename std::enable_if<std::is_void<R>::value>::type
114 invoke(const Args&... args)
116 GVariant* parameters = serialize(args...);
118 GVariant* invokeResult = invokeSync(parameters);
120 g_variant_unref(invokeResult);
124 * @remarks ArgsM... are the same as Args...; it has been made a template
125 * to make overloading/specialization possible.
127 template<typename ...ArgsM>
128 GVariant* serialize(ArgsM&&... args)
130 return ServerSerialization::serialize(std::forward<ArgsM>(args)...);
134 * @remarks Specialization for zero-argument functions.
136 GVariant* serialize()
142 * Calls remote method over DBus.
144 * @param parameters Input parameters for the remote method.
145 * @return Result returned by the remote method.
146 * @throw DBus::InvalidArgumentException If invalid argument(s) supplied.
147 * @throw DBus::ConnectionClosedException If connection is closed.
148 * @throw DBus::Exception If some other error occurs.
150 GVariant* invokeSync(GVariant* parameters)
152 GError* error = NULL;
154 LogPedantic("Invoking method: " << m_interfaceName << "." << m_methodName);
155 GVariant* result = g_dbus_connection_call_sync(m_connection,
156 m_serviceName.c_str(),
157 m_objectPath.c_str(),
158 m_interfaceName.c_str(),
159 m_methodName.c_str(),
161 G_VARIANT_TYPE_TUPLE,
162 G_DBUS_CALL_FLAGS_NONE,
163 DBUS_SYNC_CALL_TIMEOUT,
168 std::ostringstream oss;
169 oss << "Error while invoking: "
170 << m_interfaceName << "." << m_methodName
171 << " <" << error->message << ">";
172 std::string message = oss.str();
174 gint code = error->code;
180 case G_IO_ERROR_INVALID_ARGUMENT:
181 ThrowMsg(DBus::InvalidArgumentException, message);
182 case G_IO_ERROR_CLOSED:
183 ThrowMsg(DBus::ConnectionClosedException, message);
185 ThrowMsg(DBus::Exception, message);
193 * Default timeout for synchronous method call.
195 * @see GIO::GDBusConnection::g_dbus_connection_call_sync() for details.
197 static const gint DBUS_SYNC_CALL_TIMEOUT = -1;
199 GDBusConnection* m_connection;
200 std::string m_serviceName;
201 std::string m_objectPath;
202 std::string m_interfaceName;
203 std::string m_methodName;
207 * Smart pointer for MethodProxy objects.
209 template<typename Result, typename ...Args>
213 explicit MethodProxyPtr(MethodProxy<Result, Args...>* method = NULL)
218 Result operator()(const Args&... args) const
220 Assert(NULL != m_method.get() && "Method not set.");
222 return (*m_method)(args...);
226 std::shared_ptr<MethodProxy<Result, Args...> > m_method;