--- /dev/null
-#define LOG_TAG "VINE_MANAGER"
+ /*
+ * 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 <dlog.h>
+ #include <unistd.h>
+ #include <glib-unix.h>
+ #include <sys/socket.h>
+ #include <sys/types.h>
+ #include <glib.h>
++#include <string.h>
+
+ #include <memory>
+ #include <utility>
+
+ #include <parcel.hh>
+ #include "vine_manager.hh"
+ #include "vine_manager_implementation.hh"
+ #include "cion/channel/data_info.hh"
+
+ #ifdef LOG_TAG
+ #undef LOG_TAG
+ #endif
+
-#define VINE_TEST_TOPIC "CION_TOPIC"
++#define LOG_TAG "CION"
+
-#define VINE_TEST_SERVER_PORT 55555
-#define VINE_TEST_CLIENT_PORT 55556
+ #define VINE_TEST_SERVICE "CION_TEST"
- vine_dp_set_received_cb(dp,
- [](vine_dp_h dp,
- size_t received_len, void *user_data) {
- int channel_id = GPOINTER_TO_INT(user_data);
- unsigned char buf[256] = {0, };
- size_t bytes = 0;
- LOGW("[RECV_CB] %p received %zd bytes.",
- dp, received_len);
- vine_dp_recv(dp, buf, 256, &bytes);
-
- std::vector<char> serialized_peerinfo;
- serialized_peerinfo.assign(buf, buf + bytes);
- channel::IEventObserver* observer =
- VineManager::GetInst().GetObserver(channel_id);
- if (observer == nullptr) {
- LOGE("cannot find observer (%d)", channel_id);
- return;
- }
- observer->OnConnectionEvent(
- std::make_shared<ConnectionInfo>(
- IConnectionInfo::ConnectionType::Discovered),
- serialized_peerinfo);
-
- PeerInfo p_info(buf, bytes);
- VineManager::GetInst().AddDataPath(
- channel_id, p_info.GetUUID(),
- p_info.GetChannelID(), dp);
- }, user_data);
+ #define VINE_TEST_SERVICE_TYPE "_cion"
+
+ using namespace cion::channel;
+
+ namespace cion {
+
+ VineManager& VineManager::GetInst() {
+ static VineManager w_inst;
+ int ret;
+
+ if (w_inst.impl_ == nullptr) {
+ ret = w_inst.Init();
+ if (ret != CION_ERROR_NONE)
+ throw ret;
+ }
+ return w_inst;
+ }
+
+ VineManager::VineManager() = default;
+ VineManager::~VineManager() = default;
+ VineManager::Impl::Impl() = default;
+
+ VineManager::Impl::~Impl() {
+ vine_deinitialize();
+ }
+
+ int VineManager::Init() {
+ std::unique_ptr<Impl> tmp_impl;
+
+ try {
+ tmp_impl = std::unique_ptr<VineManager::Impl>(new Impl());
+ } catch (const std::bad_alloc &ba) {
+ LOGE("Out of memory");
+ return -1;
+ }
+
+ impl_ = std::move(tmp_impl);
+ vine_initialize();
+ LOGI("VineManager init done");
+ return 0;
+ }
+
+ void VineManager::Impl::AddSenderInfo(std::string service_name,
+ int channel_id, std::shared_ptr<PeerInfo> peer_info) {
+ auto result = sender_list_.find(channel_id);
+ if (result != sender_list_.end()) {
+ LOGW("Already exist");
+ return;
+ }
+
+ sender_list_[channel_id] =
+ std::make_shared<SenderInfo>(service_name, channel_id, peer_info);
+ }
+
+ std::shared_ptr<SenderInfo> VineManager::Impl::GetSenderInfo(int channel_id) {
+ auto result = sender_list_.find(channel_id);
+ if (result != sender_list_.end())
+ return result->second;
+ return {};
+ }
+
+ bool VineManager::Impl::ValidateSender(std::string service_name) {
+ for (auto& sender : sender_list_) {
+ if (service_name == sender.second->GetServiceName())
+ return true;
+ }
+ return false;
+ }
+
+ channel::IEventObserver* VineManager::GetObserver(int channel_id) {
+ for (channel::IEventObserver* i : impl_->observer_list_) {
+ if (i->GetChannelId() == channel_id)
+ return i;
+ }
+ return nullptr;
+ }
+
++void VineManager::HandlingDiscoveredPeer(vine_dp_h dp, size_t received_len,
++ int channel_id, channel::IEventObserver* observer) {
++ unsigned char buf[256] = {0, };
++ size_t bytes = 0;
++
++ LOGW("[RECV_CB] %p received %zd bytes.", dp, received_len);
++ vine_dp_recv(dp, buf, 256, &bytes);
++ std::vector<char> serialized_peerinfo;
++ serialized_peerinfo.assign(buf, buf + bytes);
++
++ std::shared_ptr<PeerInfo> p_info = std::make_shared<PeerInfo>(buf, bytes);
++ std::shared_ptr<DataPathInfo> dp_info =
++ VineManager::GetInst().AddDataPath(channel_id, p_info->GetUUID(),
++ p_info->GetChannelID(), dp);
++ dp_info->SetPeerInfo(p_info);
++
++ observer->OnConnectionEvent(
++ std::make_shared<ConnectionInfo>(
++ IConnectionInfo::ConnectionType::Discovered),
++ serialized_peerinfo);
++}
++
++void VineManager::HandlingConnReqResp(vine_dp_h dp, size_t received_len,
++ int channel_id, std::shared_ptr<DataPathInfo> dp_info,
++ channel::IEventObserver* observer) {
++ unsigned char buf[256] = {0, };
++ size_t bytes = 0;
++
++ vine_dp_recv(dp, buf, 256, &bytes);
++ std::vector<char> serialized_data_payload;
++ serialized_data_payload.assign(buf, buf + bytes);
++
++ DataPayload dpl(serialized_data_payload);
++ std::vector<char> dpl_data = dpl.GetData();
++ std::string str(dpl_data.begin(), dpl_data.end());
++ LOGI("connection request resp (%s)", str.c_str());
++
++ std::shared_ptr<PeerInfo> pinfo = dp_info->GetPeerInfo();
++ std::vector<uint8_t> data = pinfo->Serialize();
++ char* p = reinterpret_cast<char*>(&data[0]);
++ std::vector<char> serialized_peerinfo(p, p + data.size());
++ if (strcmp(str.c_str(), "__accept__") == 0) {
++ LOGI("connection accepted");
++ observer->OnConnectionEvent(std::make_shared<ConnectionInfo>(
++ IConnectionInfo::ConnectionType::Connected), serialized_peerinfo);
++ dp_info->SetState(DataPathInfo::State::Connected);
++ } else if (strcmp(str.c_str(), "__reject__") == 0) {
++ LOGI("connection rejected");
++ observer->OnConnectionEvent(std::make_shared<ConnectionInfo>(
++ IConnectionInfo::ConnectionType::Rejected), serialized_peerinfo);
++ dp_info->SetState(DataPathInfo::State::Rejected);
++ }
++}
++
++void VineManager::SetClientRecvCallback(vine_dp_h client_dp, void* user_data) {
++ vine_dp_set_received_cb(client_dp,
++ [](vine_dp_h dp,
++ size_t received_len, void *user_data) {
++ int channel_id = GPOINTER_TO_INT(user_data);
++
++ channel::IEventObserver* observer =
++ VineManager::GetInst().GetObserver(channel_id);
++ if (observer == nullptr) {
++ LOGE("cannot find observer (%d)", channel_id);
++ return;
++ }
++
++ std::shared_ptr<DataPathInfo> dp_info =
++ VineManager::GetInst().GetDataPathInfo(
++ channel_id, dp);
++ if (dp_info == nullptr) {
++ VineManager::GetInst()
++ .HandlingDiscoveredPeer(dp, received_len, channel_id, observer);
++ } else if (dp_info->GetState() ==
++ DataPathInfo::State::ConnectionRequested) {
++ VineManager::GetInst().HandlingConnReqResp(dp, received_len,
++ channel_id, dp_info, observer);
++ } else {
++ LOGI("@@@@@@@@ recv");
++ }
++ }, user_data);
++}
++
+ bool VineManager::Discovery(int channel_id) {
+ std::shared_ptr<SessionInfo> info = GetSessionInfo(channel_id);
+ if (info->IsDiscovering()) {
+ LOGE("Already discovering (%d)", channel_id);
+ return false;
+ }
+
+ vine_session_set_discovered_cb(info->GetSession(),
+ [] (vine_session_h session, vine_service_h service,
+ vine_service_state_e state, void *user_data) -> void {
+ char* name;
+ int port;
+ vine_service_get_name(service, &name);
+ vine_service_get_port(service, &port);
+
+ LOGD("Available Service");
+ LOGD("Service Name: %s", name);
+ LOGD("Port: %d", port);
+ if (state != VINE_SERVICE_AVAILABLE) {
+ LOGE("Vine service unavailable (%d)", state);
+ return;
+ }
+
+ LOGW("A service is discovered\n");
+ vine_session_set_ip_resolved_cb(session, service,
+ [] (vine_session_h session, vine_service_h service,
+ const char *ip, vine_address_family_e address_family,
+ void *user_data) -> void {
+ int port;
+ vine_service_get_port(service, &port);
+ LOGW("IP address: %s\n", ip);
+ LOGW("port: %d\n", port);
+ vine_dp_h dp;
+ vine_dp_create(session, VINE_DP_TYPE_CLIENT, &dp);
+ vine_dp_set_remote_ip(dp, VINE_ADDRESS_FAMILY_IPV4, ip);
+ vine_dp_set_security(dp, nullptr);
+ vine_dp_set_port(dp, port);
+ vine_dp_open(dp,
+ [](vine_dp_h dp, vine_error_e result, void *user_data)
+ -> void {
+ LOGW("@@@@@@@ open");
-void VineManager::AddDataPath(int channel_id, std::string remote_uuid,
++ VineManager::GetInst()
++ .SetClientRecvCallback(dp, user_data);
+ }, user_data);
+ }, user_data);
+ }, GINT_TO_POINTER(channel_id));
+ vine_session_start_discovery(info->GetSession(),
+ VINE_TEST_SERVICE_TYPE, nullptr);
+ info->SetIsDiscovering(true);
+ LOGI("start discovery");
+ return true;
+ }
+
- DataPathInfo dp_info(channel_id, remote_uuid, remote_channel_id, dp);
++std::shared_ptr<DataPathInfo> VineManager::AddDataPath(
++ int channel_id, std::string remote_uuid,
+ int remote_channel_id, vine_dp_h dp) {
+ LOGE("@@@ uuid (%s)(%p)", remote_uuid.c_str(), dp);
- if (channel_id != i.GetLocalChannelId())
++ std::shared_ptr<DataPathInfo> dp_info = std::make_shared<DataPathInfo>(
++ channel_id, remote_uuid, remote_channel_id, dp);
+ impl_->data_path_list_.push_back(dp_info);
++ return dp_info;
+ }
+
+ vine_dp_h VineManager::Impl::FindDataPath(int channel_id, std::string remote_uuid,
+ int remote_channel_id) {
+ for (auto& i : data_path_list_) {
- if (remote_channel_id != -1 && remote_channel_id != i.GetRemoteChannelId())
++ if (channel_id != i->GetLocalChannelId())
+ continue;
- if (remote_uuid.empty() == false && remote_uuid != i.GetRemoteUUID())
++ if (remote_channel_id != -1 && remote_channel_id != i->GetRemoteChannelId())
+ continue;
- return i.GetDataPath();
++ if (remote_uuid.empty() == false && remote_uuid != i->GetRemoteUUID())
+ continue;
+
-void VineManager::RegisterSender(std::string service_name, int channel_id, std::shared_ptr<cion::PeerInfo> peer_info) {
- impl_->AddSenderInfo(service_name, channel_id, peer_info);
++ return i->GetDataPath();
+ }
+ return nullptr;
+ }
- int channel_id) {
++void VineManager::RegisterSender(std::string service_name,
++ int channel_id, std::shared_ptr<cion::PeerInfo> peer_info) {
++ impl_->AddSenderInfo(service_name, channel_id, peer_info);
+ }
+
+ void VineManager::UnregisterSender(int channel_id) {
+ }
+
+ void VineManager::Subscribe(int channel_id) {
+ std::shared_ptr<SessionInfo> info = GetSessionInfo(channel_id);
+ vine_dp_h dp;
+ vine_dp_create(info->GetSession(), VINE_DP_TYPE_PUBSUB, &dp);
+ vine_dp_set_address_family(dp, VINE_ADDRESS_FAMILY_IPV4);
+ vine_dp_set_port(dp, 0);
+ vine_dp_set_topic(dp, impl_->sender_list_[channel_id]->GetServiceName().c_str());
+
+ vine_dp_open(dp,
+ [] (vine_dp_h client_dp, vine_error_e result, void *user_data) -> void {
+ LOGI("@@@@@@@ dp open");
+ int channel_id = GPOINTER_TO_INT(user_data);
+ vine_dp_set_received_cb(client_dp,
+ [](vine_dp_h client_dp, size_t received_len, void *user_data) -> void {
+
+ int channel_id = GPOINTER_TO_INT(user_data);
+ unsigned char *data = new unsigned char[received_len];
+ size_t bytes;
+
+ vine_dp_recv(client_dp, data, received_len, &bytes);
+
+ LOGI("received_len bytes %d %d ", received_len, bytes);
+
+ uint32_t size;
+ tizen_base::Parcel parcel(data, bytes);
+ delete[] data;
+
+ parcel.ReadUInt32(&size);
+ data = new unsigned char[size];
+ parcel.Read(data, size);
+ auto peer_info = std::make_shared<PeerInfo>(data, size);
+ delete[] data;
+
+ parcel.ReadUInt32(&size);
+ data = new unsigned char[size];
+ parcel.Read(data, size);
+
+ channel::IEventObserver* observer =
+ VineManager::GetInst().GetObserver(channel_id);
+ observer->OnDataEvent(std::make_shared<DataInfo>(IDataInfo::MessageType::BroadCast),
+ std::vector<char>(data, data + size), peer_info);
+ delete[] data;
+
+ }, user_data);
+
+ VineManager::GetInst().AddDataPath(channel_id, "", -1, client_dp);
+ }, GINT_TO_POINTER(channel_id));
+
+ LOGI("Subscribe done");
+ }
+
+ void VineManager::RegisterService(std::shared_ptr<SessionInfo> info, int port) {
+ info->SetPort(port);
+ vine_session_set_registered_cb(info->GetSession(), [] (
+ vine_session_h session, const char* service_name,
+ vine_error_e error, void* user_data) -> void {
+
+ LOGI("session registered");
+ }, nullptr);
+
+ vine_session_register(info->GetSession(), info->GetService(), nullptr);
+ }
+
+ std::shared_ptr<SessionInfo> VineManager::GetSessionInfo(
-bool VineManager::IsDataPathExist(int channel_id, vine_dp_h dp) {
++ int channel_id) {
+ auto result = impl_->sessions_.find(channel_id);
+ if (result != impl_->sessions_.end())
+ return result->second;
+
+ LOGI("Not found try create new(%d)", channel_id);
+ vine_session_h session;
+ vine_session_create(&session);
+ int fd;
+ vine_session_get_event_fd(session, &fd);
+ g_unix_fd_add(fd, G_IO_IN , [] (int fd, GIOCondition condition,
+ gpointer user_data) -> int {
+ LOGI("gio in!! %d", (int)condition);
+ vine_session_h session = static_cast<VineManager*>(user_data);
+ vine_session_process_event(session);
+ LOGI("process event done !!");
+ return G_SOURCE_CONTINUE;
+ }, session);
+
+ impl_->sessions_[channel_id] =
+ std::make_shared<SessionInfo>(channel_id, session);
+ LOGI("create session(%d) done !!", channel_id);
+ result = impl_->sessions_.find(channel_id);
+ return result->second;
+ }
+
+ void VineManager::SendPeerInfo(vine_dp_h dp) {
+ PeerInfo info;
+ std::vector<uint8_t> data = info.Serialize();
+ if (data.size() == 0) {
+ LOGE("fail to serialize peer info");
+ return;
+ }
+
+ auto* p = reinterpret_cast<unsigned char*>(&data[0]);
+ vine_dp_send(dp, p, data.size());
+ LOGW("send peerinfo %d", data.size());
+ }
+
++std::shared_ptr<DataPathInfo> VineManager::GetDataPathInfo(
++ int channel_id, vine_dp_h dp) {
++ for (auto& i : impl_->data_path_list_) {
++ if (i->GetLocalChannelId() == channel_id && i->GetDataPath() == dp)
++ return i;
++ }
++ return nullptr;
++}
+
- if (i.GetLocalChannelId() == channel_id && i.GetDataPath() == dp)
- return true;
++std::shared_ptr<DataPathInfo> VineManager::GetDataPathInfo(
++ int channel_id, std::shared_ptr<PeerInfo> peer_info) {
++ LOGI("Try Connect");
++ int remote_channel_id = peer_info->GetChannelID();
++ std::string remote_uuid = peer_info->GetUUID();
++ LOGI("data path cnt (%d)", impl_->data_path_list_.size());
+ for (auto& i : impl_->data_path_list_) {
- return false;
++ LOGI("%d, %d, %s", channel_id, remote_channel_id, remote_uuid.c_str());
++ LOGI("peer %d, %d, %s", i->GetLocalChannelId(),
++ i->GetRemoteChannelId(), i->GetRemoteUUID().c_str());
++ if (i->GetLocalChannelId() == channel_id &&
++ i->GetRemoteChannelId() == remote_channel_id &&
++ i->GetRemoteUUID() == remote_uuid) {
++ return i;
++ }
+ }
- vine_dp_h dp, vine_dp_h accepted_dp, void *user_data) -> void {
++ return nullptr;
++}
++
++void VineManager::Accept(int channel_id, std::shared_ptr<PeerInfo> peer_info) {
++ channel::IEventObserver* observer = GetObserver(channel_id);
++ if (observer == nullptr) {
++ LOGE("cannot find observer (%d)", channel_id);
++ return;
++ }
++
++ std::vector<unsigned char> u_serialized_peerinfo = peer_info->Serialize();
++ std::vector<char> serialized_peerinfo(
++ u_serialized_peerinfo.begin(), u_serialized_peerinfo.end());
++ observer->OnConnectionEvent(
++ std::make_shared<ConnectionInfo>(
++ IConnectionInfo::ConnectionType::Connected),
++ serialized_peerinfo);
++ std::shared_ptr<DataPathInfo> dpInfo = GetDataPathInfo(channel_id, peer_info);
++
++ vine_dp_h dp = dpInfo->GetDataPath();
++ char cmd[] = "__accept__";
++ std::vector<char> cmd_data(cmd, cmd + sizeof(cmd)/sizeof(*cmd));
++ DataPayload dpayload;
++ dpayload.SetData(cmd_data);
++ std::vector<char> serialized = dpayload.Serialize();
++ auto* p = reinterpret_cast<unsigned char*>(&serialized[0]);
++ vine_dp_send(dp, p, serialized.size());
++ LOGI("@@@@@@@@ send datapayload (%d)", serialized.size());
++
++ dpInfo->SetState(DataPathInfo::State::Connected);
++}
++
++void VineManager::Reject(int channel_id, std::shared_ptr<PeerInfo> peer_info) {
++ channel::IEventObserver* observer = GetObserver(channel_id);
++ if (observer == nullptr) {
++ LOGE("cannot find observer (%d)", channel_id);
++ return;
++ }
++
++ std::vector<unsigned char> u_serialized_peerinfo = peer_info->Serialize();
++ std::vector<char> serialized_peerinfo(
++ u_serialized_peerinfo.begin(), u_serialized_peerinfo.end());
++ observer->OnConnectionEvent(
++ std::make_shared<ConnectionInfo>(
++ IConnectionInfo::ConnectionType::Rejected),
++ serialized_peerinfo);
++ std::shared_ptr<DataPathInfo> dpInfo = GetDataPathInfo(channel_id, peer_info);
++
++ vine_dp_h dp = dpInfo->GetDataPath();
++ char cmd[] = "__reject__";
++ std::vector<char> cmd_data(cmd, cmd + sizeof(cmd)/sizeof(*cmd));
++ DataPayload dpayload;
++ dpayload.SetData(cmd_data);
++ std::vector<char> serialized = dpayload.Serialize();
++ auto* p = reinterpret_cast<unsigned char*>(&serialized[0]);
++ vine_dp_send(dp, p, serialized.size());
+ }
+
+ void VineManager::OpenServer(int channel_id) {
+ std::shared_ptr<SessionInfo> info = GetSessionInfo(channel_id);
+ vine_dp_h dp;
++
+ vine_dp_create(info->GetSession(), VINE_DP_TYPE_SERVER, &dp);
+ LOGI("create dp done");
+ vine_dp_set_accepted_cb(dp, [] (
- vine_dp_set_received_cb(accepted_dp,
++ vine_dp_h dp, vine_dp_h client_dp, void *user_data) -> void {
+ LOGW("accpeted");
- if (VineManager::GetInst().IsDataPathExist(channel_id, client_dp)) {
- // data recieved
++ vine_dp_set_received_cb(client_dp,
+ [](vine_dp_h client_dp, size_t received_len,
+ void *user_data) -> void {
+ int channel_id = GPOINTER_TO_INT(user_data);
-
++ std::shared_ptr<DataPathInfo> dpInfo =
++ VineManager::GetInst().GetDataPathInfo(channel_id, client_dp);
++ if (dpInfo) {
++ if (dpInfo->GetState() == DataPathInfo::State::Connected) {
++ // data recieved
++ }
+ } else { // connection request
+ channel::IEventObserver* observer =
+ VineManager::GetInst().GetObserver(channel_id);
+ if (observer == nullptr) {
+ LOGE("cannot find observer (%d)", channel_id);
+ return;
+ }
+
+ unsigned char buf[256] = {0, };
+ size_t bytes = 0;
++
+ LOGW("[RECV_CB] %p received %zd bytes.",
+ client_dp, received_len);
+ vine_dp_recv(client_dp, buf, 256, &bytes);
- //impl_->AddDataPath(
- // channel_id, p_info.GetUUID(), p_info.GetChannelID(), client_dp);
+ std::vector<char> serialized_peerinfo;
+ serialized_peerinfo.assign(buf, buf + bytes);
++
++ PeerInfo p_info(buf, bytes);
++ VineManager::GetInst().AddDataPath(
++ channel_id, p_info.GetUUID(), p_info.GetChannelID(), client_dp);
++
+ observer->OnConnectionEvent(
+ std::make_shared<ConnectionInfo>(
+ IConnectionInfo::ConnectionType::ConnectionRequested),
+ serialized_peerinfo);
- VineManager::GetInst().SendPeerInfo(accepted_dp);
+ }
+ }, user_data);
+ // send peer info to Client
- if (i.GetLocalChannelId() == channel_id &&
- i.GetRemoteChannelId() == remote_channel_id &&
- i.GetRemoteUUID() == remote_uuid) {
++ VineManager::GetInst().SendPeerInfo(client_dp);
+ }, GINT_TO_POINTER(channel_id));
+ LOGI("accepted callback add done");
+ vine_dp_set_port(dp, 0);
+ vine_dp_open(dp,
+ [] (vine_dp_h dp, vine_error_e result, void *user_data) -> void {
+ LOGW("Opened !!! %d", result);
+ int port;
+ int channel_id = GPOINTER_TO_INT(user_data);
+ vine_dp_get_port(dp, &port);
+ VineManager::GetInst().RegisterService(
+ VineManager::GetInst().GetSessionInfo(channel_id), port);
+ }, GINT_TO_POINTER(channel_id));
+ }
+
+ void VineManager::RegisterObserver(channel::IEventObserver* observer) {
+ int channel_id = observer->GetChannelId();
+ std::string service_name = observer->GetServiceName();
+ std::shared_ptr<SessionInfo> info = GetSessionInfo(channel_id);
+
+ vine_service_h service;
+ vine_service_create(&service);
+ vine_service_set_type(service, VINE_TEST_SERVICE_TYPE);
+ vine_service_set_name(service, service_name.c_str());
+ info->SetService(service);
+ impl_->observer_list_.push_back(observer);
+ LOGI("session register !! : %s", service_name.c_str());
+ }
+
+ void VineManager::UnregisterObserver(channel::IEventObserver* observer) {
+ }
+
+ void VineManager::Connect(int channel_id, std::shared_ptr<PeerInfo> peer_info) {
+ LOGI("Try Connect");
+ int remote_channel_id = peer_info->GetChannelID();
+ std::string remote_uuid = peer_info->GetUUID();
++ LOGI("data path cnt (%d)", impl_->data_path_list_.size());
+ for (auto& i : impl_->data_path_list_) {
- SendPeerInfo(i.GetDataPath());
++ LOGI("%d, %d, %s", channel_id, remote_channel_id, remote_uuid.c_str());
++ LOGI("peer %d, %d, %s", i->GetLocalChannelId(),
++ i->GetRemoteChannelId(), i->GetRemoteUUID().c_str());
++ if (i->GetLocalChannelId() == channel_id &&
++ i->GetRemoteChannelId() == remote_channel_id &&
++ i->GetRemoteUUID() == remote_uuid) {
+ LOGI("Peer info (%d -> %s:%d)",
+ channel_id, remote_uuid.c_str(), remote_channel_id);
- }
-}
-
-/*
-
-enum ControlType {
- LISTEN,
- STOP,
- CONNECT,
- DISCONNECT,
- DISCOVERY,
- STOP_DISCOVERY
- };
-
- enum ChannelType {
- COMMUNICATION,
- BROADCAST,
- };
-
-*/
++ SendPeerInfo(i->GetDataPath());
++ i->SetState(DataPathInfo::State::ConnectionRequested);
+ break;
+ }
+ }
+ }
+
+ void VineManager::SendDataAsync(const std::vector<char>& serialized_payload,
+ std::shared_ptr<channel::IDataInfo> info, int channel_id,
+ std::shared_ptr<PeerInfo> remote_peer_info) {
+
+ auto sender = impl_->GetSenderInfo(channel_id);
+ auto session = GetSessionInfo(channel_id);
+ auto datapath = impl_->FindDataPath(channel_id, "", -1);
+ if (info->GetType() == IDataInfo::MessageType::BroadCast) {
+ tizen_base::Parcel parcel;
+ std::vector<uint8_t> peer = sender->GetPeerInfo()->Serialize();
+ if (peer.size() == 0) {
+ LOGE("fail to serialize peer info");
+ return;
+ }
+
+ parcel.WriteUInt32(peer.size());
+ parcel.Write(peer.data(), peer.size());
+
+ parcel.WriteUInt32(serialized_payload.size());
+ parcel.Write(serialized_payload.data(), serialized_payload.size());
+
+ auto* p = reinterpret_cast<unsigned char*>(const_cast<uint8_t*>(parcel.GetRaw().data()));
+ vine_dp_send(datapath , p, parcel.GetRaw().size());
+ LOGW("send payload %d", parcel.GetRaw().size());
+ }
+ }
+
+ std::vector<char> VineManager::SendData(
+ const std::vector<char>& serialized_payload,
+ std::shared_ptr<channel::IDataInfo> info, int channel_id,
+ std::string peer_uuid, int timeout) {
+ return {};
+ }
+
+ void VineManager::OperateChannel(int channel_id,
+ std::shared_ptr<channel::IControlInfo> info,
+ std::shared_ptr<PeerInfo> peer_info) {
+ if (info->GetChannelType() == IControlInfo::ChannelType::COMMUNICATION) {
+ switch (info->GetControlType()) {
+ case IControlInfo::ControlType::LISTEN :
+ OpenServer(channel_id);
+ break;
+ case IControlInfo::ControlType::STOP :
+ break;
+ case IControlInfo::ControlType::CONNECT :
+ Connect(channel_id, peer_info);
+ break;
+ case IControlInfo::ControlType::DISCONNECT :
+ break;
+ case IControlInfo::ControlType::DISCOVERY :
+ Discovery(channel_id);
+ break;
+ case IControlInfo::ControlType::STOP_DISCOVERY :
+ break;
+ }
+ } else if(info->GetChannelType() == IControlInfo::ChannelType::BROADCAST)
+ switch (info->GetControlType()) {
+ case IControlInfo::ControlType::LISTEN :
+ Subscribe(channel_id);
+ break;
+ case IControlInfo::ControlType::STOP :
+ break;
+ default :
+ break;
++ }
++ }
+
+ } // namespace cion