Use peer_id instead of ip address in pub/sub
authorjusung <jusung07.son@samsung.com>
Tue, 1 Jun 2021 11:49:50 +0000 (20:49 +0900)
committer손주성/Tizen Platform Lab(SR)/Staff Engineer/삼성전자 <jusung07.son@samsung.com>
Wed, 2 Jun 2021 08:14:49 +0000 (17:14 +0900)
Signed-off-by: jusung <jusung07.son@samsung.com>
cion/channel/broadcast_channel.cc
cion/channel/broadcast_channel_implementation.hh
cion/vine/vine_dp.cc
cion/vine/vine_dp.hh
cion/vine/vine_interfaces.hh
cion/vine/vine_pubsub_dp.cc

index b7d3f403d0fb321a862d58b35dc804df121f2a41..a3ea5447308c5311e63c9f3ad64f71c2f9191b61 100644 (file)
@@ -73,44 +73,46 @@ BroadcastChannel::Impl::Impl(BroadcastChannel* parent, std::string topic,
   dp_->SetDpReceivedEventHandler(this);
   dp_->SetPubSubJoinedEventHandler(this);
   dp_->SetPubSubleftEventHandler(this);
-
 }
 
 BroadcastChannel::Impl::~Impl() {
 }
 
-void BroadcastChannel::Impl::AddJoinedPeerInfo(std::string ip,
+void BroadcastChannel::Impl::AddJoinedPeerInfo(std::string peer_id,
     std::shared_ptr<PeerInfo> peer_info) {
-
-  joined_peerinfo_map_[ip] = peer_info;
+  LOG(INFO) << "AddJoinedPeerInfo [" << peer_id << "]";
+  joined_peerinfo_map_[peer_id] = peer_info;
 }
 
-void BroadcastChannel::Impl::RemoveJoinedPeerInfo(std::string ip) {
-  auto iter = joined_peerinfo_map_.find(ip);
+void BroadcastChannel::Impl::RemoveJoinedPeerInfo(std::string peer_id) {
+  LOG(INFO) << "RemoveJoinedPeerInfo [" << peer_id << "]";
+  auto iter = joined_peerinfo_map_.find(peer_id);
   if (iter == joined_peerinfo_map_.end())
     return;
 
   joined_peerinfo_map_.erase(iter);
 }
 
-std::shared_ptr<PeerInfo> BroadcastChannel::Impl::GetJoinedPeerInfo(std::string ip) {
-  auto iter = joined_peerinfo_map_.find(ip);
+void BroadcastChannel::Impl::ClearJoinedPeerInfo() {
+  joined_peerinfo_map_.clear();
+}
+
+std::shared_ptr<PeerInfo> BroadcastChannel::Impl::GetJoinedPeerInfo(std::string peer_id) {
+  LOG(INFO) << "GetJoinedPeerInfo [" << peer_id << "]";
+  auto iter = joined_peerinfo_map_.find(peer_id);
   if (iter == joined_peerinfo_map_.end())
     return nullptr;
 
   return iter->second;
 }
 
-void BroadcastChannel::Impl::OnJoined(VineDpPtr dp, std::string ip, int port) {
-  LOG(INFO)<< "OnJoined " << topic_ << "[" << dp_->GetPort() << "] - "
-      << ip << ":" << port;
-
+void BroadcastChannel::Impl::OnJoined(VineDpPtr dp, std::string peer_id) {
+  LOG(INFO)<< "OnJoined " << topic_ << "[" << dp_->GetPeerId() << "] [" << peer_id << "]";
   tizen_base::Parcel parcel;
-  parcel.WriteUInt32(cmd::CionCmd::PeerInfo);
 
-  std::string ip_info = dp_->GetIP() + std::string(":") + std::to_string(dp_->GetPort());
-  parcel.WriteString(ip_info);
+  parcel.WriteUInt32(cmd::CionCmd::PeerInfo);
 
+  parcel.WriteString(dp_->GetPeerId());
   std::vector<uint8_t> peer = my_peer_info_->Serialize();
   if (peer.size() == 0) {
     LOG(ERROR) << "fail to serialize peer info";
@@ -123,16 +125,17 @@ void BroadcastChannel::Impl::OnJoined(VineDpPtr dp, std::string ip, int port) {
   dp_->SendDataAsync(parcel.GetRaw());
 }
 
-void BroadcastChannel::Impl::OnLeft(VineDpPtr dp, std::string ip, int port) {
-  LOG(INFO)<< "OnLeft " << topic_ << " - " << ip << ":" << port;
+void BroadcastChannel::Impl::OnLeft(VineDpPtr dp, std::string peer_id) {
+  LOG(INFO)<< "OnLeft " << topic_ << " - " << peer_id;
 
   auto parcel = std::make_shared<tizen_base::Parcel>();
-  parcel->WriteString(ip + std::string(":") + std::to_string(port));
+
+  parcel->WriteString(peer_id);
 
   ChannelJob<BroadcastChannel> job(parent_, parcel);
   operation_queue_.Push(job);
 
-   g_idle_add_full(G_PRIORITY_HIGH,
+    g_idle_add_full(G_PRIORITY_HIGH,
       [](gpointer data) -> gboolean {
         BroadcastChannel::Impl* self =
             static_cast<BroadcastChannel::Impl*>(data);
@@ -141,14 +144,13 @@ void BroadcastChannel::Impl::OnLeft(VineDpPtr dp, std::string ip, int port) {
         self->operation_queue_.TryAndPop(job);
 
         auto parcel = job.GetParcel();
-        std::string ip = parcel->ReadString();
-        auto peer_info = self->GetJoinedPeerInfo(ip);
+        std::string peer_id = parcel->ReadString();
+        auto peer_info = self->GetJoinedPeerInfo(peer_id);
         if (peer_info == nullptr)
           return G_SOURCE_REMOVE;
 
         self->parent_->OnLeft(peer_info, self->topic_);
-
-        self->RemoveJoinedPeerInfo(ip);
+        self->RemoveJoinedPeerInfo(peer_id);
         return G_SOURCE_REMOVE;
       }, this, nullptr);
 }
@@ -185,20 +187,22 @@ void BroadcastChannel::Impl::OnReceived(VineDpPtr dp,
           case cmd::CionCmd::PeerInfo:
           {
             // read ip_info
-            std::string ip_info = parcel->ReadString();
-            auto jp = self->GetJoinedPeerInfo(ip_info);
+            std::string peer_id = parcel->ReadString();
+            auto jp = self->GetJoinedPeerInfo(peer_id);
             if (jp != nullptr) {
-               LOG(INFO) << " Already exist ip " << ip_info << " " << jp->GetAppID();
+               LOG(INFO) << " Already exist peer_id " << peer_id;
               break;
             }
 
+            LOG(INFO) << "[" << peer_id << "]";
+
             parcel->ReadUInt32(&size);
             unsigned char* received_data = new unsigned char[size];
             parcel->Read(received_data, size);
             auto peer_info = std::make_shared<PeerInfo>(received_data, size);
             delete[] received_data;
 
-            self->AddJoinedPeerInfo(ip_info, peer_info);
+            self->AddJoinedPeerInfo(peer_id, peer_info);
             self->parent_->OnJoined(peer_info, self->topic_);
             break;
           }
@@ -256,6 +260,8 @@ void BroadcastChannel::Impl::Unsubscribe() {
     LOG(ERROR) << "Failed to close VinePubSubDp";
     return;
   }
+
+  ClearJoinedPeerInfo();
   is_online_ = false;
   LOG(INFO) << "topic " << topic_ << ", " << channel_id_;
 }
index 88f7131f01875b16b5182a468399acc0acdeb51a..277174011a0d623edcb0fd20aca21a1dbfb4db8d 100644 (file)
@@ -48,13 +48,14 @@ class BroadcastChannel::Impl : private IVineDpTerminatedEventHandler,
   void OnTerminated(VineDpPtr dp) override;
   void OnOpened(VineDpPtr dp, int result) override;
   void OnReceived(VineDpPtr dp, std::vector<unsigned char> data) override;
-  void OnJoined(VineDpPtr dp, std::string ip, int port) override;
-  void OnLeft(VineDpPtr dp, std::string ip, int port) override;
+  void OnJoined(VineDpPtr dp, std::string peer_id) override;
+  void OnLeft(VineDpPtr dp, std::string peer_id) override;
   void Subscribe();
   void Unsubscribe();
   void Publish(IPayload* data);
   void AddJoinedPeerInfo(std::string ip, std::shared_ptr<PeerInfo> peer_info);
   void RemoveJoinedPeerInfo(std::string ip);
+  void ClearJoinedPeerInfo();
   std::shared_ptr<PeerInfo> GetJoinedPeerInfo(std::string ip);
 
   BroadcastChannel* parent_;
index 2e6c6d56cc6a59843c9847382894e8b484fe93b2..a436bb9f9f2ead96f41c504dc15ee9cda36deaac 100644 (file)
@@ -90,19 +90,18 @@ int VineDp::GetPort() const {
   return port;
 }
 
-std::string VineDp::GetIP() {
-  vine_address_family_e addr_family;
-  char* ip;
-  int ret = vine_dp_get_remote_ip(dp_, &addr_family, &ip);
+std::string VineDp::GetPeerId() {
+  char* id;
+  int ret = vine_dp_get_id(dp_, &id);
   if (ret != VINE_ERROR_NONE) {
-    LOG(ERROR) << "Failed to get ip from dp(" << static_cast<void*>(dp_)
+    LOG(ERROR) << "Failed to get id from dp(" << static_cast<void*>(dp_)
                << "): " << VineErrorToString(ret);
     return {};
   }
 
-  std::string ip_str = std::string(ip);
-  free(ip);
-  return ip_str;
+  std::string id_str = std::string(id);
+  free(id);
+  return id_str;
 }
 
 bool VineDp::Open() {
index 59f72611d31f953210c735437981286bb96e602a..9b8d03e85184b86e817c0f41987a383cebdf578c 100644 (file)
@@ -54,7 +54,7 @@ class VineDp {
 
   void SetSequenceId(unsigned int id);
   unsigned int GetSequenceId();
-  std::string GetIP();
+  std::string GetPeerId();
 
  protected:
   explicit VineDp(std::shared_ptr<VineSession> session);
index 2120790339c00133d048efcc25fdb195fa64c2f6..cd205fcb238af48aa2eb8edf9d00336b19931c24 100644 (file)
@@ -61,13 +61,13 @@ class IVineBrowser {
 class IVinePubSubJoinedEventHandler {
  public:
   virtual ~IVinePubSubJoinedEventHandler() = default;
-  virtual void OnJoined(VineDpPtr dp, std::string ip, int port) = 0;
+  virtual void OnJoined(VineDpPtr dp, std::string peer_id) = 0;
 };
 
 class IVinePubSubleftEventHandler {
  public:
   virtual ~IVinePubSubleftEventHandler() = default;
-  virtual void OnLeft(VineDpPtr dp, std::string ip, int port) = 0;
+  virtual void OnLeft(VineDpPtr dp, std::string peer_id) = 0;
 };
 
 }  // namespace cion
index 725c81cd82ce558ce8e8fd14c8e949f2f6d153ba..86d2cb31188591f52fe738eb28c27e54f4fa31ac 100644 (file)
@@ -73,9 +73,9 @@ VinePubSubDp::VinePubSubDp(std::shared_ptr<VineSession> session,
   }
 
   ret = vine_dp_set_peer_joined_cb(dp_ ,
-      [](vine_dp_h dp, const char *ip, int port, void *user_data) {
+      [](vine_dp_h dp, const char *peer_id, void *user_data) {
         LOG(INFO) << "dp(" << static_cast<void*>(dp) << ") joined : "
-            << ip << ":" << port;
+            << peer_id;
         VinePubSubDp* self = static_cast<VinePubSubDp*>(user_data);
         if (!self->joined_handler_) {
           LOG(WARNING) << "No joined_handler_ for pubsub dp("
@@ -84,7 +84,7 @@ VinePubSubDp::VinePubSubDp(std::shared_ptr<VineSession> session,
         }
 
         VineDpPtr sdp = std::make_shared<VineDp>(dp, false);
-        self->joined_handler_->OnJoined(sdp, std::string(ip), port);
+        self->joined_handler_->OnJoined(sdp, std::string(peer_id));
       }, this);
       if (ret != VINE_ERROR_NONE) {
       LOG(ERROR) << "Failed to set joined_cb of pubsub dp: "
@@ -93,9 +93,9 @@ VinePubSubDp::VinePubSubDp(std::shared_ptr<VineSession> session,
     }
 
   ret = vine_dp_set_peer_left_cb(dp_ ,
-      [](vine_dp_h dp, const char *ip, int port, void *user_data) {
+      [](vine_dp_h dp, const char *peer_id, void *user_data) {
         LOG(INFO) << "dp(" << static_cast<void*>(dp) << ") left : "
-            << ip << ":" << port;
+            << peer_id;
         VinePubSubDp* self = static_cast<VinePubSubDp*>(user_data);
         if (!self->left_handler_) {
           LOG(WARNING) << "No left_handler_ for pubsub dp("
@@ -104,7 +104,7 @@ VinePubSubDp::VinePubSubDp(std::shared_ptr<VineSession> session,
         }
 
         VineDpPtr sdp = std::make_shared<VineDp>(dp, false);
-        self->left_handler_->OnLeft(sdp, std::string(ip), port);
+        self->left_handler_->OnLeft(sdp, std::string(peer_id));
       }, this);
       if (ret != VINE_ERROR_NONE) {
       LOG(ERROR) << "Failed to set left_cb of pubsub dp: "