From e0972833fb123901e1acbf4129340e626781f5bb Mon Sep 17 00:00:00 2001 From: Alexander Aksenov Date: Tue, 12 Sep 2017 14:33:02 +0300 Subject: [PATCH] Replace protobuf with in-project serialization Change-Id: I81661025ab54deb8e09d7f42f87aa5f8c83dc2be Signed-off-by: Alexander Aksenov --- daemon/Makefile | 32 +++------ daemon/cpp/auxd/auxd_client.cpp | 2 +- daemon/cpp/memd/memd_client.cpp | 2 +- packaging/swap-manager.spec | 1 - src/auxd/CMakeLists.txt | 16 +---- src/auxd/protocol/serialize.cpp | 132 ++++++++++++++++++++++++++++++++++++ src/auxd/protocol/serialize.h | 53 +++++++++++++++ src/auxd/src/main.cpp | 8 +-- src/memd/CMakeLists.txt | 17 +---- src/memd/protocol/serialize.cpp | 107 +++++++++++++++++++++++++++++ src/memd/protocol/serialize.h | 47 +++++++++++++ src/memd/src/main.cpp | 6 +- src/utils/CMakeLists.txt | 7 ++ src/utils/event_loop.h | 6 +- src/utils/serializer/serializer.cpp | 46 +++++++++++++ src/utils/serializer/serializer.h | 10 +++ 16 files changed, 428 insertions(+), 64 deletions(-) create mode 100644 src/auxd/protocol/serialize.cpp create mode 100644 src/auxd/protocol/serialize.h create mode 100644 src/memd/protocol/serialize.cpp create mode 100644 src/memd/protocol/serialize.h create mode 100644 src/utils/serializer/serializer.cpp create mode 100644 src/utils/serializer/serializer.h diff --git a/daemon/Makefile b/daemon/Makefile index 4bb4a86..b01b120 100644 --- a/daemon/Makefile +++ b/daemon/Makefile @@ -127,7 +127,11 @@ SRC_CPP := \ cpp/memd/memd_client_c.cpp \ \ cpp/kernel/module.cpp \ - cpp/kernel/module_c.cpp + cpp/kernel/module_c.cpp \ +\ + ../src/auxd/protocol/serialize.cpp \ + ../src/memd/protocol/serialize.cpp + TARGET = swap_manager @@ -239,23 +243,9 @@ endif # WSI_SUPPORT $(GENERATED_DIR): mkdir -p $(GENERATED_DIR) -# AUXC -AUX_PROTOBUF_DIR = ../src/auxd/protocol -protobuf_generated_auxd: - protoc --cpp_out=$(GENERATED_DIR) --proto_path=$(AUX_PROTOBUF_DIR) $(AUX_PROTOBUF_DIR)/auxd_metadata.proto - -SRC_CC += $(GENERATED_DIR)/auxd_metadata.pb.cc - -# MEMD -MEMD_PROTOBUF_DIR = ../src/memd/protocol -protobuf_generated_memd: - protoc --cpp_out=$(GENERATED_DIR) --proto_path=$(MEMD_PROTOBUF_DIR) $(MEMD_PROTOBUF_DIR)/memd_metadata.proto +# AUXC, MEMD -SRC_CC += $(GENERATED_DIR)/memd_metadata.pb.cc - - -protobuf_generated: protobuf_generated_auxd protobuf_generated_memd -LDFLAGS += -lprotobuf-lite -lswaputils -L../build/lib +LDFLAGS += -lswaputils -L../build/lib headers: $(GENERATED_DIR) $(GENERATED_HEADERS) @@ -268,21 +258,17 @@ release: CPPFLAGS += -DNOLOGI=1 release: debug OBJS_C := $(SRC_C:.c=.o) -OBJS_CC := $(SRC_CC:.cc=.o) OBJS_CPP := $(SRC_CPP:.cpp=.o) -OBJS := $(OBJS_C) $(OBJS_CC) $(OBJS_CPP) +OBJS := $(OBJS_C) $(OBJS_CPP) .c.o: $(CC) $(CFLAGS) -c -o $@ $< -.cc.o: - $(CPP) $(CPPFLAGS) -c -o $@ $< - .cpp.o: $(CPP) $(CPPFLAGS) -c -o $@ $< -debug: rmheaders headers protobuf_generated $(SCRIPTS) $(OBJS) +debug: rmheaders headers $(SCRIPTS) $(OBJS) $(CPP) $(OBJS) -o $(TARGET) $(LDFLAGS) diff --git a/daemon/cpp/auxd/auxd_client.cpp b/daemon/cpp/auxd/auxd_client.cpp index da093e7..b1dda9a 100644 --- a/daemon/cpp/auxd/auxd_client.cpp +++ b/daemon/cpp/auxd/auxd_client.cpp @@ -29,7 +29,7 @@ #include #include -#include +#include template diff --git a/daemon/cpp/memd/memd_client.cpp b/daemon/cpp/memd/memd_client.cpp index d4cd8d8..f549e40 100644 --- a/daemon/cpp/memd/memd_client.cpp +++ b/daemon/cpp/memd/memd_client.cpp @@ -29,7 +29,7 @@ #include #include -#include +#include template diff --git a/packaging/swap-manager.spec b/packaging/swap-manager.spec index 258d77d..49222ea 100644 --- a/packaging/swap-manager.spec +++ b/packaging/swap-manager.spec @@ -55,7 +55,6 @@ BuildRequires: pkgconfig(libtzplatform-config) %if %{SYSTEMD_SUPPORT} BuildRequires: systemd-devel -BuildRequires: protobuf-devel %endif # graphic support diff --git a/src/auxd/CMakeLists.txt b/src/auxd/CMakeLists.txt index 2bfe92e..a8791ec 100644 --- a/src/auxd/CMakeLists.txt +++ b/src/auxd/CMakeLists.txt @@ -3,33 +3,24 @@ cmake_minimum_required(VERSION 2.8) project(auxd) set(PROJECT_NAME swap_auxd) -find_package(Protobuf REQUIRED) - set(CMAKE_CXX_FLAGS "-Wall -Werror -std=c++0x -fpie") set(CMAKE_EXE_LINKER_FLAGS "-pie") -# setup protobuf +# setup protocol set(PROTO_DIR ${CMAKE_CURRENT_SOURCE_DIR}/protocol) -protobuf_generate_cpp( - PROTO_SRCS - PROTO_HDRS - ${PROTO_DIR}/auxd_metadata.proto -) - # headers setup include_directories( ${PROTO_DIR} # protocol headers - ${CMAKE_CURRENT_BINARY_DIR} # generated protobuf headers - ${CMAKE_CURRENT_SOURCE_DIR}/../utils/ # utils headers + ${CMAKE_CURRENT_SOURCE_DIR}/../ # utils headers ) # setup sorces set(SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src) set(SRC - ${PROTO_SRCS} + ${PROTO_DIR}/serialize.cpp ${SRC_DIR}/main.cpp ${SRC_DIR}/shell.cpp ${SRC_DIR}/tizen_app_launch.cpp @@ -42,7 +33,6 @@ link_directories(${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) add_executable(${PROJECT_NAME} ${SRC}) target_link_libraries( ${PROJECT_NAME} - ${PROTOBUF_LITE_LIBRARIES} libswaputils.so libsystemd.so ) diff --git a/src/auxd/protocol/serialize.cpp b/src/auxd/protocol/serialize.cpp new file mode 100644 index 0000000..134b0da --- /dev/null +++ b/src/auxd/protocol/serialize.cpp @@ -0,0 +1,132 @@ +#include +#include +#include "serialize.h" + + + + +bool AUXD::LaunchRequest::ParseFromString(const std::string &in) +{ + // 1 - command + // 2 - app id + + std::size_t pos = 0; + + if (!get_from_string(in, pos, len_size_, command_)) + return false; + + if (!get_from_string(in, pos, len_size_, app_id_)) + return false; + + return true; +} + +bool AUXD::LaunchRequest::SerializeToString(std::string *out) const +{ + std::string res; + + if (!put_to_string(res, len_size_, command_)) + return false; + + if (!put_to_string(res, len_size_, app_id_)) + return false; + + *out = res; + + return true; +} + +std::string AUXD::LaunchRequest::command() const +{ + return command_; +} + +void AUXD::LaunchRequest::set_command(const std::string &command) +{ + command_ = command; +} + +std::string AUXD::LaunchRequest::app_id() const +{ + return app_id_; +} + +void AUXD::LaunchRequest::set_app_id(const std::string &app_id) +{ + app_id_ = app_id; +} + + + + +bool AUXD::ShellOutput::ParseFromString(const std::string &in) +{ + // 1 - error code + // 2 - stdout + // 3 - stderr + + std::string errstr; + std::size_t pos = 0; + + if (!get_from_string(in, pos, len_size_, errstr)) + return false; + + errcode_ = std::stoi(errstr); + + if (!get_from_string(in, pos, len_size_, stdout_)) + return false; + + if (!get_from_string(in, pos, len_size_, stderr_)) + return false; + + return true; +} + +bool AUXD::ShellOutput::SerializeToString(std::string *out) const +{ + std::string res; + std::string errstr = std::to_string(errcode_); + + if (!put_to_string(res, len_size_, errstr)) + return false; + + if (!put_to_string(res, len_size_, stdout_)) + return false; + + if (!put_to_string(res, len_size_, stderr_)) + return false; + + *out = res; + + return true; +} + +int AUXD::ShellOutput::errcode() +{ + return errcode_; +} + +void AUXD::ShellOutput::set_errcode(int errcode) +{ + errcode_ = errcode; +} + +std::string AUXD::ShellOutput::stdout() +{ + return stdout_; +} + +void AUXD::ShellOutput::set_stdout(const std::string &stdout) +{ + stdout_ = stdout; +} + +std::string AUXD::ShellOutput::stderr() +{ + return stderr_; +} + +void AUXD::ShellOutput::set_stderr(const std::string &stderr) +{ + stderr_ = stderr; +} diff --git a/src/auxd/protocol/serialize.h b/src/auxd/protocol/serialize.h new file mode 100644 index 0000000..0fef698 --- /dev/null +++ b/src/auxd/protocol/serialize.h @@ -0,0 +1,53 @@ +#ifndef __SERIALIZE_H__ +#define __SERIALIZE_H__ + +namespace AUXD { + + +class LaunchRequest +{ +public: + bool ParseFromString(const std::string &in); + bool SerializeToString(std::string *out) const; + + std::string command() const; + void set_command(const std::string &command); + + std::string app_id() const; + void set_app_id(const std::string &app_id); + +private: + std::string command_; + std::string app_id_; + + static const int len_size_ = 6; +}; + + +class ShellOutput +{ +public: + bool ParseFromString(const std::string &in); + bool SerializeToString(std::string *out) const; + + int errcode(); + void set_errcode(int errcode); + + std::string stdout(); + void set_stdout(const std::string &out); + + std::string stderr(); + void set_stderr(const std::string &err); + +private: + int errcode_; + std::string stdout_; + std::string stderr_; + + static const unsigned int len_size_ = 6; +}; + + +} // namespace AUXD + +#endif // __SERIALIZE_H__ diff --git a/src/auxd/src/main.cpp b/src/auxd/src/main.cpp index 24b74da..b134fd2 100644 --- a/src/auxd/src/main.cpp +++ b/src/auxd/src/main.cpp @@ -26,11 +26,11 @@ #include #include #include -#include +#include #include "tizen_app_launch.h" #include "shell.h" #include -#include +#include @@ -78,8 +78,8 @@ static EventLoop::HandlerCode aux_tizen_app_launch(void *user_data, try { try_aux_tizen_app_launch(user_data, in, out); return EventLoop::HC_SUCCESS; - } catch (const google::protobuf::FatalException &e) { - throw std::runtime_error(e.what()); + } catch (...) { + throw std::runtime_error("Unknown exception in " + std::string(__func__)); } return EventLoop::HC_ERROR; diff --git a/src/memd/CMakeLists.txt b/src/memd/CMakeLists.txt index 3cf4387..c812b9f 100644 --- a/src/memd/CMakeLists.txt +++ b/src/memd/CMakeLists.txt @@ -3,34 +3,24 @@ cmake_minimum_required(VERSION 2.8) project(memd) set(PROJECT_NAME swap_memd) -find_package(Protobuf REQUIRED) - set(CMAKE_CXX_FLAGS "-Wall -Werror -std=c++0x -fpie") set(CMAKE_EXE_LINKER_FLAGS "-pie") -# setup protobuf +# setup protocol set(PROTO_DIR ${CMAKE_CURRENT_SOURCE_DIR}/protocol) -protobuf_generate_cpp( - PROTO_SRCS - PROTO_HDRS - ${PROTO_DIR}/memd_metadata.proto -) - - # headers setup include_directories( ${PROTO_DIR} # protocol headers - ${CMAKE_CURRENT_BINARY_DIR} # generated protobuf headers - ${CMAKE_CURRENT_SOURCE_DIR}/../utils/ # utils headers + ${CMAKE_CURRENT_SOURCE_DIR}/../ # utils headers ) # setup sorces set(SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src) set(SRC - ${PROTO_SRCS} + ${PROTO_DIR}/serialize.cpp ${SRC_DIR}/main.cpp ) @@ -41,7 +31,6 @@ link_directories(${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) add_executable(${PROJECT_NAME} ${SRC}) target_link_libraries( ${PROJECT_NAME} - ${PROTOBUF_LITE_LIBRARIES} libswaputils.so libsystemd.so ) diff --git a/src/memd/protocol/serialize.cpp b/src/memd/protocol/serialize.cpp new file mode 100644 index 0000000..ea150fe --- /dev/null +++ b/src/memd/protocol/serialize.cpp @@ -0,0 +1,107 @@ +#include +#include +#include +#include "serialize.h" + + + + +bool MEMD::ReadFileRequest::ParseFromString(const std::string &in) +{ + path_ = in; + + return true; +} + +bool MEMD::ReadFileRequest::SerializeToString(std::string *out) const +{ + *out = path_; + + return true; +} + +void MEMD::ReadFileRequest::set_path(const std::string &path) +{ + path_ = path; +} + +std::string MEMD::ReadFileRequest::path() const +{ + return path_; +} + + + + +bool MEMD::ReadFileAnswer::ParseFromString(const std::string &in) +{ + // 1 - success + // 2 - error string + // 3 - data + + std::size_t pos = 0; + std::string sucstr; + + if (!get_from_string(in, pos, len_size_, sucstr)) + return false; + + if (!get_from_string(in, pos, len_size_, error_)) + return false; + + if (!get_from_string(in, pos, len_size_, data_)) + return false; + + std::istringstream(sucstr) >> success_; + + return true; +} + +bool MEMD::ReadFileAnswer::SerializeToString(std::string *out) const +{ + std::string sucstr(success_ ? "1" : "0"); + std::string res; + std::stringstream ss; + + if (!put_to_string(res, len_size_, sucstr)) + return false; + + if (!put_to_string(res, len_size_, error_)) + return false; + + if (!put_to_string(res, len_size_, data_)) + return false; + + *out = res; + + return true; +} + +bool MEMD::ReadFileAnswer::success() const +{ + return success_; +} + +void MEMD::ReadFileAnswer::set_success(bool res) +{ + success_ = res; +} + +std::string MEMD::ReadFileAnswer::error() const +{ + return error_; +} + +void MEMD::ReadFileAnswer::set_error(const std::string &err) +{ + error_ = err; +} + +std::string MEMD::ReadFileAnswer::data() const +{ + return data_; +} + +void MEMD::ReadFileAnswer::set_data(const std::string &data) +{ + data_ = data; +} diff --git a/src/memd/protocol/serialize.h b/src/memd/protocol/serialize.h new file mode 100644 index 0000000..e5e2223 --- /dev/null +++ b/src/memd/protocol/serialize.h @@ -0,0 +1,47 @@ +#ifndef __SERIALIZE_H__ +#define __SERIALIZE_H__ + + +namespace MEMD { + + +class ReadFileRequest +{ +public: + bool ParseFromString(const std::string &in); + bool SerializeToString(std::string *out) const; + + std::string path() const; + void set_path(const std::string &path); + +private: + std::string path_; +}; + + +class ReadFileAnswer +{ +public: + bool ParseFromString(const std::string &in); + bool SerializeToString(std::string *out) const; + + bool success() const; + void set_success(bool res); + + std::string error() const; + void set_error(const std::string &err); + + std::string data() const; + void set_data(const std::string &out); + +private: + bool success_; + std::string error_; + std::string data_; + + static const unsigned int len_size_ = 6; +}; + +} // namespace MEMD + +#endif // __SERIALIZE_H__ diff --git a/src/memd/src/main.cpp b/src/memd/src/main.cpp index d9f060c..77bbbb2 100644 --- a/src/memd/src/main.cpp +++ b/src/memd/src/main.cpp @@ -27,9 +27,9 @@ #include #include #include -#include +#include #include -#include +#include static const int CONNECT_TIMEOUT = 10 * 1000; @@ -88,8 +88,6 @@ static EventLoop::HandlerCode memd_read_file(void *user_data, try { try_memd_read_file(user_data, in, out); return EventLoop::HC_SUCCESS; - } catch (const google::protobuf::FatalException &e) { - throw std::runtime_error(e.what()); } catch (...) { throw std::runtime_error(std::string("Unknown exeption in: ") + __func__); } diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt index 9ac664b..7fd883a 100644 --- a/src/utils/CMakeLists.txt +++ b/src/utils/CMakeLists.txt @@ -11,6 +11,7 @@ set(CMAKE_CXX_FLAGS "-Wall -Werror -std=c++0x -fPIC") # headers set(INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/../ ) include_directories(${INCLUDE_DIR}) @@ -31,10 +32,16 @@ set(SRC_SECURITY ${CMAKE_CURRENT_SOURCE_DIR}/security/privilege.cpp ) +set(SRC_SERIALIZER + ${CMAKE_CURRENT_SOURCE_DIR}/serializer/serializer.cpp +) + + set(SRC ${SRC_UNIX} ${SRC_IPC} ${SRC_SECURITY} + ${SRC_SERIALIZER} socket.cpp fd.cpp event_loop.cpp diff --git a/src/utils/event_loop.h b/src/utils/event_loop.h index dec2e7e..7795f25 100644 --- a/src/utils/event_loop.h +++ b/src/utils/event_loop.h @@ -27,9 +27,9 @@ #include #include -#include -#include -#include +#include +#include +#include class EventLoop diff --git a/src/utils/serializer/serializer.cpp b/src/utils/serializer/serializer.cpp new file mode 100644 index 0000000..f175b44 --- /dev/null +++ b/src/utils/serializer/serializer.cpp @@ -0,0 +1,46 @@ +#include +#include +#include + + + +bool put_to_string(std::string &out, const unsigned int len_size, + const std::string &data) +{ + std::stringstream ss; + std::size_t max_pack_int = 1; + + if (len_size == 0) + return false; + + for (unsigned int i = 0; i < len_size; i++) + max_pack_int *= 10; + + if (data.size() >= max_pack_int) + return false; + + ss << std::setfill('0') << std::setw(len_size) << data.size(); + out.append(ss.str()); + out.append(data); + + return true; +} + +bool get_from_string(const std::string &in, size_t &pos, + const unsigned int len_size, std::string &data) +{ + int len; + + if (in.size() < pos + len_size) + return false; + + len = std::stoi(in.substr(pos, len_size)); + pos += len_size; + if (in.size() < pos + len) + return false; + + data = in.substr(pos, len); + pos += len; + + return true; +} diff --git a/src/utils/serializer/serializer.h b/src/utils/serializer/serializer.h new file mode 100644 index 0000000..92ebbd1 --- /dev/null +++ b/src/utils/serializer/serializer.h @@ -0,0 +1,10 @@ +#ifndef __SERIALIZER_H__ +#define __SERIALIZER_H__ + + +bool put_to_string(std::string &out, const unsigned int len_size, + const std::string &data); +bool get_from_string(const std::string &in, size_t &pos, + const unsigned int len_size, std::string &data); + +#endif // __SERIALIZER_H__ -- 2.7.4