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>
42 * Represents a remote method.
44 template<typename Result, typename ... Args>
50 g_object_unref(m_connection);
54 * Invokes remote method.
56 * @param args Input arguments for remote method.
57 * @return Value returned by remote method.
58 * @throw DBus::InvalidArgumentException If invalid argument(s) supplied.
59 * @throw DBus::ConnectionClosedException If connection is closed.
60 * @throw DBus::Exception If some other error occurs.
62 Result operator()(const Args& ... args)
64 return invoke(args ...);
68 friend class ObjectProxy;
70 MethodProxy(GDBusConnection* connection,
71 const std::string& serviceName,
72 const std::string& objectPath,
73 const std::string& interfaceName,
74 const std::string& methodName) :
75 m_connection(connection),
76 m_serviceName(serviceName),
77 m_objectPath(objectPath),
78 m_interfaceName(interfaceName),
79 m_methodName(methodName)
81 Assert(m_connection && "Connection is not set.");
83 g_object_ref(m_connection);
87 * @remarks Making it a template with parameter set by default to class
88 * template parameter to overload on return type by utilizing
91 template<typename R = Result>
92 typename std::enable_if<!std::is_void<R>::value, R>::type
93 invoke(const Args& ... args)
95 GVariant* parameters = serialize(args ...);
97 GVariant* invokeResult = invokeSync(parameters);
101 ServerDeserialization::deserialize(invokeResult, &result);
103 g_variant_unref(invokeResult);
109 * @remarks Void return type overload.
111 template<typename R = Result>
112 typename std::enable_if<std::is_void<R>::value>::type
113 invoke(const Args& ... args)
115 GVariant* parameters = serialize(args ...);
117 GVariant* invokeResult = invokeSync(parameters);
119 g_variant_unref(invokeResult);
123 * @remarks ArgsM... are the same as Args...; it has been made a template
124 * to make overloading/specialization possible.
126 template<typename ... ArgsM>
127 GVariant* serialize(ArgsM && ... args)
129 return ServerSerialization::serialize(std::forward<ArgsM>(args) ...);
133 * @remarks Specialization for zero-argument functions.
135 GVariant* serialize()
141 * Calls remote method over DBus.
143 * @param parameters Input parameters for the remote method.
144 * @return Result returned by the remote method.
145 * @throw DBus::InvalidArgumentException If invalid argument(s) supplied.
146 * @throw DBus::ConnectionClosedException If connection is closed.
147 * @throw DBus::Exception If some other error occurs.
149 GVariant* invokeSync(GVariant* parameters)
151 GError* error = NULL;
154 "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,
166 if (NULL == result) {
167 std::ostringstream oss;
168 oss << "Error while invoking: "
169 << m_interfaceName << "." << m_methodName
170 << " <" << error->message << ">";
171 std::string message = oss.str();
173 gint code = error->code;
178 case G_IO_ERROR_INVALID_ARGUMENT:
179 ThrowMsg(DBus::InvalidArgumentException, message);
180 case G_IO_ERROR_CLOSED:
181 ThrowMsg(DBus::ConnectionClosedException, message);
183 ThrowMsg(DBus::Exception, message);
191 * Default timeout for synchronous method call.
193 * @see GIO::GDBusConnection::g_dbus_connection_call_sync() for details.
195 static const gint DBUS_SYNC_CALL_TIMEOUT = -1;
197 GDBusConnection* m_connection;
198 std::string m_serviceName;
199 std::string m_objectPath;
200 std::string m_interfaceName;
201 std::string m_methodName;
205 * Smart pointer for MethodProxy objects.
207 template<typename Result, typename ... Args>
211 explicit MethodProxyPtr(MethodProxy<Result, Args ...>* method = NULL) :
215 Result operator()(const Args& ... args) const
217 Assert(NULL != m_method.get() && "Method not set.");
219 return (*m_method)(args ...);
223 std::shared_ptr<MethodProxy<Result, Args ...> > m_method;