}
std::shared_ptr<VineClientDp> ClientChannel::Impl::PopDp(VineDpPtr dp) {
- for (std::shared_ptr<VineClientDp> i : dp_list_) {
- if (*i == *dp) {
- dp_list_.remove(i);
- return i;
+ for (auto it = dp_map_.begin(); it != dp_map_.end(); it++ ) {
+ if (*(it->first) == *dp) {
+ auto pop_dp = it->first;
+ dp_map_.erase(it);
+ return std::dynamic_pointer_cast<VineClientDp>(pop_dp);
}
}
return nullptr;
StopDiscovery();
discoverer_.reset();
session_.reset();
- for (const auto& dp : dp_list_) {
- dp->Dispose();
+ for (const auto& it : dp_map_) {
+ (it.first)->Dispose();
+ }
+ if (peer_) {
+ peer_sync_dp_ = nullptr;
+ peer_dp_ = nullptr;
}
- if (connected_peer_)
- connected_peer_->Dispose();
}
void ClientChannel::Impl::SendPeerInfo(PeerInfo* peerinfo) {
}
void ClientChannel::Impl::Send(cmd::CionCmd cmd, IPayload* data) {
- if (connected_peer_ == nullptr) {
+ if (peer_ == nullptr) {
LOG(ERROR) << "No connection to send";
THROW(error::CION_ERROR_INVALID_OPERATION);
}
auto dplist = std::make_shared<std::list<std::shared_ptr<VineDp>>>();
- dplist->push_back(connected_peer_->GetVineClientDp());
+ dplist->push_back(peer_dp_);
+
+ if (peer_dp_)
+ LOG(ERROR) << "peer dp exist";
+ else
+ LOG(ERROR) << "peer dp does not exist";
LOG(INFO) << "send CMD " << cmd;
AsyncSender as;
void ClientChannel::Impl::Send(cmd::CionCmd cmd,
const std::vector<uint8_t>& raw, VineDpPtr dp) {
LOG(INFO) << "send uint& CMD " << cmd;
- if (connected_peer_ == nullptr) {
+ if (peer_ == nullptr) {
LOG(ERROR) << "No connection to send";
THROW(error::CION_ERROR_INVALID_OPERATION);
}
if (dp)
client_dp = dp;
else
- client_dp = connected_peer_->GetVineClientDp();
+ client_dp = peer_dp_;
tizen_base::Parcel parcel;
parcel.WriteUInt32(cmd);
parcel.WriteUInt32(raw.size());
int timeout) {
std::lock_guard<std::mutex> lock(mutex_);
- if (!connected_peer_ || !is_connected_) {
+ if (!peer_ || !is_connected_) {
LOG(ERROR) << "No connection established";
THROW(error::CION_ERROR_INVALID_OPERATION);
}
LOG(WARNING) << "send data seq id : " << sequence_id_;
auto *d = reinterpret_cast<std::vector<uint8_t>*>(&data);
- Send(cmd::DataSync, *d, connected_peer_->GetVineClientSyncDp());
+ // Send(cmd::DataSync, *d, connected_peer_->GetVineClientSyncDp());
+ Send(cmd::DataSync, *d, peer_sync_dp_);
reply_queue_.WaitAndPopFor(ret, timeout);
if (ret.empty()) {
if (data == nullptr)
THROW(error::CION_ERROR_INVALID_PARAMETER);
- if (!connected_peer_ || !is_connected_) {
+ if (!peer_ || !is_connected_) {
LOG(ERROR) << "No connection established";
THROW(error::CION_ERROR_INVALID_OPERATION);
}
async_result_manager_->AddAsyncResultToWaitList(
- connected_peer_, data->GetID(),
+ peer_, data->GetID(),
PayloadAsyncResult::ResultCode::Pending, func);
Send(cmd::PayloadAsync, data);
if (!dp)
return;
- if (connected_peer_ != nullptr && connected_peer_->GetVineClientDp()) {
+ if (peer_ != nullptr && peer_dp_) {
LOG(INFO) << "Compare " << dp->GetRawDp() << " : "
- << connected_peer_->GetVineClientDp()->GetRawDp();
- if (*connected_peer_->GetVineClientDp() == *dp)
- parent_->OnDisconnected(connected_peer_);
+ << peer_dp_->GetRawDp();
+ if (*peer_dp_ == *dp)
+ parent_->OnDisconnected(peer_);
}
PopDp(dp);
}
std::shared_ptr<VineClientDp> ClientChannel::Impl::GetDp(
std::string ip, int port) {
- for (std::shared_ptr<VineClientDp> i : dp_list_) {
- if (i->GetPort() == port && i->GetIp() == ip)
- return i;
+ for (auto& it : dp_map_) {
+ auto dp = std::dynamic_pointer_cast<VineClientDp>(it.first);
+ if (dp->GetPort() == port && dp->GetIp() == ip)
+ return dp;
}
LOG(WARNING) << "Cannot find dp [ip : " << ip << ", port : "
<< port << "]";
return nullptr;
}
-std::shared_ptr<VineClientDp> ClientChannel::Impl::GetDp(
- std::shared_ptr<VineDp> dp) {
- for (std::shared_ptr<VineClientDp> i : dp_list_) {
- if (*i == *dp)
- return i;
+std::shared_ptr<VineClientDp> ClientChannel::Impl::GetPeerDp(std::shared_ptr<PeerInfo> peer) {
+ std::shared_ptr<VineClientDp> client_dp = nullptr;
+ for (auto& it : dp_map_) {
+ if (*it.second == *peer) {
+ client_dp = std::dynamic_pointer_cast<VineClientDp>(it.first);
+ break;
+ }
}
- LOG(WARNING) << "Cannot find dp : " << dp->GetRawDp();
- return nullptr;
+ return client_dp;
}
void ClientChannel::Impl::TrySyncReply(std::vector<unsigned char> data) {
}
void ClientChannel::Impl::TryConnect(std::shared_ptr<PeerInfo> peer) {
- if (connected_peer_) {
+ if (peer_) {
LOG(ERROR) << "There is a ongoing connection!";
return;
}
LOG(INFO) << "TryConnect";
- std::shared_ptr<ClientPeerInfo> cpeer =
- std::dynamic_pointer_cast<ClientPeerInfo>(peer);
- connected_peer_ = cpeer;
+ peer_ = peer;
+ std::shared_ptr<VineClientDp> client_dp = GetPeerDp(peer);
+ if (!client_dp) {
+ LOG(ERROR) << "Cannot find dp for " << peer->GetUUID();
+ return;
+ }
std::string appid = peer->GetAppID();
if (peer == ondemand_list_[appid]) {
parcel.Write(payload_data_raw.data(), payload_data_raw.size());
try {
- Send(cmd::OndemandLaunch, parcel.GetRaw(),
- connected_peer_->GetVineClientDp());
+ Send(cmd::OndemandLaunch, parcel.GetRaw(), GetPeerDp(peer_));
} catch (const Exception& e) {
LOG(ERROR) << "Failed to try connect: " << e.what();
throw e;
return;
}
- std::shared_ptr<VineClientDp> client_dp = connected_peer_->GetVineClientDp();
+ // std::shared_ptr<VineClientDp> client_dp = connected_peer_->GetVineClientDp();
client_dp->Close();
client_dp->SetDpOpenedEventHandler(this);
client_dp->SetDpReceivedEventHandler(this);
}
void ClientChannel::Impl::Disconnect() {
- if (connected_peer_ == nullptr) {
+ if (peer_ == nullptr) {
LOG(ERROR) << "no connection";
return;
}
is_connected_ = false;
- Disconnect(connected_peer_->GetVineClientSyncDp());
- Disconnect(connected_peer_->GetVineClientDp());
- connected_peer_ = nullptr;
+ Disconnect(peer_dp_);
+ Disconnect(peer_sync_dp_);
+ peer_ = nullptr;
}
void ClientChannel::Impl::OnServiceDiscovered(VineService service,
return;
}
- dp_list_.push_back(cdp);
+ dp_map_.emplace(cdp, nullptr);
cdp->SetDpReceivedEventHandler(this);
cdp->SetDpTerminatedEventHandler(this);
if (!security_.IsEmpty()) {
}
void ClientChannel::Impl::OnDpOpened(VineDpPtr dp, int result) {
- if (connected_peer_) {
+ if (peer_) {
LOG(INFO) << "OnDpOpened : SendPeerInfo " << dp->GetRawDp();
try {
Send(cmd::CionCmd::PeerInfo, my_peer_info_->Serialize(), dp);
- auto cdp = connected_peer_->GetVineClientDp();
+ auto cdp = GetPeerDp(peer_);
if (cdp && cdp->GetRawDp() != dp->GetRawDp()) {
// Sync dp is opened, invoke OnConnectionResultCallback at idle
idle_invoker_.InvokeAtIdle(
[this]() -> void {
ConnectionResult res(ConnectionResult::ConnectionStatus::OK);
is_connected_ = true;
- parent_->OnConnectionResult(connected_peer_, res);
+ parent_->OnConnectionResult(peer_, res);
});
}
} catch (const Exception& e) {
parcel->ReadUInt32(&size);
std::vector<unsigned char> peerinfo_data(size, 0);
parcel->Read(peerinfo_data.data(), peerinfo_data.size());
- auto peer_dp = GetDp(dp);
- std::shared_ptr<ClientPeerInfo> discovered_info =
- std::make_shared<ClientPeerInfo>(peerinfo_data.data(),
- peerinfo_data.size(), GetDp(dp));
+ std::shared_ptr<PeerInfo> discovered_info =
+ std::make_shared<PeerInfo>(peerinfo_data.data(),
+ peerinfo_data.size());
+ if (dp_map_.count(dp))
+ dp_map_[dp] = discovered_info;
- auto peer = connected_peer_;
+ auto peer = peer_;
if (peer && peer->GetDeviceID() == discovered_info->GetDeviceID() &&
peer->GetAppID() == discovered_info->GetAppID()) {
if (ondemand_list_[discovered_info->GetAppID()]) {
LOG(INFO) << "Ondemand Launched " << discovered_info->GetAppID();
- connected_peer_ = discovered_info;
- std::shared_ptr<VineClientDp> client_dp =
- connected_peer_->GetVineClientDp();
+ peer_ = discovered_info;
+ std::shared_ptr<VineClientDp> client_dp = GetPeerDp(peer_);
client_dp->Close();
client_dp->SetDpOpenedEventHandler(this);
client_dp->SetDpReceivedEventHandler(this);
ondemand_list_.erase(discovered_info->GetAppID());
ConnectionResult result(ConnectionResult::ConnectionStatus::OK);
- parent_->OnConnectionResult(std::dynamic_pointer_cast<PeerInfo>(
- connected_peer_), result);
+ parent_->OnConnectionResult(peer_, result);
return;
}
return;
}
- if (!peer_dp) {
+ if (!dp_map_.count(dp)) {
LOG(WARNING) << "Ignore sync dp";
return;
}
LOG(INFO) << "On discovered " << discovered_info->GetAppID();
- parent_->OnDiscovered(
- std::dynamic_pointer_cast<PeerInfo>(discovered_info));
+ parent_->OnDiscovered(discovered_info);
return;
}
case cmd::Accept: {
LOG(INFO) << "Connection request accepted";
- auto peer = connected_peer_;
- auto client_dp = peer->GetVineClientDp();
+ // auto peer = connected_peer_;
+ auto client_dp = GetPeerDp(peer_);
+ peer_dp_ = client_dp;
+ // auto client_dp = peer->GetVineClientDp();
auto client_sync_dp = std::make_shared<VineClientDp>(
session_, client_dp->GetIp(),
client_dp->GetRemotePort());
- peer->SetVineClientSyncDp(client_sync_dp);
+ // peer->SetVineClientSyncDp(client_sync_dp);
+ peer_sync_dp_ = client_sync_dp;
client_sync_dp->SetDpOpenedEventHandler(this);
client_sync_dp->SetDpReceivedEventHandler(this);
client_sync_dp->SetDpTerminatedEventHandler(this);
ConnectionResult result(
ConnectionResult::ConnectionStatus::Rejected, reason);
- parent_->OnConnectionResult(std::dynamic_pointer_cast<PeerInfo>(
- connected_peer_), result);
- connected_peer_ = nullptr;
+ parent_->OnConnectionResult(peer_, result);
+ peer_ = nullptr;
break;
}
case cmd::AsyncResult: {
parcel->Read(res_data.data(), res_data.size());
std::shared_ptr<PayloadAsyncResult> result =
std::make_shared<PayloadAsyncResult>(std::move(res_data));
- result->SetPeerInfo(connected_peer_);
+ result->SetPeerInfo(peer_);
async_result_manager_->InvokeResultCallback(result);
break;
}
case cmd::PayloadAsync: {
PayloadManager pm(parent_);
- pm.Process(parcel, dp,
- my_peer_info_, connected_peer_);
+ pm.Process(parcel, dp, my_peer_info_, peer_);
break;
}
case cmd::OndemandDaemonReply: {
list_parcel->ReadUInt32(&peer_data_size);
std::vector<unsigned char> peerinfo_data(peer_data_size, 0);
list_parcel->Read(peerinfo_data.data(), peerinfo_data.size());
- std::shared_ptr<ClientPeerInfo> discovered_info =
- std::make_shared<ClientPeerInfo>(peerinfo_data.data(),
- peerinfo_data.size(), GetDp(dp));
+ std::shared_ptr<PeerInfo> discovered_info =
+ std::make_shared<PeerInfo>(peerinfo_data.data(),
+ peerinfo_data.size());
ondemand_list_[discovered_info->GetAppID()] =
discovered_info;
LOG(INFO) << "On discovered " << discovered_info->GetAppID();
}
std::shared_ptr<PeerInfo> ClientChannel::GetPeerInfo() {
- return impl_->connected_peer_;
+ return impl_->peer_;
}
std::string ClientChannel::GetServiceName() {