Use Parcel API instead of custom Parcel 06/255106/1
authorHwankyu Jhun <h.jhun@samsung.com>
Fri, 12 Mar 2021 05:37:14 +0000 (14:37 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Fri, 12 Mar 2021 05:37:14 +0000 (14:37 +0900)
Change-Id: I802c7aaee4fbeb476a27745e7e303ab801e52e78
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
CMakeLists.txt
aul/socket/client.cc
aul/socket/packet.cc
aul/socket/packet.hh
aul/socket/parcel.cc [deleted file]
aul/socket/parcel.hh [deleted file]
packaging/aul.spec

index 9513203..284c975 100644 (file)
@@ -43,6 +43,7 @@ PKG_CHECK_MODULES(INIPARSER_DEPS REQUIRED iniparser)
 PKG_CHECK_MODULES(LIBSMACK_DEPS REQUIRED libsmack)
 PKG_CHECK_MODULES(LIBTZPLATFORM_CONFIG_DEPS REQUIRED libtzplatform-config)
 PKG_CHECK_MODULES(LIBXML_DEPS REQUIRED libxml-2.0)
+PKG_CHECK_MODULES(PARCEL_DEPS REQUIRED parcel)
 PKG_CHECK_MODULES(PKGMGR_INFO_DEPS REQUIRED pkgmgr-info)
 PKG_CHECK_MODULES(PKGMGR_INSTALLER_DEPS REQUIRED pkgmgr-installer)
 PKG_CHECK_MODULES(SQLITE3_DEPS REQUIRED sqlite3)
@@ -52,7 +53,6 @@ PKG_CHECK_MODULES(UUID_DEPS REQUIRED uuid)
 PKG_CHECK_MODULES(VCONF_DEPS REQUIRED vconf)
 PKG_CHECK_MODULES(XDGMIME_DEPS REQUIRED xdgmime)
 
-
 ## Target sources
 AUX_SOURCE_DIRECTORY(src SRCS)
 AUX_SOURCE_DIRECTORY(aul AUL_SRCS)
@@ -100,6 +100,7 @@ APPLY_PKG_CONFIG(${TARGET_AUL} PUBLIC
   LIBSMACK_DEPS
   LIBTZPLATFORM_CONFIG_DEPS
   LIBXML_DEPS
+  PARCEL_DEPS
   PKGMGR_INFO_DEPS
   STORAGE_DEPS
   TTRACE_DEPS
index b5cef4c..879dc8d 100644 (file)
@@ -19,7 +19,6 @@
 #include "aul/common/exception.hh"
 #include "aul/common/log_private.hh"
 #include "aul/socket/client.hh"
-#include "aul/socket/parcel.hh"
 
 namespace aul {
 
@@ -49,7 +48,9 @@ Client::Client(std::string path, int timeout_msec) : Socket(std::move(path)) {
 
 int Client::Send(const Packet& packet) {
   _W("cmd(%d)", packet.GetCmd());
-  auto raw = const_cast<Packet&>(packet).GetRaw();
+  tizen_base::Parcel parcel;
+  parcel.WriteParcelable(packet);
+  auto raw = parcel.GetRaw();
   return Socket::Send(reinterpret_cast<void*>(&raw[0]), raw.size());
 }
 
index d312441..4577669 100644 (file)
@@ -17,7 +17,6 @@
 #include <memory>
 
 #include "aul/socket/packet.hh"
-#include "aul/socket/parcel.hh"
 
 namespace aul {
 
@@ -36,12 +35,8 @@ Packet::Packet(int cmd, int opt, tizen_base::Bundle data)
 }
 
 Packet::Packet(const unsigned char* buf, unsigned int size) {
-  Parcel parcel(buf, size);
-  cmd_ = parcel.ReadInt32();
-  int len = parcel.ReadInt32();
-  opt_ = parcel.ReadInt32();
-  auto* p = reinterpret_cast<unsigned char*>(&data_[0]);
-  parcel.Read(p, len);
+  tizen_base::Parcel parcel(buf, size);
+  parcel.ReadParcelable(this);
 }
 
 Packet::~Packet() = default;
@@ -76,15 +71,25 @@ tizen_base::Bundle Packet::DataToBundle() {
   return tizen_base::Bundle(b, false, true);
 }
 
-const std::vector<unsigned char>& Packet::GetRaw() {
-  if (parcel_.GetRaw().size() == 0) {
-    parcel_.WriteInt32(cmd_);
-    parcel_.WriteInt32(data_.size());
-    parcel_.WriteInt32(opt_);
+void Packet::WriteToParcel(tizen_base::Parcel* parcel) const {
+  parcel->WriteInt32(cmd_);
+  parcel->WriteInt32(data_.size());
+  parcel->WriteInt32(opt_);
+  if (data_.size() > 0) {
+    auto* p = reinterpret_cast<const void*>(&data_[0]);
+    parcel->Write(p, data_.size());
+  }
+}
+
+void Packet::ReadFromParcel(tizen_base::Parcel* parcel) {
+  parcel->ReadInt32(&cmd_);
+  int size = 0;
+  parcel->ReadInt32(&size);
+  parcel->ReadInt32(&opt_);
+  if (size > 0) {
     auto* p = reinterpret_cast<unsigned char*>(&data_[0]);
-    parcel_.Write(p, data_.size());
+    parcel->Read(p, size);
   }
-  return parcel_.GetRaw();
 }
 
 }  // namespace aul
index 0cc3245..7b61e0d 100644 (file)
 #define AUL_SOCKET_PACKET_HH_
 
 #include <bundle_cpp.h>
+#include <parcel.hh>
+#include <parcelable.hh>
 
 #include <vector>
 
-#include "aul/socket/parcel.hh"
-
 namespace aul {
 
-class Packet {
+class Packet : public tizen_base::Parcelable {
  public:
   Packet();
   Packet(int cmd, int opt, std::vector<unsigned char> data);
@@ -43,13 +43,13 @@ class Packet {
 
   tizen_base::Bundle DataToBundle();
 
-  const std::vector<unsigned char>& GetRaw();
+  void WriteToParcel(tizen_base::Parcel* parcel) const override;
+  void ReadFromParcel(tizen_base::Parcel* parcel) override;
 
  private:
   int cmd_;
   int opt_;
   std::vector<unsigned char> data_;
-  Parcel parcel_;
 };
 
 }  // namespace aul
diff --git a/aul/socket/parcel.cc b/aul/socket/parcel.cc
deleted file mode 100644 (file)
index a860339..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * 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.
- */
-
-#include "aul/socket/parcel.hh"
-
-namespace aul {
-
-Parcel::Parcel(const unsigned char* buf, unsigned int size)
-  : data_(buf, buf + size) {
-}
-
-void Parcel::Write(const unsigned char* buf, unsigned int size) {
-  std::copy(buf, buf + size, std::back_inserter(data_));
-}
-
-void Parcel::Read(unsigned char* buf, unsigned int size) {
-  if (reader_ + size > data_.size())
-    return;
-
-  std::copy(&data_[reader_], &data_[reader_] + size, buf);
-  reader_ += size;
-}
-
-void Parcel::WriteInt32(int i) {
-  Write<int>(i);
-}
-
-int Parcel::ReadInt32() {
-  return Read<int>();
-}
-
-const std::vector<unsigned char>& Parcel::GetRaw() {
-  return data_;
-}
-
-}  // namespace aul
diff --git a/aul/socket/parcel.hh b/aul/socket/parcel.hh
deleted file mode 100644 (file)
index 3358353..0000000
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * 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.
- */
-
-#ifndef AUL_SOCKET_PARCEL_HH_
-#define AUL_SOCKET_PARCEL_HH_
-
-#include <vector>
-
-namespace aul {
-
-class Parcel {
- public:
-  Parcel() = default;
-  Parcel(const unsigned char* buf, unsigned int size);
-
-  void Write(const unsigned char* buf, unsigned int size);
-  void Read(unsigned char* buf, unsigned int size);
-  void WriteInt32(int i);
-  int ReadInt32();
-  const std::vector<unsigned char>& GetRaw();
-
- private:
-  template<typename T>
-  void Write(T d) {
-    unsigned char* p = reinterpret_cast<unsigned char*>(&d);
-    std::copy(p, p + sizeof(T), std::back_inserter(data_));
-  }
-
-  template<typename T>
-  T Read() {
-    T d = 0;
-    unsigned char* p = reinterpret_cast<unsigned char*>(&d);
-    if (reader_ + sizeof(T) > data_.size())
-      return d;
-
-    std::copy(&data_[reader_], &data_[reader_] + sizeof(T), p);
-    reader_ += sizeof(T);
-    return d;
-  }
-
- private:
-  std::vector<unsigned char> data_;
-  unsigned int reader_ = 0;
-};
-
-}  // namespace aul
-
-#endif  // AUL_SOCKET_PARCEL_HH_
index 03762c0..55e3f90 100644 (file)
@@ -35,6 +35,7 @@ BuildRequires:  pkgconfig(libxml-2.0)
 BuildRequires:  pkgconfig(uuid)
 BuildRequires:  pkgconfig(libsmack)
 BuildRequires:  pkgconfig(gmock)
+BuildRequires:  pkgconfig(parcel)
 
 %if 0%{?gcov:1}
 BuildRequires:  lcov