From 9cd7290dfb3a15cc5115e1cdd8152d49128d13fd Mon Sep 17 00:00:00 2001 From: Hyunho Date: Wed, 23 Jun 2021 15:58:00 +0900 Subject: [PATCH] Remove duplication Change-Id: I93921fbfde6c0a6fac549c2e636d21eed6ff78c5 Signed-off-by: Hyunho --- cion/channel/client_channel.cc | 177 ++++------------- cion/channel/client_channel.hh | 8 +- cion/channel/client_channel_implementation.hh | 2 + cion/channel/ipayload_receiver.hh | 48 +++++ cion/channel/payload_manager.cc | 183 ++++++++++++++++++ cion/channel/payload_manager.hh | 70 +++++++ cion/channel/server_channel.cc | 145 ++------------ cion/channel/server_channel.hh | 6 +- cion/channel/server_channel_implementation.hh | 5 +- cion/tizen-api/cion_client.cc | 19 +- cion/tizen-api/cion_client.h | 1 + cion/tizen-api/cion_payload.h | 10 + cion/tizen-api/cion_server.cc | 10 +- cion/tizen-api/cion_server.h | 1 + 14 files changed, 392 insertions(+), 293 deletions(-) create mode 100644 cion/channel/ipayload_receiver.hh create mode 100644 cion/channel/payload_manager.cc create mode 100644 cion/channel/payload_manager.hh diff --git a/cion/channel/client_channel.cc b/cion/channel/client_channel.cc index 7c680cb..b8873cd 100644 --- a/cion/channel/client_channel.cc +++ b/cion/channel/client_channel.cc @@ -49,22 +49,6 @@ * ClientChannel implementation */ -namespace { - -bool IsExist(const std::string &path) -{ - int fd = open(path.c_str(), O_RDONLY); - if (fd < 0) - return false; - struct stat sb; - bool result = (fstat(fd, &sb) == 0); - close(fd); - return result; -} - -} - - namespace cion { namespace channel { @@ -200,38 +184,44 @@ void ClientChannel::Impl::OnTerminated(VineDpPtr dp) { }, parent_, nullptr); } -void ClientChannel::Impl::OnReceived( - VineDpPtr dp, std::vector data) { - LOG(INFO) << "OnReceived " << data.size(); +void ClientChannel::Impl::TrySyncReply(std::vector data) { tizen_base::Parcel pl(reinterpret_cast(data.data()), data.size()); unsigned int cmd; pl.ReadUInt32(&cmd); - if (cmd == cmd::DataSyncReply) { - uint32_t size; - pl.ReadUInt32(&size); - unsigned int sequence_id; - pl.ReadUInt32(&sequence_id); - LOG(INFO) << "OnReceived datasync reply seq id : " << sequence_id; - - if (is_timeover_) { - LOG(INFO) << "OnReceived data sync reply timeover seq id : " << sequence_id; - return; - } + if (cmd != cmd::DataSyncReply) + return; - if (sequence_id == sequence_id_) { - unsigned char* payload_data = - (unsigned char*)calloc(size, sizeof(unsigned char)); - pl.Read(payload_data, size); - reply_queue_.Push( - std::vector(payload_data, payload_data + size)); - free(payload_data); - } else { - LOG(INFO) << "OnReceived data sync sequence id not matched"; - } + uint32_t size; + pl.ReadUInt32(&size); + unsigned int sequence_id; + pl.ReadUInt32(&sequence_id); + LOG(INFO) << "OnReceived datasync reply seq id : " << sequence_id; + + if (is_timeover_) { + LOG(INFO) << "OnReceived data sync reply timeover seq id : " << sequence_id; return; } + if (sequence_id == sequence_id_) { + unsigned char* payload_data = + (unsigned char*)calloc(size, sizeof(unsigned char)); + pl.Read(payload_data, size); + reply_queue_.Push( + std::vector(payload_data, payload_data + size)); + free(payload_data); + } else { + LOG(INFO) << "OnReceived data sync sequence id not matched"; + } +} + +void ClientChannel::Impl::OnReceived( + VineDpPtr dp, std::vector data) { + LOG(INFO) << "OnReceived " << data.size(); + + // Reply to the sender if a reply is needed + TrySyncReply(data); + ChannelJob job(parent_, std::make_shared( reinterpret_cast(data.data()), data.size()), dp); @@ -312,110 +302,9 @@ void ClientChannel::Impl::OnReceived( } case cmd::PayloadAsync: { - LOG(INFO) << "client PayloadReceived from server"; - uint32_t size = 0; - parcel->ReadUInt32(&size); - - char* payload_data = new char[size]; - parcel->Read(payload_data, size); - - auto payload = FactoryManager::GetInst().CreatePayload( - std::vector(payload_data, payload_data + size)); - delete[] payload_data; - - if (payload->GetType() == IPayload::PayloadType::File) { - - LOG(INFO) << "client PayloadReceived from client file"; - - FilePayloadReaderInternal filepayload_reader(payload.get()); - - std::shared_ptr fp = - std::dynamic_pointer_cast(payload); - - size = 0; - parcel->ReadUInt32(&size); - if (size == 0) { - LOG(ERROR) << "client receive fail : " << - filepayload_reader.GetFilePath() << " : " << - filepayload_reader.GetTemporaryFileName(); - break; - } - - payload_data = new char[size]; - parcel->Read(payload_data, size); - uint32_t cnt = 0; - parcel->ReadUInt32(&cnt); - LOG(INFO) << "client receive number " << cnt; - LOG(INFO) << "client receive " << fp->GetTotalBytes() << - ", path " << filepayload_reader.GetFilePath() << - " : " << filepayload_reader.GetTemporaryFileName(); - - /* temp dir */ - const char *temp_dir = aul_get_app_data_path(); - - std::string cion_dir = std::string(temp_dir) + ".cion/.temp/"; - - int ret = -2; - if(!IsExist(cion_dir)) - ret = mkdir(cion_dir.c_str(), 0777); - - LOG(INFO) << "client receive : " << temp_dir; - LOG(INFO) << "client receive : " << cion_dir << ", mk : " << ret; - - /* file write */ - std::ofstream file_; - int full_size = fp->GetTotalBytes(); - std::string temp_path; - - filepayload_reader.SetTemporaryFilePath(cion_dir); - - temp_path = cion_dir + filepayload_reader.GetTemporaryFileName(); - LOG(INFO) << "client receive temp file path " << temp_path; - LOG(INFO) << "client receive full size " << full_size << ", received size " << size; - - file_.open(temp_path, std::ofstream::app | std::ofstream::binary); - - bool is_open = file_.is_open(); - uint64_t file_size = 0; - - if (is_open) { - LOG(INFO) << "client receive file write open success"; - - file_.write(payload_data, size); - file_size = file_.tellp(); - file_.close(); - } else { - LOG(INFO) << "client receive open fail"; - } - delete[] payload_data; - - if (is_open) { - filepayload_reader.SetReceivedBytes(file_size); - channel->OnPayloadReceived(payload); - } else { - //error callback - } - - if (file_size == fp->GetTotalBytes()) { - std::remove(temp_path.c_str()); - LOG(INFO) << " delete file " << temp_path << " " << file_size << - " / " << fp->GetTotalBytes(); - AsyncSender as; - as.SendResult(job.GetClientDp(), - channel->impl_->my_peer_info_->GetUUID(), - channel->impl_->my_peer_info_->GetChannelID(), - payload->GetID(), - PayloadAsyncResult::ResultCode::Success); - } - } else { - channel->OnPayloadReceived(payload); - AsyncSender as; - as.SendResult(job.GetClientDp(), - channel->impl_->my_peer_info_->GetUUID(), - channel->impl_->my_peer_info_->GetChannelID(), - payload->GetID(), - PayloadAsyncResult::ResultCode::Success); - } + PayloadManager pm(channel); + pm.Process(parcel, job.GetClientDp(), + channel->impl_->my_peer_info_, channel->impl_->connected_peer_); break; } } diff --git a/cion/channel/client_channel.hh b/cion/channel/client_channel.hh index 3abdd4b..33d2c22 100644 --- a/cion/channel/client_channel.hh +++ b/cion/channel/client_channel.hh @@ -26,13 +26,13 @@ #include "cion/common/ipayload.hh" #include "cion/channel/connection_result.hh" #include "cion/channel/payload_async_result.hh" +#include "cion/channel/ipayload_receiver.hh" namespace cion { namespace channel { -class ClientChannel { +class ClientChannel : public IPayloadReceiver { public: - explicit ClientChannel(const std::string& service_name); explicit ClientChannel(const std::string& service_name, SecurityInfo security); @@ -48,7 +48,9 @@ class ClientChannel { std::string GetSerivceName(); protected: - virtual void OnPayloadReceived(std::shared_ptr data) = 0; + virtual void OnPayloadReceived(std::shared_ptr data, + std::shared_ptr peer, + PayloadTransferStatus status) = 0; virtual void OnConnectionResult(std::shared_ptr info, const ConnectionResult& result) = 0; virtual void OnDiscovered(std::shared_ptr peer) = 0; diff --git a/cion/channel/client_channel_implementation.hh b/cion/channel/client_channel_implementation.hh index ae0cbdd..d16ed18 100644 --- a/cion/channel/client_channel_implementation.hh +++ b/cion/channel/client_channel_implementation.hh @@ -31,6 +31,7 @@ #include "cion/common/cion_cmd.hh" #include "cion/security/security_info.hh" #include "cion/channel/payload_async_result_manager.hh" +#include "cion/channel/payload_manager.hh" #include "cion/vine/vine_session.hh" #include "cion/vine/vine_discoverer.hh" @@ -50,6 +51,7 @@ class ClientChannel::Impl : public IVineBrowser, IVineDpOpenedEventHandler, void Disconnect(VineDpPtr dp); std::shared_ptr GetDp(std::string ip, int port); std::shared_ptr GetDp(std::shared_ptr dp); + void TrySyncReply(std::vector data); void OnDiscovered(VineService service, std::string ip) override; void OnOpened(VineDpPtr dp, int result) override; diff --git a/cion/channel/ipayload_receiver.hh b/cion/channel/ipayload_receiver.hh new file mode 100644 index 0000000..1c10f86 --- /dev/null +++ b/cion/channel/ipayload_receiver.hh @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd. + * + * 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 CION_CHANNEL_IPAYLOAD_RECEIVER_HH_ +#define CION_CHANNEL_IPAYLOAD_RECEIVER_HH_ + +#include +#include +#include + +#include "cion/common/ipayload.hh" +#include "cion/common/peer_info.hh" + +#ifndef EXPORT_API +#define EXPORT_API __attribute__((visibility("default"))) +#endif + +namespace cion { + +class IPayloadReceiver { + public: + enum class PayloadTransferStatus { + Success, + Failure, + InProgress + }; + virtual ~IPayloadReceiver() = default; + virtual void OnPayloadReceived(std::shared_ptr data, + std::shared_ptr peer, + PayloadTransferStatus status) = 0; +}; + +} // namespace cion + +#endif // CION_CHANNEL_IPAYLOAD_RECEIVER_HH_ diff --git a/cion/channel/payload_manager.cc b/cion/channel/payload_manager.cc new file mode 100644 index 0000000..bd4046b --- /dev/null +++ b/cion/channel/payload_manager.cc @@ -0,0 +1,183 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd. + * + * 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 +#include + +#include "cion/channel/payload_manager.hh" +#include "cion/channel/async_sender.hh" +#include "cion/common/util/logging.hh" +#include "cion/common/util/factory_manager.hh" + +namespace cion { + +PayloadManager::~PayloadManager() { + LOG(WARNING) << "destroy Payload manager "; +} + +void PayloadManager::Process(std::shared_ptr parcel, + std::shared_ptr peer_dp, + std::shared_ptr my_peer_info, + std::shared_ptr sender_peer_info) { + uint32_t size = 0; + parcel->ReadUInt32(&size); + + auto payload_data = std::unique_ptr( + static_cast(calloc(sizeof(char), size)), std::free); + parcel->Read(payload_data.get(), size); + + auto payload = FactoryManager::GetInst().CreatePayload( + std::vector(payload_data.get(), payload_data.get() + size)); + if (payload->GetType() == IPayload::PayloadType::File) + ProcessFile(payload, peer_dp, my_peer_info, sender_peer_info, parcel); + else if (payload->GetType() == IPayload::PayloadType::Data) + ProcessData(payload, peer_dp, my_peer_info, sender_peer_info); +} + +void PayloadManager::SendResult(std::shared_ptr dp, + std::shared_ptr peer, + std::string payload_id, + PayloadAsyncResult::ResultCode result) { + AsyncSender as; + as.SendResult(dp, peer->GetUUID(), + peer->GetChannelID(), + payload_id, + result); +} + +void PayloadManager::ProcessData(std::shared_ptr payload, + std::shared_ptr peer_dp, + std::shared_ptr my_peer_info, + std::shared_ptr sender_peer_info) { + receiver_->OnPayloadReceived(payload, sender_peer_info, + IPayloadReceiver::PayloadTransferStatus::Success); + SendResult(peer_dp, my_peer_info, payload->GetID(), + PayloadAsyncResult::ResultCode::Success); +} + +bool PayloadManager::IsExist(const std::string &path) { + int fd = open(path.c_str(), O_RDONLY); + if (fd < 0) + return false; + struct stat sb; + bool result = (fstat(fd, &sb) == 0); + close(fd); + return result; +} + +void PayloadManager::ResultHanling(std::shared_ptr payload, + std::shared_ptr peer_dp, + std::shared_ptr my_peer_info, + std::shared_ptr sender_peer_info, + IPayloadReceiver::PayloadTransferStatus status) { + + receiver_->OnPayloadReceived(payload, sender_peer_info, status); + if (status == IPayloadReceiver::PayloadTransferStatus::Success) { + SendResult(peer_dp, my_peer_info, payload->GetID(), + PayloadAsyncResult::ResultCode::Success); + } else if (status == IPayloadReceiver::PayloadTransferStatus::Failure) { + LOG(ERROR) << "Payload transfer fail !! " << payload->GetID(); + SendResult(peer_dp, my_peer_info, payload->GetID(), + PayloadAsyncResult::ResultCode::Fail); + } +} + +void PayloadManager::ProcessFile(std::shared_ptr payload, + std::shared_ptr peer_dp, + std::shared_ptr my_peer_info, + std::shared_ptr sender_peer_info, + std::shared_ptr parcel) { + LOG(INFO) << "client PayloadReceived from client file"; + FilePayloadReaderInternal filepayload_reader(payload.get()); + std::shared_ptr fp = + std::dynamic_pointer_cast(payload); + + uint32_t size = 0; + parcel->ReadUInt32(&size); + if (size == 0) { + LOG(ERROR) << "client read fail : size 0"; + return; + } + + auto payload_data = std::unique_ptr( + static_cast(calloc(sizeof(char), size)), std::free); + parcel->Read(payload_data.get(), size); + + uint32_t cnt = 0; + parcel->ReadUInt32(&cnt); + LOG(INFO) << "file part number : " << cnt; + + const char *temp_dir = aul_get_app_data_path(); + std::string cion_dir = std::string(temp_dir) + ".cion/.temp/"; + if (!IsExist(cion_dir)) { + if (mkdir(cion_dir.c_str(), 0777) < 0) { + LOG(ERROR) << "Failed to create dir " + << cion_dir << "errno(" << errno << ")"; + ResultHanling(payload, peer_dp, my_peer_info, sender_peer_info, + IPayloadReceiver::PayloadTransferStatus::Failure); + return; + } + } + + filepayload_reader.SetTemporaryFilePath(cion_dir); + std::string temp_path = cion_dir + filepayload_reader.GetTemporaryFileName(); + LOG(INFO) << "temp file path : " << temp_path + << "/ file path : " << filepayload_reader.GetFilePath(); + LOG(INFO) << "received (" << fp->GetTotalBytes() << " / " << size << ")"; + + // When the temp file was already deleted because of any file operation error + if (cnt > 1 && !IsExist(temp_path)) { + LOG(ERROR) << "REMOVED FILE (" << temp_path << ")"; + return; + } + + std::ofstream ofile; + ofile.open(temp_path, std::ofstream::app | std::ofstream::binary); + uint64_t file_size = 0; + if (ofile.is_open()) { + LOG(INFO) << "client receive file write open success"; + ofile.write(payload_data.get(), size); + file_size = ofile.tellp(); + ofile.close(); + if (ofile.fail()) { /* In case write buffer fail */ + LOG(ERROR) << "fail to write file!! " << ofile.bad(); + std::remove(temp_path.c_str()); + ResultHanling(payload, peer_dp, my_peer_info, sender_peer_info, + IPayloadReceiver::PayloadTransferStatus::Failure); + return; + } + + filepayload_reader.SetReceivedBytes(file_size); + ResultHanling(payload, peer_dp, my_peer_info, sender_peer_info, + IPayloadReceiver::PayloadTransferStatus::InProgress); + } else { + LOG(ERROR) << "client receive open fail"; + ResultHanling(payload, peer_dp, my_peer_info, sender_peer_info, + IPayloadReceiver::PayloadTransferStatus::Failure); + return; + } + + if (file_size == fp->GetTotalBytes()) { + ResultHanling(payload, peer_dp, my_peer_info, sender_peer_info, + IPayloadReceiver::PayloadTransferStatus::Success); + + std::remove(temp_path.c_str()); + LOG(INFO) << " delete file " << temp_path << " " << file_size + << " / " << fp->GetTotalBytes(); + } +} + +} // namespace cion diff --git a/cion/channel/payload_manager.hh b/cion/channel/payload_manager.hh new file mode 100644 index 0000000..b21f44f --- /dev/null +++ b/cion/channel/payload_manager.hh @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd. + * + * 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 CION_CHANNEL_PAYLOAD_MANAGER_HH_ +#define CION_CHANNEL_PAYLOAD_MANAGER_HH_ + +#include +#include +#include +#include + +#include "cion/channel/ipayload_receiver.hh" +#include "cion/channel/payload_async_result.hh" +#include "cion/common/peer_info.hh" +#include "cion/common/ipayload.hh" +#include "cion/vine/vine_dp.hh" + +namespace cion { + +class PayloadManager { + public: + explicit PayloadManager(IPayloadReceiver* receiver) : receiver_(receiver) {} + virtual ~PayloadManager(); + + void Process(std::shared_ptr parcel, + std::shared_ptr peer_dp, + std::shared_ptr my_peer_info, + std::shared_ptr sender_peer_info); + void ProcessFile(std::shared_ptr payload, + std::shared_ptr peer_dp, + std::shared_ptr my_peer_info, + std::shared_ptr sender_peer_info, + std::shared_ptr parcel); + void ProcessData(std::shared_ptr payload, + std::shared_ptr peer_dp, + std::shared_ptr my_peer_info, + std::shared_ptr sender_peer_info); + bool IsExist(const std::string &path); + + private: + void SendResult(std::shared_ptr dp, + std::shared_ptr peer, + std::string payload_id, + PayloadAsyncResult::ResultCode result); + void ResultHanling(std::shared_ptr payload, + std::shared_ptr peer_dp, + std::shared_ptr my_peer_info, + std::shared_ptr sender_peer_info, + IPayloadReceiver::PayloadTransferStatus status); + + private: + IPayloadReceiver* receiver_; +}; + +} // namespace cion + +#endif // CION_CHANNEL_PAYLOAD_MANAGER_HH_ diff --git a/cion/channel/server_channel.cc b/cion/channel/server_channel.cc index 6686a6a..46e419e 100644 --- a/cion/channel/server_channel.cc +++ b/cion/channel/server_channel.cc @@ -15,18 +15,18 @@ * limitations under the License. */ -#include -#include -#include - #include #include #include -#include -#include #include #include +#include +#include +#include +#include +#include + #include "cion/channel/server_channel.hh" #include "cion/channel/server_peer_info.hh" #include "cion/channel/connection_result.hh" @@ -57,17 +57,6 @@ constexpr int kVineSvcAttrMaxLen = 200; constexpr char kVineSvcAttrKey1[] = "svcpart1"; constexpr char kVineSvcAttrKey2[] = "svcpart2"; -bool IsExist(const std::string &path) -{ - int fd = open(path.c_str(), O_RDONLY); - if (fd < 0) - return false; - struct stat sb; - bool result = (fstat(fd, &sb) == 0); - close(fd); - return result; -} - gboolean IdleCallback(gpointer data) { auto callback = static_cast*>(data); (*callback)(); @@ -154,90 +143,6 @@ void ServerChannel::Reject( }), nullptr); } -void ServerChannel::Impl::ReceiveFile(std::shared_ptr payload, - std::shared_ptr parcel, - std::shared_ptr peer, std::shared_ptr dp) { - LOG(INFO) << "server PayloadReceived from client file"; - FilePayloadReaderInternal filepayload_reader(payload.get()); - - std::shared_ptr fp = - std::dynamic_pointer_cast(payload); - - uint32_t size = 0; - parcel->ReadUInt32(&size); - if (size == 0) { - LOG(ERROR) << "sever receive fail : " << - filepayload_reader.GetFilePath() << " : " << - filepayload_reader.GetTemporaryFileName(); - return; - } - - char* payload_data = new char[size]; - parcel->Read(payload_data, size); - uint32_t cnt = 0; - parcel->ReadUInt32(&cnt); - LOG(INFO) << "sever receive number " << cnt; - LOG(INFO) << "sever receive " << fp->GetTotalBytes() - << ", path " << filepayload_reader.GetFilePath() - << " : " << filepayload_reader.GetTemporaryFileName(); - - /* temp dir */ - const char *temp_dir = aul_get_app_data_path(); - std::string cion_dir = std::string(temp_dir) + ".cion/.temp/"; - int ret = -2; - if (!IsExist(cion_dir)) - ret = mkdir(cion_dir.c_str(), 0777); - - LOG(INFO) << "sever receive : " << temp_dir; - LOG(INFO) << "sever receive : " << cion_dir << ", mk : " << ret; - - /* file write */ - std::ofstream file; - int full_size = fp->GetTotalBytes(); - std::string temp_path; - - filepayload_reader.SetTemporaryFilePath(cion_dir); - - temp_path = cion_dir + filepayload_reader.GetTemporaryFileName(); - LOG(INFO) << "sever receive temp file path " << temp_path; - LOG(INFO) << "sever receive full size " << full_size - << ", received size " << size; - - file.open(temp_path, std::ofstream::app | std::ofstream::binary); - bool is_open = file.is_open(); - uint64_t file_size = 0; - - if (is_open) { - LOG(INFO) << "sever receive file write open success"; - - file.write(payload_data, size); - file_size = file.tellp(); - file.close(); - } else { - LOG(INFO) << "sever receive open fail"; - } - delete[] payload_data; - - if (is_open) { - filepayload_reader.SetReceivedBytes( - static_cast(file_size)); - parent_->OnPayloadReceived(payload, peer); - } else { - parent_->OnErrorReported(cion::error::CION_ERROR_INVALID_OPERATION, peer); - } - - if (file_size == fp->GetTotalBytes()) { - std::remove(temp_path.c_str()); - LOG(INFO) << " delete file " << temp_path << " " << - file_size << " / " << fp->GetTotalBytes(); - AsyncSender as; - as.SendResult(dp, my_peer_info_->GetUUID(), my_peer_info_->GetChannelID(), - payload->GetID(), - PayloadAsyncResult::ResultCode::Success); - } -} - - void ServerChannel::Impl::OnReceived( VineDpPtr dp, std::vector data) { LOG(INFO) << "server OnReceived " << dp->GetRawDp(); @@ -297,38 +202,18 @@ void ServerChannel::Impl::OnReceived( case cmd::CionCmd::PayloadAsync: { LOG(INFO) << "server PayloadReceived from client"; + std::shared_ptr speer = nullptr; for (std::shared_ptr peer : channel->impl_->peerlist_) { - std::shared_ptr speer = - std::dynamic_pointer_cast(peer); - if (*speer->GetClientDp() == *job.GetClientDp()) { - size = 0; - parcel->ReadUInt32(&size); - if (size == 0) { - LOG(ERROR) << "sever receive fail : size 0"; - break; - } - char* payload_data = new char[size]; - parcel->Read(payload_data, size); - - auto payload = FactoryManager::GetInst().CreatePayload( - std::vector(payload_data, payload_data + size)); - delete[] payload_data; - - if (payload->GetType() == IPayload::PayloadType::File) { - channel->impl_->ReceiveFile(payload, parcel, peer, - speer->GetClientDp()); - } else { - channel->OnPayloadReceived(payload, peer); - AsyncSender as; - as.SendResult(speer->GetClientDp(), - channel->impl_->my_peer_info_->GetUUID(), - channel->impl_->my_peer_info_->GetChannelID(), - payload->GetID(), - PayloadAsyncResult::ResultCode::Success); - } + speer = std::dynamic_pointer_cast(peer); + if (*speer->GetClientDp() == *job.GetClientDp()) break; - } } + if (speer == nullptr) + break; + + PayloadManager pm(channel); + pm.Process(parcel, job.GetClientDp(), + channel->impl_->my_peer_info_, speer); break; } diff --git a/cion/channel/server_channel.hh b/cion/channel/server_channel.hh index 4337ba8..be272cb 100644 --- a/cion/channel/server_channel.hh +++ b/cion/channel/server_channel.hh @@ -29,11 +29,12 @@ #include "cion/common/ipayload.hh" #include "cion/channel/server_peer_info.hh" #include "cion/channel/payload_async_result.hh" +#include "cion/channel/ipayload_receiver.hh" namespace cion { namespace channel { -class ServerChannel { +class ServerChannel : public IPayloadReceiver { public: explicit ServerChannel(std::string service_name); explicit ServerChannel(std::string service_name, SecurityInfo security); @@ -54,7 +55,8 @@ class ServerChannel { virtual std::vector OnDataReceived(const std::vector& data, std::shared_ptr peer) = 0; virtual void OnPayloadReceived(std::shared_ptr data, - std::shared_ptr peer) = 0; + std::shared_ptr peer, + PayloadTransferStatus status) = 0; virtual void OnConnectionRequest(std::shared_ptr peer) = 0; virtual void OnConnectionResult(std::shared_ptr info, const ConnectionResult& result) = 0; diff --git a/cion/channel/server_channel_implementation.hh b/cion/channel/server_channel_implementation.hh index 4652f5d..4aa3685 100644 --- a/cion/channel/server_channel_implementation.hh +++ b/cion/channel/server_channel_implementation.hh @@ -33,6 +33,7 @@ #include "cion/vine/vine_service.hh" #include "cion/vine/vine_interfaces.hh" #include "cion/channel/payload_async_result_manager.hh" +#include "cion/channel/payload_manager.hh" namespace cion { namespace channel { @@ -56,10 +57,6 @@ class ServerChannel::Impl : public IVineDpOpenedEventHandler, std::shared_ptr>>, IPayload* data); void Send(cmd::CionCmd cmd, VineDpPtr client_dp, std::vector raw); - void ReceiveFile(std::shared_ptr payload, - std::shared_ptr parcel, - std::shared_ptr peer, - std::shared_ptr dp); private: friend class ServerChannel; diff --git a/cion/tizen-api/cion_client.cc b/cion/tizen-api/cion_client.cc index fa02827..8322eec 100644 --- a/cion/tizen-api/cion_client.cc +++ b/cion/tizen-api/cion_client.cc @@ -36,7 +36,8 @@ namespace { template struct CbData { CbData(T cb, void* user_data) : cb(cb), user_data(user_data) { } - CbData(T cb, std::string id, void* user_data) : cb(cb), id(id), user_data(user_data) { } + CbData(T cb, std::string id, void* user_data) + : cb(cb), id(id), user_data(user_data) { } T cb; std::string id; @@ -72,12 +73,16 @@ class CionClient : public cion::channel::ClientChannel { } } - void OnPayloadReceived(std::shared_ptr data) override { - std::shared_ptr peer = GetPeerInfo(); + void OnPayloadReceived(std::shared_ptr data, + std::shared_ptr peer, + IPayloadReceiver::PayloadTransferStatus status) override { std::string service_name = GetSerivceName(); auto peerinfo = static_cast(&peer); - for (const auto& cbdata : payload_recieved_cb_list_) - cbdata.cb(service_name.c_str(), peerinfo, &data, cbdata.user_data); + for (const auto& cbdata : payload_received_cb_list_) { + cbdata.cb(service_name.c_str(), peerinfo, &data, + static_cast(status), + cbdata.user_data); + } } void OnErrorReported(int code, @@ -107,7 +112,7 @@ class CionClient : public cion::channel::ClientChannel { void AddPayloadReceivedCb(cion_client_payload_recieved_cb cb, void* user_data) { - payload_recieved_cb_list_.emplace_back(cb, user_data); + payload_received_cb_list_.emplace_back(cb, user_data); } void AddErrorReportedCb(cion_client_error_reported_cb cb, void* user_data) { @@ -132,7 +137,7 @@ class CionClient : public cion::channel::ClientChannel { std::vector> connection_result_cb_list_; std::vector> - payload_recieved_cb_list_; + payload_received_cb_list_; std::vector> error_reported_cb_list_; std::vector> diff --git a/cion/tizen-api/cion_client.h b/cion/tizen-api/cion_client.h index 3a0824b..672e89a 100644 --- a/cion/tizen-api/cion_client.h +++ b/cion/tizen-api/cion_client.h @@ -56,6 +56,7 @@ typedef void (*cion_client_payload_async_result_cb)( */ typedef void (*cion_client_payload_recieved_cb)(const char *service_name, const cion_peer_info_h peer_info, const cion_payload_h payload, + cion_payload_transfer_status_e status, void *user_data); /** diff --git a/cion/tizen-api/cion_payload.h b/cion/tizen-api/cion_payload.h index a795a42..c6f789c 100644 --- a/cion/tizen-api/cion_payload.h +++ b/cion/tizen-api/cion_payload.h @@ -35,6 +35,16 @@ typedef enum _payload_type_e { CION_PAYLOAD_TYPE_FILE, } cion_payload_type_e; + +/** + * @brief + */ +typedef enum _payload_transfer_status_e { + CION_PAYLOAD_TRANSFER_STATUS_SUCCESS, + CION_PAYLOAD_TRANSFER_STATUS_FAILURE, + CION_PAYLOAD_TRANSFER_STATUS_IN_PROGRESS, +} cion_payload_transfer_status_e; + /** * @brief */ diff --git a/cion/tizen-api/cion_server.cc b/cion/tizen-api/cion_server.cc index 9309036..f34368b 100644 --- a/cion/tizen-api/cion_server.cc +++ b/cion/tizen-api/cion_server.cc @@ -96,14 +96,18 @@ class CionServer : public cion::channel::ServerChannel { } void OnPayloadReceived(std::shared_ptr data, - std::shared_ptr peer) { + std::shared_ptr peer, + IPayloadReceiver::PayloadTransferStatus status) { // FIXME auto ptr = new std::shared_ptr(data); auto payload = static_cast*>(ptr); auto peer_info = static_cast(&peer); std::string service_name = GetSerivceName(); - for (const auto& cbdata : payload_received_cb_list_) - cbdata.cb(service_name.c_str(), peer_info, payload, cbdata.user_data); + for (const auto& cbdata : payload_received_cb_list_) { + cbdata.cb(service_name.c_str(), peer_info, &data, + static_cast(status), + cbdata.user_data); + } // FIXME(jeremy.jang): THIS WILL DESTROY 'data' when left this scope. delete ptr; diff --git a/cion/tizen-api/cion_server.h b/cion/tizen-api/cion_server.h index acba725..66f0564 100644 --- a/cion/tizen-api/cion_server.h +++ b/cion/tizen-api/cion_server.h @@ -64,6 +64,7 @@ typedef void (*cion_server_data_recieved_cb)(const char *service_name, */ typedef void (*cion_server_payload_recieved_cb)(const char *service_name, const cion_peer_info_h peer_info, const cion_payload_h payload, + cion_payload_transfer_status_e status, void *user_data); /** -- 2.34.1