From 855c585bf6a39817a301e72e61620f6f9776210d Mon Sep 17 00:00:00 2001 From: Jan Olszak Date: Tue, 10 Mar 2015 13:35:55 +0100 Subject: [PATCH] IPC agnostic MethodResultBuilder [Feature] ZoneManager and Zone doesn't know which IPC mechanism is used. [Cause] N/A [Solution] N/A [Verification] Build, run tests, run tests with valgrind Change-Id: I852fa5ed3bcd105096cb7ca740f9e98fb8b6adba --- client/vasum-client-impl.cpp | 4 +- common/api/dbus-method-result-builder.hpp | 88 ++++++ common/api/ipc-method-result-builder.cpp | 54 ++++ common/api/ipc-method-result-builder.hpp | 56 ++++ common/api/messages.hpp | 404 +++++++++++++++++++++++++ common/api/method-result-builder.hpp | 62 ++++ server/host-connection.cpp | 108 ++++--- server/host-connection.hpp | 51 ++-- server/host-dbus-definitions.hpp | 2 +- server/zone-connection.cpp | 18 +- server/zone-connection.hpp | 15 +- server/zone.cpp | 6 +- server/zone.hpp | 6 +- server/zones-manager.cpp | 252 +++++++-------- server/zones-manager.hpp | 66 ++-- tests/unit_tests/server/ut-zone-connection.cpp | 17 +- tests/unit_tests/server/ut-zones-manager.cpp | 4 +- 17 files changed, 947 insertions(+), 266 deletions(-) create mode 100644 common/api/dbus-method-result-builder.hpp create mode 100644 common/api/ipc-method-result-builder.cpp create mode 100644 common/api/ipc-method-result-builder.hpp create mode 100644 common/api/messages.hpp create mode 100644 common/api/method-result-builder.hpp diff --git a/client/vasum-client-impl.cpp b/client/vasum-client-impl.cpp index d8ba562..1ac4c3c 100644 --- a/client/vasum-client-impl.cpp +++ b/client/vasum-client-impl.cpp @@ -74,7 +74,7 @@ void toDict(GVariant* in, VsmArrayString* keys, VsmArrayString* values) value_type* outv = (value_type*)calloc(size + 1, sizeof(value_type)); g_variant_iter_init(&iter, in); - for (int i = 0; g_variant_iter_loop(&iter, "{ss}", &key, &value); i++) { + for (int i = 0; g_variant_iter_loop(&iter, "(ss)", &key, &value); i++) { outk[i] = strdup(key); outv[i] = strdup(value); } @@ -377,7 +377,7 @@ VsmStatus Client::vsm_get_zone_dbuses(VsmArrayString* keys, VsmArrayString* valu VsmStatus ret = callMethod(HOST_INTERFACE, api::host::METHOD_GET_ZONE_DBUSES, NULL, - "(a{ss})", + "(a(ss))", &out); if (ret != VSMCLIENT_SUCCESS) { return ret; diff --git a/common/api/dbus-method-result-builder.hpp b/common/api/dbus-method-result-builder.hpp new file mode 100644 index 0000000..99f3bc0 --- /dev/null +++ b/common/api/dbus-method-result-builder.hpp @@ -0,0 +1,88 @@ +/* +* Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved +* +* Contact: Jan Olszak (j.olszak@samsung.com) +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License +*/ + +/** + * @file + * @author Jan Olszak (j.olszak@samsung.com) + * @brief Interface for result builders + + */ + +#ifndef COMMON_RESULT_DBUS_METHOD_RESULT_BUILDER_HPP +#define COMMON_RESULT_DBUS_METHOD_RESULT_BUILDER_HPP + +#include "config.hpp" + +#include "api/method-result-builder.hpp" + +#include "dbus/connection.hpp" +#include "config/manager.hpp" + +#include +#include + +namespace vasum { +namespace api { + +template +class DbusMethodResultBuilder: public MethodResultBuilder { +public: + DbusMethodResultBuilder(const dbus::MethodResultBuilder::Pointer& methodResultBuilderPtr); + ~DbusMethodResultBuilder() {} + +private: + void setImpl(const std::shared_ptr& data) override; + void setVoid() override; + void setError(const std::string& name, const std::string& message) override; + + dbus::MethodResultBuilder::Pointer mMethodResultBuilderPtr; + std::function)> mSerialize; +}; + +template +DbusMethodResultBuilder::DbusMethodResultBuilder(const dbus::MethodResultBuilder::Pointer& methodResultBuilderPtr) + : mMethodResultBuilderPtr(methodResultBuilderPtr) +{ + mSerialize = [](const std::shared_ptr data)->GVariant* { + return config::saveToGVariant(*std::static_pointer_cast(data)); + }; +} + +template +void DbusMethodResultBuilder::setImpl(const std::shared_ptr& data) +{ + GVariant* parameters = mSerialize(data); + mMethodResultBuilderPtr->set(parameters); +} + +template +void DbusMethodResultBuilder::setVoid() +{ + mMethodResultBuilderPtr->setVoid(); +} + +template +void DbusMethodResultBuilder::setError(const std::string& name, const std::string& message) +{ + mMethodResultBuilderPtr->setError(name, message); +} + +} // namespace result +} // namespace vasum + +#endif // COMMON_RESULT_DBUS_METHOD_RESULT_BUILDER_HPP diff --git a/common/api/ipc-method-result-builder.cpp b/common/api/ipc-method-result-builder.cpp new file mode 100644 index 0000000..4ea2845 --- /dev/null +++ b/common/api/ipc-method-result-builder.cpp @@ -0,0 +1,54 @@ +/* +* Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved +* +* Contact: Jan Olszak (j.olszak@samsung.com) +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License +*/ + +/** + * @file + * @author Jan Olszak (j.olszak@samsung.com) + * @brief Class for sending the result of a method + */ + +#include "api/ipc-method-result-builder.hpp" + +namespace vasum { +namespace api { + +IPCMethodResultBuilder::IPCMethodResultBuilder(const ipc::MethodResult::Pointer& methodResultPtr) + : mMethodResultPtr(methodResultPtr) +{ +} + +void IPCMethodResultBuilder::setImpl(const std::shared_ptr& data) +{ + mMethodResultPtr->set(data); +} + +void IPCMethodResultBuilder::setVoid() +{ + mMethodResultPtr->setVoid(); +} + +void IPCMethodResultBuilder::setError(const std::string& , const std::string& message) +{ + // TODO: Change int codes to string names in IPC MethodResult + mMethodResultPtr->setError(1, message); +} + +} // namespace result +} // namespace vasum + + diff --git a/common/api/ipc-method-result-builder.hpp b/common/api/ipc-method-result-builder.hpp new file mode 100644 index 0000000..7b03ece --- /dev/null +++ b/common/api/ipc-method-result-builder.hpp @@ -0,0 +1,56 @@ +/* +* Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved +* +* Contact: Jan Olszak (j.olszak@samsung.com) +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License +*/ + +/** + * @file + * @author Jan Olszak (j.olszak@samsung.com) + * @brief Interface for result builders + + */ + +#ifndef COMMON_RESULT_IPC_METHOD_RESULT_BUILDER_HPP +#define COMMON_RESULT_IPC_METHOD_RESULT_BUILDER_HPP + +#include "config.hpp" + +#include "api/method-result-builder.hpp" +#include "ipc/method-result.hpp" + +#include + +namespace vasum { +namespace api { + +class IPCMethodResultBuilder: public MethodResultBuilder { +public: + IPCMethodResultBuilder(const ipc::MethodResult::Pointer& methodResult); + ~IPCMethodResultBuilder() {} + +private: + void setImpl(const std::shared_ptr& data) override; + void setVoid() override; + void setError(const std::string& name, const std::string& message) override; + + ipc::MethodResult::Pointer mMethodResultPtr; +}; + + +} // namespace result +} // namespace vasum + +#endif // COMMON_RESULT_IPC_METHOD_RESULT_BUILDER_HPP diff --git a/common/api/messages.hpp b/common/api/messages.hpp new file mode 100644 index 0000000..6c6d4da --- /dev/null +++ b/common/api/messages.hpp @@ -0,0 +1,404 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Jan Olszak (j.olszak@samsung.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +/** + * @file + * @author Jan Olszak (j.olszak@samsung.com) + * @brief Host's internal IPC messages declaration + */ + + +#ifndef COMMON_API_MESSAGES +#define COMMON_API_MESSAGES + +#include "config/fields.hpp" +#include +#include + +namespace vasum { +namespace api { + +struct Void { + CONFIG_REGISTER_EMPTY +}; + +struct String { + std::string value; + + CONFIG_REGISTER + ( + value + ) +}; + +struct StringPair { + std::string first; + std::string second; + + CONFIG_REGISTER + ( + first, + second + ) +}; + +struct VectorOfStrings { + std::vector values; + + CONFIG_REGISTER + ( + values + ) +}; + +struct VectorOfStringPairs { + std::vector values; + + CONFIG_REGISTER + ( + values + ) +}; + +typedef api::String ZoneId; +typedef api::String Declaration; +typedef api::String FileMoveRequestStatus; +typedef api::VectorOfStrings ZoneIds; +typedef api::VectorOfStrings Declarations; +typedef api::VectorOfStrings NetDevList; +typedef api::VectorOfStringPairs Dbuses; +typedef api::VectorOfStringPairs NetDevAttrs; + +struct ZoneInfo { + std::string id; + int vt; + std::string state; + std::string rootPath; + + CONFIG_REGISTER + ( + id, + vt, + state, + rootPath + ) +}; + +// struct MethodSetActiveZoneConfig { +// std::string id; + +// CONFIG_REGISTER +// ( +// id +// ) +// }; + +// struct MethodGetZoneDbusesConfig { +// CONFIG_REGISTER_EMPTY +// }; + +// struct MethodGetZoneIdListConfig { +// CONFIG_REGISTER_EMPTY +// }; + + +// struct MethodGetActiveZoneIdConfig { +// CONFIG_REGISTER_EMPTY +// }; + + +// struct MethodGetZoneInfoConfig { +// std::string id; + +// CONFIG_REGISTER +// ( +// id +// ) +// }; + +// struct MethodSetNetDevAttrsConfig { +// std::string zone; +// std::string netdev; + +// struct Store { +// std::string key; +// std::string value; + +// CONFIG_REGISTER +// ( +// key, +// value +// ) +// }; + +// std::vector attrs; + +// CONFIG_REGISTER +// ( +// zone, +// netdev, +// attrs +// ) +// }; + +// struct MethodGetNetDevAttrsConfig { +// std::string zone; +// std::string netdev; + +// CONFIG_REGISTER +// ( +// zone, +// netdev +// ) +// }; + +// struct MethodGetNetDevListConfig { +// std::string zone; + +// CONFIG_REGISTER +// ( +// zone +// ) +// }; + +// struct MethodCreateNetDevVethConfig { +// std::string id; +// std::string zoneDev; +// std::string hostDev; + +// CONFIG_REGISTER +// ( +// id, +// zoneDev, +// hostDev +// ) +// }; + +// struct MethodCreateNetDevMacvlanConfig { +// std::string id; +// std::string zoneDev; +// std::string hostDev; + +// CONFIG_REGISTER +// ( +// id, +// zoneDev, +// hostDev +// ) +// }; + +// struct MethodCreateNetDevPhysConfig { +// std::string id; +// std::string devId; + +// CONFIG_REGISTER +// ( +// id, +// devId +// ) +// }; + +// struct MethodGetDeclareFileConfig { +// std::string zone; +// int32_t type; +// std::string path; +// int32_t flags; +// int32_t mode; + +// CONFIG_REGISTER +// ( +// zone, +// type, +// path, +// flags, +// mode +// ) +// }; + +// struct MethodGetDeclareMountConfig { +// std::string source; +// std::string zone; +// std::string target; +// uint64_t flags; +// std::string data; + +// CONFIG_REGISTER +// ( +// source, +// zone, +// target, +// flags, +// data +// ) +// }; + +// struct MethodGetDeclareLinkConfig { +// std::string source; +// std::string zone; +// std::string target; + +// CONFIG_REGISTER +// ( +// source, +// zone, +// target +// ) +// }; + +// struct MethodGetDeclarationConfig { +// std::string zone; +// std::string declarationId; + +// CONFIG_REGISTER +// ( +// zone, +// declarationId +// ) +// }; + +// struct MethodRemoveDeclarationConfig { +// std::string id; +// std::string declarationId; + +// CONFIG_REGISTER +// ( +// id, +// declarationId +// ) +// }; + +// struct MethodCreateZoneConfig { +// std::string id; +// std::string templateName; + +// CONFIG_REGISTER +// ( +// id, +// templateName +// ) +// }; + +// struct MethodDestroyZoneConfig { +// std::string id; + +// CONFIG_REGISTER +// ( +// id +// ) +// }; + + +// struct MethodShutdownZoneConfig { +// std::string id; + +// CONFIG_REGISTER +// ( +// id +// ) +// }; + +// struct MethodStartZoneConfig { +// std::string id; + +// CONFIG_REGISTER +// ( +// id +// ) +// }; + +// struct MethodLockZoneConfig { +// std::string id; + +// CONFIG_REGISTER +// ( +// id +// ) +// }; + +// struct MethodUnlockZoneConfig { +// std::string id; + +// CONFIG_REGISTER +// ( +// id +// ) +// }; + +// struct MethodGrantDeviceConfig { +// std::string id; +// std::string device; +// uint32_t flags; + +// CONFIG_REGISTER +// ( +// id, +// device, +// flags +// ) +// }; + +// struct MethodRevokeDeviceConfig { +// std::string id; +// std::string device; + +// CONFIG_REGISTER +// ( +// id, +// device +// ) +// }; + +// TODO: Agregate configs if it makes sense. For example: MethodLockZoneConfig and MethodUnlockZoneConfig + + +// Zone: +// struct MethodNotifyActiveZoneConfig { +// std::string application; +// std::string message; + +// CONFIG_REGISTER +// ( +// application, +// message +// ) +// }; + +// struct MethodFileMoveRequest { +// std::string destination; +// std::string path; + +// CONFIG_REGISTER +// ( +// destination, +// path +// ) +// }; + +// struct MethodFileMoveRequestResult { +// std::string result; + +// CONFIG_REGISTER +// ( +// result +// ) +// }; + +} // namespace api +} // namespace vasum + +#endif // COMMON_API_MESSAGES diff --git a/common/api/method-result-builder.hpp b/common/api/method-result-builder.hpp new file mode 100644 index 0000000..04ede40 --- /dev/null +++ b/common/api/method-result-builder.hpp @@ -0,0 +1,62 @@ +/* +* Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved +* +* Contact: Jan Olszak (j.olszak@samsung.com) +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License +*/ + +/** + * @file + * @author Jan Olszak (j.olszak@samsung.com) + * @brief Interface for result builders + + */ + +#ifndef COMMON_RESULT_METHOD_RESULT_BUILDER_HPP +#define COMMON_RESULT_METHOD_RESULT_BUILDER_HPP + +#include + +#include "config/is-union.hpp" + +namespace vasum { +namespace api { + +/** + * An interface used to set a result to a method call. + */ +class MethodResultBuilder { +public: + typedef std::shared_ptr Pointer; + + virtual ~MethodResultBuilder() {} + virtual void setVoid() = 0; + virtual void setError(const std::string& name, const std::string& message) = 0; + + template + void set(const std::shared_ptr& data) + { + static_assert(config::isVisitable::value, "Use only libConfig's structures"); + setImpl(data); + } + +private: + virtual void setImpl(const std::shared_ptr& data) =0; + +}; + +} // namespace api +} // namespace vasum + +#endif // COMMON_RESULT_METHOD_RESULT_BUILDER_HPP diff --git a/server/host-connection.cpp b/server/host-connection.cpp index 6790480..a1a7448 100644 --- a/server/host-connection.cpp +++ b/server/host-connection.cpp @@ -27,6 +27,8 @@ #include "host-connection.hpp" #include "host-dbus-definitions.hpp" #include "exception.hpp" +#include "api/dbus-method-result-builder.hpp" +#include "api/messages.hpp" #include "logger/logger.hpp" @@ -65,7 +67,7 @@ HostConnection::HostConnection() mDbusConnection->registerObject(api::host::OBJECT_PATH, api::host::DEFINITION, std::bind(&HostConnection::onMessageCall, - this, _1, _2, _3, _4, _5)); + this, _1, _2, _3, _4, _5)); LOGD("Connected"); } @@ -246,14 +248,16 @@ void HostConnection::onMessageCall(const std::string& objectPath, g_variant_get(parameters, "(&s)", &id); if (mSetActiveZoneCallback) { - mSetActiveZoneCallback(id, result); + auto rb = std::make_shared>(result); + mSetActiveZoneCallback(id, rb); } return; } if (methodName == api::host::METHOD_GET_ZONE_DBUSES) { if (mGetZoneDbusesCallback) { - mGetZoneDbusesCallback(result); + auto rb = std::make_shared>(result); + mGetZoneDbusesCallback(rb); } return; } @@ -287,31 +291,34 @@ void HostConnection::onMessageCall(const std::string& objectPath, return; } - if (methodName == api::host::METHOD_GET_ZONE_ID_LIST){ - if (mGetZoneIdsCallback){ - mGetZoneIdsCallback(result); + if (methodName == api::host::METHOD_GET_ZONE_ID_LIST) { + if (mGetZoneIdsCallback) { + auto rb = std::make_shared>(result); + mGetZoneIdsCallback(rb); } return; } - if (methodName == api::host::METHOD_GET_ACTIVE_ZONE_ID){ - if (mGetActiveZoneIdCallback){ - mGetActiveZoneIdCallback(result); + if (methodName == api::host::METHOD_GET_ACTIVE_ZONE_ID) { + if (mGetActiveZoneIdCallback) { + auto rb = std::make_shared>(result); + mGetActiveZoneIdCallback(rb); } return; } - if (methodName == api::host::METHOD_GET_ZONE_INFO){ + if (methodName == api::host::METHOD_GET_ZONE_INFO) { const gchar* id = NULL; g_variant_get(parameters, "(&s)", &id); if (mGetZoneInfoCallback) { - mGetZoneInfoCallback(id, result); + auto rb = std::make_shared>(result); + mGetZoneInfoCallback(id, rb); } return; } - if (methodName == api::host::METHOD_SET_NETDEV_ATTRS){ + if (methodName == api::host::METHOD_SET_NETDEV_ATTRS) { const gchar* zone = NULL; const gchar* netdev = NULL; GVariantIter* iter; @@ -324,26 +331,29 @@ void HostConnection::onMessageCall(const std::string& objectPath, } g_variant_iter_free(iter); if (mSetNetdevAttrsCallback) { - mSetNetdevAttrsCallback(zone, netdev, attrs, result); + auto rb = std::make_shared>(result); + mSetNetdevAttrsCallback(zone, netdev, attrs, rb); } return; } - if (methodName == api::host::METHOD_GET_NETDEV_ATTRS){ + if (methodName == api::host::METHOD_GET_NETDEV_ATTRS) { const gchar* zone = NULL; const gchar* netdev = NULL; g_variant_get(parameters, "(&s&s)", &zone, &netdev); if (mGetNetdevAttrsCallback) { - mGetNetdevAttrsCallback(zone, netdev, result); + auto rb = std::make_shared>(result); + mGetNetdevAttrsCallback(zone, netdev, rb); } return; } - if (methodName == api::host::METHOD_GET_NETDEV_LIST){ + if (methodName == api::host::METHOD_GET_NETDEV_LIST) { const gchar* zone = NULL; g_variant_get(parameters, "(&s)", &zone); if (mGetNetdevListCallback) { - mGetNetdevListCallback(zone, result); + auto rb = std::make_shared>(result); + mGetNetdevListCallback(zone, rb); } return; } @@ -355,7 +365,8 @@ void HostConnection::onMessageCall(const std::string& objectPath, const gchar* hostDev = NULL; g_variant_get(parameters, "(&s&s&s)", &id, &zoneDev, &hostDev); if (mCreateNetdevVethCallback) { - mCreateNetdevVethCallback(id, zoneDev, hostDev, result); + auto rb = std::make_shared>(result); + mCreateNetdevVethCallback(id, zoneDev, hostDev, rb); } return; } @@ -367,7 +378,8 @@ void HostConnection::onMessageCall(const std::string& objectPath, guint32 mode; g_variant_get(parameters, "(&s&s&su)", &id, &zoneDev, &hostDev, &mode); if (mCreateNetdevMacvlanCallback) { - mCreateNetdevMacvlanCallback(id, zoneDev, hostDev, mode, result); + auto rb = std::make_shared>(result); + mCreateNetdevMacvlanCallback(id, zoneDev, hostDev, mode, rb); } } @@ -376,7 +388,8 @@ void HostConnection::onMessageCall(const std::string& objectPath, const gchar* devId = NULL; g_variant_get(parameters, "(&s&s)", &id, &devId); if (mCreateNetdevPhysCallback) { - mCreateNetdevPhysCallback(id, devId, result); + auto rb = std::make_shared>(result); + mCreateNetdevPhysCallback(id, devId, rb); } } @@ -389,7 +402,8 @@ void HostConnection::onMessageCall(const std::string& objectPath, g_variant_get(parameters, "(&si&sii)", &zone, &type, &path, &flags, &mode); if (mDeclareFileCallback) { - mDeclareFileCallback(zone, type, path, flags, mode, result); + auto rb = std::make_shared>(result); + mDeclareFileCallback(zone, type, path, flags, mode, rb); } return; } @@ -411,7 +425,8 @@ void HostConnection::onMessageCall(const std::string& objectPath, &data); if (mDeclareMountCallback) { - mDeclareMountCallback(source, zone, target, type, flags, data, result); + auto rb = std::make_shared>(result); + mDeclareMountCallback(source, zone, target, type, flags, data, rb); } return; } @@ -423,7 +438,8 @@ void HostConnection::onMessageCall(const std::string& objectPath, g_variant_get(parameters, "(&s&s&s)", &source, &zone, &target); if (mDeclareLinkCallback) { - mDeclareLinkCallback(source, zone, target, result); + auto rb = std::make_shared>(result); + mDeclareLinkCallback(source, zone, target, rb); } return; } @@ -433,7 +449,8 @@ void HostConnection::onMessageCall(const std::string& objectPath, g_variant_get(parameters, "(&s)", &zone); if (mGetDeclarationsCallback) { - mGetDeclarationsCallback(zone, result); + auto rb = std::make_shared>(result); + mGetDeclarationsCallback(zone, rb); } return; } @@ -444,7 +461,8 @@ void HostConnection::onMessageCall(const std::string& objectPath, g_variant_get(parameters, "(&s&s)", &zone, &declarationId); if (mRemoveDeclarationCallback) { - mRemoveDeclarationCallback(zone, declarationId, result); + auto rb = std::make_shared>(result); + mRemoveDeclarationCallback(zone, declarationId, rb); } return; } @@ -454,8 +472,9 @@ void HostConnection::onMessageCall(const std::string& objectPath, const gchar* templateName = NULL; g_variant_get(parameters, "(&s&s)", &id, &templateName); - if (mCreateZoneCallback){ - mCreateZoneCallback(id, templateName, result); + if (mCreateZoneCallback) { + auto rb = std::make_shared>(result); + mCreateZoneCallback(id, templateName, rb); } return; } @@ -464,8 +483,9 @@ void HostConnection::onMessageCall(const std::string& objectPath, const gchar* id = NULL; g_variant_get(parameters, "(&s)", &id); - if (mDestroyZoneCallback){ - mDestroyZoneCallback(id, result); + if (mDestroyZoneCallback) { + auto rb = std::make_shared>(result); + mDestroyZoneCallback(id, rb); } return; } @@ -474,8 +494,9 @@ void HostConnection::onMessageCall(const std::string& objectPath, const gchar* id = NULL; g_variant_get(parameters, "(&s)", &id); - if (mShutdownZoneCallback){ - mShutdownZoneCallback(id, result); + if (mShutdownZoneCallback) { + auto rb = std::make_shared>(result); + mShutdownZoneCallback(id, rb); } } @@ -483,8 +504,9 @@ void HostConnection::onMessageCall(const std::string& objectPath, const gchar* id = NULL; g_variant_get(parameters, "(&s)", &id); - if (mStartZoneCallback){ - mStartZoneCallback(id, result); + if (mStartZoneCallback) { + auto rb = std::make_shared>(result); + mStartZoneCallback(id, rb); } } @@ -492,8 +514,9 @@ void HostConnection::onMessageCall(const std::string& objectPath, const gchar* id = NULL; g_variant_get(parameters, "(&s)", &id); - if (mLockZoneCallback){ - mLockZoneCallback(id, result); + if (mLockZoneCallback) { + auto rb = std::make_shared>(result); + mLockZoneCallback(id, rb); } return; } @@ -502,8 +525,9 @@ void HostConnection::onMessageCall(const std::string& objectPath, const gchar* id = NULL; g_variant_get(parameters, "(&s)", &id); - if (mUnlockZoneCallback){ - mUnlockZoneCallback(id, result); + if (mUnlockZoneCallback) { + auto rb = std::make_shared>(result); + mUnlockZoneCallback(id, rb); } return; } @@ -514,8 +538,9 @@ void HostConnection::onMessageCall(const std::string& objectPath, uint32_t flags; g_variant_get(parameters, "(&s&su)", &id, &device, &flags); - if (mGrantDeviceCallback){ - mGrantDeviceCallback(id, device, flags, result); + if (mGrantDeviceCallback) { + auto rb = std::make_shared>(result); + mGrantDeviceCallback(id, device, flags, rb); } return; } @@ -525,8 +550,9 @@ void HostConnection::onMessageCall(const std::string& objectPath, const gchar* device = NULL; g_variant_get(parameters, "(&s&s)", &id, &device); - if (mRevokeDeviceCallback){ - mRevokeDeviceCallback(id, device, result); + if (mRevokeDeviceCallback) { + auto rb = std::make_shared>(result); + mRevokeDeviceCallback(id, device, rb); } return; } diff --git a/server/host-connection.hpp b/server/host-connection.hpp index 569c68f..3ef8cdf 100644 --- a/server/host-connection.hpp +++ b/server/host-connection.hpp @@ -27,6 +27,7 @@ #define SERVER_HOST_CONNECTION_HPP #include "dbus/connection.hpp" +#include "api/method-result-builder.hpp" #include #include @@ -53,48 +54,48 @@ public: GVariant* parameters, dbus::MethodResultBuilder::Pointer result )> ProxyCallCallback; - typedef std::function GetZoneDbusesCallback; - typedef std::function GetZoneIdsCallback; - typedef std::function GetActiveZoneIdCallback; typedef std::function GetZoneInfoCallback; typedef std::function>& attrs, - dbus::MethodResultBuilder::Pointer result + api::MethodResultBuilder::Pointer result )> SetNetdevAttrsCallback; typedef std::function GetNetdevAttrsCallback; typedef std::function GetNetdevListCallback; typedef std::function CreateNetdevVethCallback; typedef std::function CreateNetdevMacvlanCallback; typedef std::function CreateNetdevPhysCallback; typedef std::function DeclareFileCallback; typedef std::function DeclareMountCallback; typedef std::function DeclareLinkCallback; typedef std::function GetDeclarationsCallback; typedef std::function RemoveDeclarationCallback; typedef std::function SetActiveZoneCallback; typedef std::function CreateZoneCallback; typedef std::function DestroyZoneCallback; typedef std::function ShutdownZoneCallback; typedef std::function StartZoneCallback; typedef std::function LockZoneCallback; typedef std::function UnlockZoneCallback; typedef std::function GrantDeviceCallback; typedef std::function RevokeDeviceCallback; /** @@ -171,7 +172,7 @@ public: /** * Register a callback called to get the active zone id */ - void setGetActiveZoneIdCallback(const GetZoneIdsCallback& callback); + void setGetActiveZoneIdCallback(const GetActiveZoneIdCallback& callback); /** * Register a callback called to get the zone informations diff --git a/server/host-dbus-definitions.hpp b/server/host-dbus-definitions.hpp index 2c2a201..34d64eb 100644 --- a/server/host-dbus-definitions.hpp +++ b/server/host-dbus-definitions.hpp @@ -79,7 +79,7 @@ const std::string DEFINITION = " " " " " " - " " + " " " " " " " " diff --git a/server/zone-connection.cpp b/server/zone-connection.cpp index 35341b3..a86886c 100644 --- a/server/zone-connection.cpp +++ b/server/zone-connection.cpp @@ -30,6 +30,9 @@ // TODO: Switch to real power-manager dbus defs when they will be implemented in power-manager #include "fake-power-manager-dbus-definitions.hpp" +#include "api/dbus-method-result-builder.hpp" +#include "api/messages.hpp" + #include "logger/logger.hpp" @@ -139,10 +142,10 @@ void ZoneConnection::setDisplayOffCallback(const DisplayOffCallback& callback) mDisplayOffCallback = callback; } -void ZoneConnection::setFileMoveRequestCallback( - const FileMoveRequestCallback& callback) +void ZoneConnection::setFileMoveCallback( + const FileMoveCallback& callback) { - mFileMoveRequestCallback = callback; + mFileMoveCallback = callback; } void ZoneConnection::setProxyCallCallback(const ProxyCallCallback& callback) @@ -165,8 +168,8 @@ void ZoneConnection::onMessageCall(const std::string& objectPath, const gchar* message = NULL; g_variant_get(parameters, "(&s&s)", &application, &message); if (mNotifyActiveZoneCallback) { - mNotifyActiveZoneCallback(application, message); - result->setVoid(); + auto rb = std::make_shared>(result); + mNotifyActiveZoneCallback(application, message, rb); } } @@ -174,8 +177,9 @@ void ZoneConnection::onMessageCall(const std::string& objectPath, const gchar* destination = NULL; const gchar* path = NULL; g_variant_get(parameters, "(&s&s)", &destination, &path); - if (mFileMoveRequestCallback) { - mFileMoveRequestCallback(destination, path, result); + if (mFileMoveCallback) { + auto rb = std::make_shared>(result); + mFileMoveCallback(destination, path, rb); } } diff --git a/server/zone-connection.hpp b/server/zone-connection.hpp index 78b6e56..8f025b0 100644 --- a/server/zone-connection.hpp +++ b/server/zone-connection.hpp @@ -27,11 +27,11 @@ #define SERVER_ZONE_CONNECTION_HPP #include "dbus/connection.hpp" +#include "api/method-result-builder.hpp" #include #include - namespace vasum { @@ -47,13 +47,14 @@ public: // ------------- API -------------- typedef std::function NotifyActiveZoneCallback; typedef std::function FileMoveRequestCallback; + api::MethodResultBuilder::Pointer result + )> FileMoveCallback; typedef std::function ProxyCallCallback; + )> ProxyCallCallback; /** * Register notification request callback @@ -77,7 +78,7 @@ public: /* * Register file move request callback */ - void setFileMoveRequestCallback(const FileMoveRequestCallback& callback); + void setFileMoveCallback(const FileMoveCallback& callback); /** * Register proxy call callback @@ -110,7 +111,7 @@ private: OnNameLostCallback mOnNameLostCallback; NotifyActiveZoneCallback mNotifyActiveZoneCallback; DisplayOffCallback mDisplayOffCallback; - FileMoveRequestCallback mFileMoveRequestCallback; + FileMoveCallback mFileMoveCallback; ProxyCallCallback mProxyCallCallback; void onNameAcquired(); diff --git a/server/zone.cpp b/server/zone.cpp index 1bc2d09..181cc9e 100644 --- a/server/zone.cpp +++ b/server/zone.cpp @@ -206,7 +206,7 @@ void Zone::connect() mConnection->setDisplayOffCallback(mDisplayOffCallback); } if (mFileMoveCallback) { - mConnection->setFileMoveRequestCallback(mFileMoveCallback); + mConnection->setFileMoveCallback(mFileMoveCallback); } if (mProxyCallCallback) { mConnection->setProxyCallCallback(mProxyCallCallback); @@ -411,13 +411,13 @@ void Zone::setDisplayOffCallback(const DisplayOffCallback& callback) } } -void Zone::setFileMoveRequestCallback(const FileMoveRequestCallback& callback) +void Zone::setFileMoveCallback(const FileMoveCallback& callback) { Lock lock(mReconnectMutex); mFileMoveCallback = callback; if (mConnection) { - mConnection->setFileMoveRequestCallback(callback); + mConnection->setFileMoveCallback(callback); } } diff --git a/server/zone.hpp b/server/zone.hpp index 69d0324..45bacbd 100644 --- a/server/zone.hpp +++ b/server/zone.hpp @@ -67,7 +67,7 @@ public: typedef ZoneConnection::NotifyActiveZoneCallback NotifyActiveZoneCallback; typedef ZoneConnection::DisplayOffCallback DisplayOffCallback; - typedef ZoneConnection::FileMoveRequestCallback FileMoveRequestCallback; + typedef ZoneConnection::FileMoveCallback FileMoveCallback; typedef ZoneConnection::ProxyCallCallback ProxyCallCallback; typedef std::function DbusStateChangedCallback; @@ -210,7 +210,7 @@ public: /** * Register file move request callback */ - void setFileMoveRequestCallback(const FileMoveRequestCallback& callback); + void setFileMoveCallback(const FileMoveCallback& callback); /** * Register dbus state changed callback @@ -319,7 +319,7 @@ private: mutable std::recursive_mutex mReconnectMutex; NotifyActiveZoneCallback mNotifyCallback; DisplayOffCallback mDisplayOffCallback; - FileMoveRequestCallback mFileMoveCallback; + FileMoveCallback mFileMoveCallback; ProxyCallCallback mProxyCallCallback; DbusStateChangedCallback mDbusStateChangedCallback; std::string mDbusAddress; diff --git a/server/zones-manager.cpp b/server/zones-manager.cpp index d90bab7..2dcfaa0 100644 --- a/server/zones-manager.cpp +++ b/server/zones-manager.cpp @@ -40,6 +40,7 @@ #include "utils/fs.hpp" #include "utils/img.hpp" #include "utils/environment.hpp" +#include "api/messages.hpp" #include #include @@ -56,7 +57,7 @@ namespace { bool regexMatchVector(const std::string& str, const std::vector& v) { - for (const boost::regex& toMatch: v) { + for (const boost::regex& toMatch : v) { if (boost::regex_match(str, toMatch)) { return true; } @@ -129,7 +130,7 @@ ZonesManager::ZonesManager(const std::string& configPath) mHostConnection.setProxyCallCallback(bind(&ZonesManager::handleProxyCall, this, HOST_ID, _1, _2, _3, _4, _5, _6, _7)); - mHostConnection.setGetZoneDbusesCallback(bind(&ZonesManager::handleGetZoneDbuses, + mHostConnection.setGetZoneDbusesCallback(bind(&ZonesManager::handleGetZoneDbusesCall, this, _1)); mHostConnection.setGetZoneIdsCallback(bind(&ZonesManager::handleGetZoneIdsCall, @@ -142,22 +143,22 @@ ZonesManager::ZonesManager(const std::string& configPath) this, _1, _2)); mHostConnection.setSetNetdevAttrsCallback(bind(&ZonesManager::handleSetNetdevAttrsCall, - this, _1, _2, _3, _4)); + this, _1, _2, _3, _4)); mHostConnection.setGetNetdevAttrsCallback(bind(&ZonesManager::handleGetNetdevAttrsCall, - this, _1, _2, _3)); + this, _1, _2, _3)); mHostConnection.setGetNetdevListCallback(bind(&ZonesManager::handleGetNetdevListCall, - this, _1, _2)); + this, _1, _2)); mHostConnection.setCreateNetdevVethCallback(bind(&ZonesManager::handleCreateNetdevVethCall, - this, _1, _2, _3, _4)); + this, _1, _2, _3, _4)); mHostConnection.setCreateNetdevMacvlanCallback(bind(&ZonesManager::handleCreateNetdevMacvlanCall, - this, _1, _2, _3, _4, _5)); + this, _1, _2, _3, _4, _5)); mHostConnection.setCreateNetdevPhysCallback(bind(&ZonesManager::handleCreateNetdevPhysCall, - this, _1, _2, _3)); + this, _1, _2, _3)); mHostConnection.setDeclareFileCallback(bind(&ZonesManager::handleDeclareFileCall, this, _1, _2, _3, _4, _5, _6)); @@ -172,7 +173,7 @@ ZonesManager::ZonesManager(const std::string& configPath) this, _1, _2)); mHostConnection.setRemoveDeclarationCallback(bind(&ZonesManager::handleRemoveDeclarationCall, - this, _1, _2, _3)); + this, _1, _2, _3)); mHostConnection.setSetActiveZoneCallback(bind(&ZonesManager::handleSetActiveZoneCall, this, _1, _2)); @@ -212,9 +213,9 @@ ZonesManager::ZonesManager(const std::string& configPath) if (mConfig.inputConfig.enabled) { LOGI("Registering input monitor [" << mConfig.inputConfig.device.c_str() << "]"); mSwitchingSequenceMonitor.reset( - new InputMonitor(mConfig.inputConfig, - std::bind(&ZonesManager::switchingSequenceMonitorNotify, - this))); + new InputMonitor(mConfig.inputConfig, + std::bind(&ZonesManager::switchingSequenceMonitorNotify, + this))); } @@ -307,13 +308,13 @@ void ZonesManager::insertZone(const std::string& zoneId, const std::string& zone mConfig.runMountPointPrefix)); using namespace std::placeholders; - zone->setNotifyActiveZoneCallback(bind(&ZonesManager::notifyActiveZoneHandler, - this, zoneId, _1, _2)); + zone->setNotifyActiveZoneCallback(bind(&ZonesManager::handleNotifyActiveZoneCall, + this, zoneId, _1, _2, _3)); - zone->setDisplayOffCallback(bind(&ZonesManager::displayOffHandler, + zone->setDisplayOffCallback(bind(&ZonesManager::handleDisplayOffCall, this, zoneId)); - zone->setFileMoveRequestCallback(bind(&ZonesManager::handleZoneMoveFileRequest, + zone->setFileMoveCallback(bind(&ZonesManager::handleFileMoveCall, this, zoneId, _1, _2, _3)); zone->setProxyCallCallback(bind(&ZonesManager::handleProxyCall, @@ -547,11 +548,12 @@ void ZonesManager::setZonesDetachOnExit() } } -void ZonesManager::notifyActiveZoneHandler(const std::string& caller, - const std::string& application, - const std::string& message) +void ZonesManager::handleNotifyActiveZoneCall(const std::string& caller, + const std::string& application, + const std::string& message, + api::MethodResultBuilder::Pointer result) { - LOGI("notifyActiveZoneHandler(" << caller << ", " << application << ", " << message + LOGI("handleNotifyActiveZoneCall(" << caller << ", " << application << ", " << message << ") called"); Lock lock(mMutex); @@ -561,12 +563,14 @@ void ZonesManager::notifyActiveZoneHandler(const std::string& caller, if (iter != mZones.end() && caller != get(iter).getId()) { get(iter).sendNotification(caller, application, message); } - } catch(const VasumException&) { + result->setVoid(); + } catch (const VasumException&) { LOGE("Notification from " << caller << " hasn't been sent"); + result->setError(api::ERROR_INTERNAL, "Notification hasn't been sent"); } } -void ZonesManager::displayOffHandler(const std::string& /*caller*/) +void ZonesManager::handleDisplayOffCall(const std::string& /*caller*/) { // get config of currently set zone and switch if switchToDefaultAfterTimeout is true Lock lock(mMutex); @@ -584,10 +588,10 @@ void ZonesManager::displayOffHandler(const std::string& /*caller*/) } } -void ZonesManager::handleZoneMoveFileRequest(const std::string& srcZoneId, - const std::string& dstZoneId, - const std::string& path, - dbus::MethodResultBuilder::Pointer result) +void ZonesManager::handleFileMoveCall(const std::string& srcZoneId, + const std::string& dstZoneId, + const std::string& path, + api::MethodResultBuilder::Pointer result) { // TODO: this implementation is only a placeholder. // There are too many unanswered questions and security concerns: @@ -622,29 +626,35 @@ void ZonesManager::handleZoneMoveFileRequest(const std::string& srcZoneId, } Zone& srcZone = get(srcIter); + auto status = std::make_shared(); + auto dstIter = findZone(dstZoneId); if (dstIter == mZones.end()) { LOGE("Destination zone '" << dstZoneId << "' not found"); - result->set(g_variant_new("(s)", api::zone::FILE_MOVE_DESTINATION_NOT_FOUND.c_str())); + status->value = api::zone::FILE_MOVE_DESTINATION_NOT_FOUND; + result->set(status); return; } Zone& dstContanier = get(dstIter); if (srcZoneId == dstZoneId) { LOGE("Cannot send a file to yourself"); - result->set(g_variant_new("(s)", api::zone::FILE_MOVE_WRONG_DESTINATION.c_str())); + status->value = api::zone::FILE_MOVE_WRONG_DESTINATION; + result->set(status); return; } if (!regexMatchVector(path, srcZone.getPermittedToSend())) { LOGE("Source zone has no permissions to send the file: " << path); - result->set(g_variant_new("(s)", api::zone::FILE_MOVE_NO_PERMISSIONS_SEND.c_str())); + status->value = api::zone::FILE_MOVE_NO_PERMISSIONS_SEND; + result->set(status); return; } if (!regexMatchVector(path, dstContanier.getPermittedToRecv())) { LOGE("Destination zone has no permissions to receive the file: " << path); - result->set(g_variant_new("(s)", api::zone::FILE_MOVE_NO_PERMISSIONS_RECEIVE.c_str())); + status->value = api::zone::FILE_MOVE_NO_PERMISSIONS_RECEIVE; + result->set(status); return; } @@ -654,9 +664,11 @@ void ZonesManager::handleZoneMoveFileRequest(const std::string& srcZoneId, if (!utils::moveFile(srcPath, dstPath)) { LOGE("Failed to move the file: " << path); - result->set(g_variant_new("(s)", api::zone::FILE_MOVE_FAILED.c_str())); + status->value = api::zone::FILE_MOVE_FAILED; + result->set(status); } else { - result->set(g_variant_new("(s)", api::zone::FILE_MOVE_SUCCEEDED.c_str())); + status->value = api::zone::FILE_MOVE_SUCCEEDED; + result->set(status); try { dstContanier.sendNotification(srcZoneId, path, api::zone::FILE_MOVE_SUCCEEDED); } catch (ServerException&) { @@ -681,15 +693,15 @@ void ZonesManager::handleProxyCall(const std::string& caller, targetInterface, targetMethod)) { LOGW("Forbidden proxy call; " << caller << " -> " << target << "; " << targetBusName - << "; " << targetObjectPath << "; " << targetInterface << "; " << targetMethod); + << "; " << targetObjectPath << "; " << targetInterface << "; " << targetMethod); result->setError(api::ERROR_FORBIDDEN, "Proxy call forbidden"); return; } LOGI("Proxy call; " << caller << " -> " << target << "; " << targetBusName - << "; " << targetObjectPath << "; " << targetInterface << "; " << targetMethod); + << "; " << targetObjectPath << "; " << targetInterface << "; " << targetMethod); - auto asyncResultCallback = [result](dbus::AsyncMethodCallResult& asyncMethodCallResult) { + auto asyncResultCallback = [result](dbus::AsyncMethodCallResult & asyncMethodCallResult) { try { GVariant* targetResult = asyncMethodCallResult.get(); result->set(g_variant_new("(v)", targetResult)); @@ -719,26 +731,22 @@ void ZonesManager::handleProxyCall(const std::string& caller, Zone& targetZone = get(targetIter); targetZone.proxyCallAsync(targetBusName, - targetObjectPath, - targetInterface, - targetMethod, - parameters, - asyncResultCallback); + targetObjectPath, + targetInterface, + targetMethod, + parameters, + asyncResultCallback); } -void ZonesManager::handleGetZoneDbuses(dbus::MethodResultBuilder::Pointer result) +void ZonesManager::handleGetZoneDbusesCall(api::MethodResultBuilder::Pointer result) { Lock lock(mMutex); - std::vector entries; + auto dbuses = std::make_shared(); for (auto& zone : mZones) { - GVariant* zoneId = g_variant_new_string(zone->getId().c_str()); - GVariant* dbusAddress = g_variant_new_string(zone->getDbusAddress().c_str()); - GVariant* entry = g_variant_new_dict_entry(zoneId, dbusAddress); - entries.push_back(entry); + dbuses->values.push_back({zone->getId(), zone->getDbusAddress()}); } - GVariant* dict = g_variant_new_array(G_VARIANT_TYPE("{ss}"), entries.data(), entries.size()); - result->set(g_variant_new("(@a{ss})", dict)); + result->set(dbuses); } void ZonesManager::handleDbusStateChanged(const std::string& zoneId, @@ -747,31 +755,28 @@ void ZonesManager::handleDbusStateChanged(const std::string& zoneId, mHostConnection.signalZoneDbusState(zoneId, dbusAddress); } -void ZonesManager::handleGetZoneIdsCall(dbus::MethodResultBuilder::Pointer result) +void ZonesManager::handleGetZoneIdsCall(api::MethodResultBuilder::Pointer result) { Lock lock(mMutex); - std::vector zoneIds; - for(auto& zone: mZones){ - zoneIds.push_back(g_variant_new_string(zone->getId().c_str())); + auto zoneIds = std::make_shared(); + for (const auto& zone : mZones) { + zoneIds->values.push_back(zone->getId()); } - GVariant* array = g_variant_new_array(G_VARIANT_TYPE("s"), - zoneIds.data(), - zoneIds.size()); - result->set(g_variant_new("(@as)", array)); + result->set(zoneIds); } -void ZonesManager::handleGetActiveZoneIdCall(dbus::MethodResultBuilder::Pointer result) +void ZonesManager::handleGetActiveZoneIdCall(api::MethodResultBuilder::Pointer result) { LOGI("GetActiveZoneId call"); - - std::string id = getRunningForegroundZoneId(); - result->set(g_variant_new("(s)", id.c_str())); + auto zoneId = std::make_shared(); + zoneId->value = getRunningForegroundZoneId(); + result->set(zoneId); } void ZonesManager::handleGetZoneInfoCall(const std::string& id, - dbus::MethodResultBuilder::Pointer result) + api::MethodResultBuilder::Pointer result) { LOGI("GetZoneInfo call"); @@ -783,38 +788,33 @@ void ZonesManager::handleGetZoneInfoCall(const std::string& id, result->setError(api::ERROR_INVALID_ID, "No such zone id"); return; } + Zone& zone = get(iter); - const char* state; + auto zoneInfo = std::make_shared(); if (zone.isRunning()) { - state = "RUNNING"; + zoneInfo->state = "RUNNING"; } else if (zone.isStopped()) { - state = "STOPPED"; + zoneInfo->state = "STOPPED"; } else if (zone.isPaused()) { - state = "FROZEN"; + zoneInfo->state = "FROZEN"; } else { LOGE("Unrecognized state of zone id=" << id); result->setError(api::ERROR_INTERNAL, "Unrecognized state of zone"); return; } - result->set(g_variant_new("((siss))", - id.c_str(), - zone.getVT(), - state, - zone.getRootPath().c_str())); + result->set(zoneInfo); } void ZonesManager::handleSetNetdevAttrsCall(const std::string& zone, const std::string& netdev, - const std::vector< - std::tuple>& attrs, - dbus::MethodResultBuilder::Pointer result) + const std::vector>& attrs, + api::MethodResultBuilder::Pointer result) { LOGI("SetNetdevAttrs call"); try { Lock lock(mMutex); - getZone(zone).setNetdevAttrs(netdev, attrs); result->setVoid(); } catch (const InvalidZoneIdException&) { @@ -828,23 +828,18 @@ void ZonesManager::handleSetNetdevAttrsCall(const std::string& zone, void ZonesManager::handleGetNetdevAttrsCall(const std::string& zone, const std::string& netdev, - dbus::MethodResultBuilder::Pointer result) + api::MethodResultBuilder::Pointer result) { LOGI("GetNetdevAttrs call"); try { Lock lock(mMutex); - + auto netDevAttrs = std::make_shared(); const auto attrs = getZone(zone).getNetdevAttrs(netdev); - GVariantBuilder builder; - g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY); - for (const auto entry : attrs) { - g_variant_builder_add(&builder, - "(ss)", - std::get<0>(entry).c_str(), - std::get<1>(entry).c_str()); + for (size_t i = 0; i < attrs.size(); ++i) { + netDevAttrs->values.push_back({std::get<0>(attrs[i]), std::get<1>(attrs[i])}); } - result->set(g_variant_builder_end(&builder)); + result->set(netDevAttrs); } catch (const InvalidZoneIdException&) { LOGE("No zone with id=" << zone); result->setError(api::ERROR_INVALID_ID, "No such zone id"); @@ -855,19 +850,14 @@ void ZonesManager::handleGetNetdevAttrsCall(const std::string& zone, } void ZonesManager::handleGetNetdevListCall(const std::string& zone, - dbus::MethodResultBuilder::Pointer result) + api::MethodResultBuilder::Pointer result) { LOGI("GetNetdevList call"); try { Lock lock(mMutex); - std::vector netdevs; - for(auto& netdev: getZone(zone).getNetdevList()){ - netdevs.push_back(g_variant_new_string(netdev.c_str())); - } - GVariant* array = g_variant_new_array(G_VARIANT_TYPE("s"), - netdevs.data(), - netdevs.size()); - result->set(g_variant_new("(@as)", array)); + auto netDevList = std::make_shared(); + netDevList->values = getZone(zone).getNetdevList(); + result->set(netDevList); } catch (const InvalidZoneIdException&) { LOGE("No zone with id=" << zone); result->setError(api::ERROR_INVALID_ID, "No such zone id"); @@ -880,7 +870,7 @@ void ZonesManager::handleGetNetdevListCall(const std::string& zone, void ZonesManager::handleCreateNetdevVethCall(const std::string& zone, const std::string& zoneDev, const std::string& hostDev, - dbus::MethodResultBuilder::Pointer result) + api::MethodResultBuilder::Pointer result) { LOGI("CreateNetdevVeth call"); try { @@ -901,7 +891,7 @@ void ZonesManager::handleCreateNetdevMacvlanCall(const std::string& zone, const std::string& zoneDev, const std::string& hostDev, const uint32_t& mode, - dbus::MethodResultBuilder::Pointer result) + api::MethodResultBuilder::Pointer result) { LOGI("CreateNetdevMacvlan call"); try { @@ -920,7 +910,7 @@ void ZonesManager::handleCreateNetdevMacvlanCall(const std::string& zone, void ZonesManager::handleCreateNetdevPhysCall(const std::string& zone, const std::string& devId, - dbus::MethodResultBuilder::Pointer result) + api::MethodResultBuilder::Pointer result) { LOGI("CreateNetdevPhys call"); try { @@ -942,15 +932,15 @@ void ZonesManager::handleDeclareFileCall(const std::string& zone, const std::string& path, const int32_t& flags, const int32_t& mode, - dbus::MethodResultBuilder::Pointer result) + api::MethodResultBuilder::Pointer result) { LOGI("DeclareFile call"); try { Lock lock(mMutex); - - const std::string id = getZone(zone).declareFile(type, path, flags, mode); - result->set(g_variant_new("(s)", id.c_str())); + auto declaration = std::make_shared(); + declaration->value = getZone(zone).declareFile(type, path, flags, mode); + result->set(declaration); } catch (const InvalidZoneIdException&) { LOGE("No zone with id=" << zone); result->setError(api::ERROR_INVALID_ID, "No such zone id"); @@ -966,15 +956,15 @@ void ZonesManager::handleDeclareMountCall(const std::string& source, const std::string& type, const uint64_t& flags, const std::string& data, - dbus::MethodResultBuilder::Pointer result) + api::MethodResultBuilder::Pointer result) { LOGI("DeclareMount call"); try { Lock lock(mMutex); - - const std::string id = getZone(zone).declareMount(source, target, type, flags, data); - result->set(g_variant_new("(s)", id.c_str())); + auto declaration = std::make_shared(); + declaration->value = getZone(zone).declareMount(source, target, type, flags, data); + result->set(declaration); } catch (const InvalidZoneIdException&) { LOGE("No zone with id=" << zone); result->setError(api::ERROR_INVALID_ID, "No such zone id"); @@ -987,14 +977,14 @@ void ZonesManager::handleDeclareMountCall(const std::string& source, void ZonesManager::handleDeclareLinkCall(const std::string& source, const std::string& zone, const std::string& target, - dbus::MethodResultBuilder::Pointer result) + api::MethodResultBuilder::Pointer result) { LOGI("DeclareLink call"); try { Lock lock(mMutex); - - const std::string id = getZone(zone).declareLink(source, target); - result->set(g_variant_new("(s)", id.c_str())); + auto declaration = std::make_shared(); + declaration->value = getZone(zone).declareLink(source, target); + result->set(declaration); } catch (const InvalidZoneIdException&) { LOGE("No zone with id=" << zone); result->setError(api::ERROR_INVALID_ID, "No such zone id"); @@ -1005,23 +995,14 @@ void ZonesManager::handleDeclareLinkCall(const std::string& source, } void ZonesManager::handleGetDeclarationsCall(const std::string& zone, - dbus::MethodResultBuilder::Pointer result) + api::MethodResultBuilder::Pointer result) { LOGI("GetDeclarations call Id=" << zone); try { Lock lock(mMutex); - - std::vector declarations = getZone(zone).getDeclarations(); - - std::vector out; - for (auto declaration : declarations) { - out.push_back(g_variant_new_string(declaration.c_str())); - } - - GVariant* array = g_variant_new_array(G_VARIANT_TYPE("s"), - out.data(), - out.size()); - result->set(g_variant_new("(@as)", array)); + auto declarations = std::make_shared(); + declarations->values = getZone(zone).getDeclarations(); + result->set(declarations); } catch (const InvalidZoneIdException&) { LOGE("No zone with id=" << zone); result->setError(api::ERROR_INVALID_ID, "No such zone id"); @@ -1029,12 +1010,11 @@ void ZonesManager::handleGetDeclarationsCall(const std::string& zone, LOGE(ex.what()); result->setError(api::ERROR_INTERNAL, ex.what()); } - } void ZonesManager::handleRemoveDeclarationCall(const std::string& zone, const std::string& declarationId, - dbus::MethodResultBuilder::Pointer result) + api::MethodResultBuilder::Pointer result) { LOGI("RemoveDeclaration call Id=" << zone); try { @@ -1053,20 +1033,20 @@ void ZonesManager::handleRemoveDeclarationCall(const std::string& zone, } void ZonesManager::handleSetActiveZoneCall(const std::string& id, - dbus::MethodResultBuilder::Pointer result) + api::MethodResultBuilder::Pointer result) { LOGI("SetActiveZone call; Id=" << id ); Lock lock(mMutex); auto iter = findZone(id); - if (iter == mZones.end()){ + if (iter == mZones.end()) { LOGE("No zone with id=" << id ); result->setError(api::ERROR_INVALID_ID, "No such zone id"); return; } - if (!get(iter).isRunning()){ + if (!get(iter).isRunning()) { LOGE("Could not activate stopped or paused zone"); result->setError(api::host::ERROR_ZONE_NOT_RUNNING, "Could not activate stopped or paused zone"); @@ -1170,11 +1150,11 @@ void ZonesManager::createZone(const std::string& id, } } - auto removeAllWrapper = [](const std::string& path) -> bool { + auto removeAllWrapper = [](const std::string & path) -> bool { try { LOGD("Removing copied data"); fs::remove_all(fs::path(path)); - } catch(const std::exception& e) { + } catch (const std::exception& e) { LOGW("Failed to remove data: " << boost::diagnostic_information(e)); } return true; @@ -1208,7 +1188,7 @@ void ZonesManager::createZone(const std::string& id, void ZonesManager::handleCreateZoneCall(const std::string& id, const std::string& templateName, - dbus::MethodResultBuilder::Pointer result) + api::MethodResultBuilder::Pointer result) { try { createZone(id, templateName); @@ -1221,7 +1201,7 @@ void ZonesManager::handleCreateZoneCall(const std::string& id, } void ZonesManager::handleDestroyZoneCall(const std::string& id, - dbus::MethodResultBuilder::Pointer result) + api::MethodResultBuilder::Pointer result) { auto destroyer = [id, result, this] { try { @@ -1243,7 +1223,7 @@ void ZonesManager::handleDestroyZoneCall(const std::string& id, } void ZonesManager::handleShutdownZoneCall(const std::string& id, - dbus::MethodResultBuilder::Pointer result) + api::MethodResultBuilder::Pointer result) { LOGI("ShutdownZone call; Id=" << id ); @@ -1272,7 +1252,7 @@ void ZonesManager::handleShutdownZoneCall(const std::string& id, } void ZonesManager::handleStartZoneCall(const std::string& id, - dbus::MethodResultBuilder::Pointer result) + api::MethodResultBuilder::Pointer result) { LOGI("StartZone call; Id=" << id ); @@ -1299,7 +1279,7 @@ void ZonesManager::handleStartZoneCall(const std::string& id, } void ZonesManager::handleLockZoneCall(const std::string& id, - dbus::MethodResultBuilder::Pointer result) + api::MethodResultBuilder::Pointer result) { LOGI("LockZone call; Id=" << id ); @@ -1334,7 +1314,7 @@ void ZonesManager::handleLockZoneCall(const std::string& id, } void ZonesManager::handleUnlockZoneCall(const std::string& id, - dbus::MethodResultBuilder::Pointer result) + api::MethodResultBuilder::Pointer result) { LOGI("UnlockZone call; Id=" << id ); @@ -1369,7 +1349,7 @@ void ZonesManager::handleUnlockZoneCall(const std::string& id, void ZonesManager::handleGrantDeviceCall(const std::string& id, const std::string& device, uint32_t flags, - dbus::MethodResultBuilder::Pointer result) + api::MethodResultBuilder::Pointer result) { LOGI("GrantDevice call; id=" << id << "; dev=" << device); @@ -1409,7 +1389,7 @@ void ZonesManager::handleGrantDeviceCall(const std::string& id, void ZonesManager::handleRevokeDeviceCall(const std::string& id, const std::string& device, - dbus::MethodResultBuilder::Pointer result) + api::MethodResultBuilder::Pointer result) { LOGI("RevokeDevice call; id=" << id << "; dev=" << device); diff --git a/server/zones-manager.hpp b/server/zones-manager.hpp index cb730e3..825b1c8 100644 --- a/server/zones-manager.hpp +++ b/server/zones-manager.hpp @@ -32,6 +32,7 @@ #include "input-monitor.hpp" #include "proxy-call-policy.hpp" #include "utils/worker.hpp" +#include "api/method-result-builder.hpp" #include #include @@ -145,14 +146,15 @@ private: int getVTForNewZone(); void insertZone(const std::string& zoneId, const std::string& templatePath); - void notifyActiveZoneHandler(const std::string& caller, - const std::string& appliaction, - const std::string& message); - void displayOffHandler(const std::string& caller); - void handleZoneMoveFileRequest(const std::string& srcZoneId, - const std::string& dstZoneId, - const std::string& path, - dbus::MethodResultBuilder::Pointer result); + void handleNotifyActiveZoneCall(const std::string& caller, + const std::string& appliaction, + const std::string& message, + api::MethodResultBuilder::Pointer result); + void handleDisplayOffCall(const std::string& caller); + void handleFileMoveCall(const std::string& srcZoneId, + const std::string& dstZoneId, + const std::string& path, + api::MethodResultBuilder::Pointer result); void handleProxyCall(const std::string& caller, const std::string& target, const std::string& targetBusName, @@ -161,76 +163,76 @@ private: const std::string& targetMethod, GVariant* parameters, dbus::MethodResultBuilder::Pointer result); - void handleGetZoneDbuses(dbus::MethodResultBuilder::Pointer result); + void handleGetZoneDbusesCall(api::MethodResultBuilder::Pointer result); void handleDbusStateChanged(const std::string& zoneId, const std::string& dbusAddress); - void handleGetZoneIdsCall(dbus::MethodResultBuilder::Pointer result); - void handleGetActiveZoneIdCall(dbus::MethodResultBuilder::Pointer result); - void handleGetZoneInfoCall(const std::string& id, dbus::MethodResultBuilder::Pointer result); + void handleGetZoneIdsCall(api::MethodResultBuilder::Pointer result); + void handleGetActiveZoneIdCall(api::MethodResultBuilder::Pointer result); + void handleGetZoneInfoCall(const std::string& id, api::MethodResultBuilder::Pointer result); void handleSetNetdevAttrsCall(const std::string& zone, const std::string& netdev, const std::vector>& attrs, - dbus::MethodResultBuilder::Pointer result); + api::MethodResultBuilder::Pointer result); void handleGetNetdevAttrsCall(const std::string& zone, const std::string& netdev, - dbus::MethodResultBuilder::Pointer result); + api::MethodResultBuilder::Pointer result); void handleGetNetdevListCall(const std::string& zone, - dbus::MethodResultBuilder::Pointer result); + api::MethodResultBuilder::Pointer result); void handleCreateNetdevVethCall(const std::string& zone, const std::string& zoneDev, const std::string& hostDev, - dbus::MethodResultBuilder::Pointer result); + api::MethodResultBuilder::Pointer result); void handleCreateNetdevMacvlanCall(const std::string& zone, const std::string& zoneDev, const std::string& hostDev, const uint32_t& mode, - dbus::MethodResultBuilder::Pointer result); + api::MethodResultBuilder::Pointer result); void handleCreateNetdevPhysCall(const std::string& zone, const std::string& devId, - dbus::MethodResultBuilder::Pointer result); + api::MethodResultBuilder::Pointer result); void handleDeclareFileCall(const std::string& zone, const int32_t& type, const std::string& path, const int32_t& flags, const int32_t& mode, - dbus::MethodResultBuilder::Pointer result); + api::MethodResultBuilder::Pointer result); void handleDeclareMountCall(const std::string& source, const std::string& zone, const std::string& target, const std::string& type, const uint64_t& flags, const std::string& data, - dbus::MethodResultBuilder::Pointer result); + api::MethodResultBuilder::Pointer result); void handleDeclareLinkCall(const std::string& source, const std::string& zone, const std::string& target, - dbus::MethodResultBuilder::Pointer result); + api::MethodResultBuilder::Pointer result); void handleGetDeclarationsCall(const std::string& zone, - dbus::MethodResultBuilder::Pointer result); + api::MethodResultBuilder::Pointer result); void handleRemoveDeclarationCall(const std::string& zone, const std::string& declarationId, - dbus::MethodResultBuilder::Pointer result); + api::MethodResultBuilder::Pointer result); void handleSetActiveZoneCall(const std::string& id, - dbus::MethodResultBuilder::Pointer result); + api::MethodResultBuilder::Pointer result); void handleCreateZoneCall(const std::string& id, const std::string& templateName, - dbus::MethodResultBuilder::Pointer result); + api::MethodResultBuilder::Pointer result); void handleDestroyZoneCall(const std::string& id, - dbus::MethodResultBuilder::Pointer result); + api::MethodResultBuilder::Pointer result); void handleShutdownZoneCall(const std::string& id, - dbus::MethodResultBuilder::Pointer result); + api::MethodResultBuilder::Pointer result); void handleStartZoneCall(const std::string& id, - dbus::MethodResultBuilder::Pointer result); + api::MethodResultBuilder::Pointer result); void handleLockZoneCall(const std::string& id, - dbus::MethodResultBuilder::Pointer result); + api::MethodResultBuilder::Pointer result); void handleUnlockZoneCall(const std::string& id, - dbus::MethodResultBuilder::Pointer result); + api::MethodResultBuilder::Pointer result); void handleGrantDeviceCall(const std::string& id, const std::string& device, uint32_t flags, - dbus::MethodResultBuilder::Pointer result); + api::MethodResultBuilder::Pointer result); void handleRevokeDeviceCall(const std::string& id, const std::string& device, - dbus::MethodResultBuilder::Pointer result); + api::MethodResultBuilder::Pointer result); }; diff --git a/tests/unit_tests/server/ut-zone-connection.cpp b/tests/unit_tests/server/ut-zone-connection.cpp index fb6df88..2a72b87 100644 --- a/tests/unit_tests/server/ut-zone-connection.cpp +++ b/tests/unit_tests/server/ut-zone-connection.cpp @@ -41,6 +41,8 @@ #include "utils/fs.hpp" #include "utils/scoped-dir.hpp" +#include "api/method-result-builder.hpp" + using namespace vasum; using namespace vasum::utils; @@ -141,10 +143,11 @@ BOOST_AUTO_TEST_CASE(NotifyActiveZoneApiTest) Latch notifyCalled; ZoneConnection connection(acquireAddress(), nullptr); - auto callback = [&](const std::string& application, const std::string& message) { + auto callback = [&](const std::string& application, const std::string& message, api::MethodResultBuilder::Pointer result) { if (application == "testapp" && message == "testmessage") { notifyCalled.set(); } + result->setVoid(); }; connection.setNotifyActiveZoneCallback(callback); @@ -169,19 +172,19 @@ BOOST_AUTO_TEST_CASE(SignalNotificationApiTest) const std::string& objectPath, const std::string& interface, const std::string& signalName, - GVariant* parameters) { + GVariant* parameters) { if (objectPath == api::zone::OBJECT_PATH && - interface == api::zone::INTERFACE && - signalName == api::zone::SIGNAL_NOTIFICATION && - g_variant_is_of_type(parameters, G_VARIANT_TYPE("(sss)"))) { + interface == api::zone::INTERFACE && + signalName == api::zone::SIGNAL_NOTIFICATION && + g_variant_is_of_type(parameters, G_VARIANT_TYPE("(sss)"))) { const gchar* zone = NULL; const gchar* application = NULL; const gchar* message = NULL; g_variant_get(parameters, "(&s&s&s)", &zone, &application, &message); if (zone == std::string("testzone") && - application == std::string("testapp") && - message == std::string("testmessage")) { + application == std::string("testapp") && + message == std::string("testmessage")) { signalEmitted.set(); } } diff --git a/tests/unit_tests/server/ut-zones-manager.cpp b/tests/unit_tests/server/ut-zones-manager.cpp index 0e40093..ea858e2 100644 --- a/tests/unit_tests/server/ut-zones-manager.cpp +++ b/tests/unit_tests/server/ut-zones-manager.cpp @@ -256,7 +256,7 @@ public: api::host::INTERFACE, api::host::METHOD_GET_ZONE_DBUSES, NULL, - "(a{ss})"); + "(a(ss))"); GVariant* array = NULL; g_variant_get(result.get(), "(*)", &array); dbus::GVariantPtr autounref(array, g_variant_unref); @@ -264,7 +264,7 @@ public: for (size_t n = 0; n < count; ++n) { const char* zoneId = NULL; const char* dbusAddress = NULL; - g_variant_get_child(array, n, "{&s&s}", &zoneId, &dbusAddress); + g_variant_get_child(array, n, "(&s&s)", &zoneId, &dbusAddress); dbuses.insert(Dbuses::value_type(zoneId, dbusAddress)); } return dbuses; -- 2.7.4